yidongnan / spring-cloud-consul-example

spring-cloud-consul-example is an example for microservices system
MIT License
178 stars 95 forks source link

Making the Consul Instance ID Unique #4

Open conanca opened 7 years ago

conanca commented 7 years ago

默认的instanceId是${spring.application.name}-${server.port},则向consul注册服务时的serviceId是service:${spring.application.name}-${server.port} 当 service 部署多个实例在某些容器平台时(port往往一致),会导致serviceId重复而只能注册最后一个服务。

可以通过添加以下配置避免这个问题:

spring.cloud.consul.discovery.instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

yidongnan commented 7 years ago

@conanca 感谢。但这个示例中不影响。

docker-compose scale service-a=2 service-b=3  

启动多台 service-a 时,每个服务对应的 ip 是不一样的。 所以你说的问题在这示例中不存在。 这里使用 service:${spring.application.name}-${server.port} 方式只是看起来直观一点。 如有serviceId冲突的情况到是可以改成你写的那种。