• 35648

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

如何自定义微服务的Instance ID

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/ITjs/2019/0611/558.html

本文我们来探讨如何自定义微服务的Intance ID。Instance ID用于唯一标识注册到Eureka Server上的微服务实例。

我们可在Eureka Server的首页直观地看到各个微服务的Instance ID。例如,图11-1中的itmuch:microservice-provider-user:8000 就是Intance ID。

图11-1 Eureka Server上的微服务列表

在Spring Cloud中,服务的Instance ID的默认值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}} 。如果想要自定义这部分的内容,只需在微服务中配置eureka.instance.instance-id 属性即可,例如:

1
2
3
4
5
6
spring:
application:
name: microservice-provider-user
eureka:
instance:
instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 将Instance ID设置成IP:端口的形式

这样,就可将微服务microservice-provider-user 的Instance ID设为IP:端口的形式。这样设置后,效果如图11-2所示。

图11-2 Eureka Server上的微服务列表

拓展阅读

Spring Cloud初始化Instance ID的相关代码:

  • org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
  • org.springframework.cloud.commons.util.IdUtils.getDefaultInstanceId(PropertyResolver)
  • org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean.getInstanceId()

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!