Python
Microservices
Eureka Server
Spring Boot
Service Registration

How to register python microservices with my eureka server spring boot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A Python microservice can register with a Spring Boot Eureka server, but success depends on correct instance metadata and heartbeat settings. Most integration issues are not in code syntax, they come from wrong host naming, lease timing, or missing shutdown cleanup. A reliable setup includes explicit registration, health checks, and graceful deregistration.

Prepare the Spring Boot Eureka Server

Your Eureka server should already run with @EnableEurekaServer, but confirm that client registration is disabled on the server itself.

yaml
1# application.yml in Eureka server
2server:
3  port: 8761
4
5eureka:
6  client:
7    register-with-eureka: false
8    fetch-registry: false
9  instance:
10    hostname: localhost

Start the server and verify the dashboard at http://localhost:8761 before connecting Python clients.

Register Python Service with py_eureka_client

The simplest option is the py-eureka-client package.

bash
pip install py-eureka-client flask

Create a small Flask service with explicit registration.

python
1from flask import Flask, jsonify
2import py_eureka_client.eureka_client as eureka
3
4app = Flask(__name__)
5
6
7@app.get("/health")
8def health():
9    return jsonify(status="UP")
10
11
12@app.get("/billing/price")
13def price():
14    return jsonify(currency="USD", value=42.50)
15
16
17def register_with_eureka():
18    eureka.init(
19        eureka_server="http://localhost:8761/eureka/",
20        app_name="PY-BILLING",
21        instance_port=5000,
22        instance_host="localhost",
23        renewal_interval_in_secs=30,
24        duration_in_secs=90,
25        health_check_url="http://localhost:5000/health",
26        home_page_url="http://localhost:5000/",
27        metadata={
28            "version": "1.0.0",
29            "runtime": "python3.12"
30        }
31    )
32
33
34if __name__ == "__main__":
35    register_with_eureka()
36    app.run(host="0.0.0.0", port=5000)

After startup, PY-BILLING should appear in Eureka.

Configure Host and Port for Real Deployments

Localhost works only for local testing. In Docker or Kubernetes, other services cannot call localhost of your container. Use reachable addresses:

  • Docker compose: register container DNS name or mapped host.
  • Kubernetes: register service DNS name.
  • VM deployment: register private host name or private IP.

If your app sits behind a reverse proxy, ensure the instance port and health URL reflect the externally reachable route expected by callers.

Graceful Deregistration on Shutdown

If your process exits without deregistering, stale instances may remain until lease expiration. Add shutdown handling.

python
1import atexit
2import signal
3import sys
4
5
6def stop_registration(*_):
7    try:
8        eureka.stop()
9    finally:
10        sys.exit(0)
11
12
13atexit.register(lambda: eureka.stop())
14signal.signal(signal.SIGTERM, stop_registration)
15signal.signal(signal.SIGINT, stop_registration)

This reduces stale entries and improves traffic routing during rollouts.

Verify Discovery from a Spring Boot Client

From another Spring service, enable Eureka client and call the Python service by its app name.

yaml
1# application.yml in Spring client
2eureka:
3  client:
4    service-url:
5      defaultZone: http://localhost:8761/eureka/
java
1@RestController
2@RequiredArgsConstructor
3public class BillingProxyController {
4    private final WebClient.Builder webClientBuilder;
5
6    @GetMapping("/proxy/price")
7    public Mono<String> getPrice() {
8        return webClientBuilder.build()
9                .get()
10                .uri("http://PY-BILLING/billing/price")
11                .retrieve()
12                .bodyToMono(String.class);
13    }
14}

If this call fails, inspect registered host, port, and health status in the dashboard first.

Operational Recommendations

Use short lease intervals in development and slightly longer ones in production to reduce churn. Track these metrics:

  • Registration success count.
  • Heartbeat failures.
  • Number of instances per service.
  • Evictions over time.

Also standardize service naming. Uppercase names like PY-BILLING are common in Eureka ecosystems and reduce confusion when Java and Python services coexist.

Security and Network Hardening

Use HTTPS between services whenever possible and keep Eureka access limited to trusted networks. If you run Eureka behind an ingress controller, verify forwarded headers so generated health links remain correct. Also set client connect and read timeouts to avoid startup hangs when the registry is temporarily unavailable. These controls keep registration stable during partial outages and reduce cascading failures.

Common Pitfalls

  • Registering localhost in containerized environments where callers cannot resolve it.
  • Exposing a health endpoint that always returns success even when dependencies are down.
  • Forgetting eureka.stop() during shutdown, leaving stale instances.
  • Setting lease durations too short, causing false evictions during temporary network delay.
  • Mismatching service name between registered app and client side discovery URL.

Summary

  • Use py-eureka-client with explicit host, port, and health URLs.
  • Verify dashboard registration before testing client side discovery.
  • Configure reachable network identity for non local deployments.
  • Add graceful deregistration to avoid stale entries.
  • Validate discovery end to end from a Spring Boot client by service name.

Course illustration
Course illustration

All Rights Reserved.