Why Does Consul Register Only One Instance in Spring Cloud Finchley? Solutions Revealed
Spring Cloud’s Consul integration in Finchley can mistakenly register multiple service instances under a single ID, causing only one instance to appear in Consul; this article explains the root cause—instance‑ID naming based on service name and port—and provides two fixes via configuration or custom registry extension.
Due to the lack of Etcd support in Spring Cloud, most users still use Eureka or Consul for service registration. This article discusses a serious pitfall when using Spring Cloud Finchley with Consul 1.2.x: only one instance is registered.
Problem Interpretation
Problem: The issue may not appear during development, but when deploying multiple instances online, Consul shows only a single instance.
Cause: Spring Cloud Consul builds the instance ID as {spring.application.name}-{server.port}. When the port does not change, the IDs are identical. Older Consul versions treated identical IDs with different addresses as distinct, but Consul 1.2.x uses the instance ID as a unique identifier, leading to the problem.
Solution
Knowing the cause, two concrete solutions are presented:
Method 1: Configure a new instance‑ID rule
Set spring.cloud.consul.discovery.instance-id to a custom pattern, e.g. using a random number:
spring.cloud.consul.discovery.instance-id=${spring.application.name}-${random.int[10000,99999]}Method 2: Extend ConsulServiceRegistry
Override the register method to construct a globally unique ID, such as “service‑name‑ip‑port”. Example implementation:
public class MyConsulServiceRegistry extends ConsulServiceRegistry {
public MyConsulServiceRegistry(ConsulClient client, ConsulDiscoveryProperties properties,
TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) {
super(client, properties, ttlScheduler, heartbeatProperties);
}
@Override
public void register(ConsulRegistration reg) {
reg.getService().setId(reg.getService().getName() + "-" + reg.getService().getAddress() + "-" + reg.getService().getPort());
super.register(reg);
}
}By concatenating service name, IP address, and port, each instance receives a truly unique ID, ensuring proper registration in Consul.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
