1.在springcloud中服务的Instance ID 默认值是:
s p r i n g . c l o u d . c l i e n t . h o s t n a m e : {spring.cloud.client.hostname}: spring.cloud.client.hostname:{spring.application.name}:KaTeX parse error: Expected '}', got 'EOF' at end of input: …n.instance\_id:{server.port}},
也就是:主机名:应用名:应用端口。如图1
2.可以自定义:
eureka.instance.instance-id= ∗ ∗ ∗ ∗ s p r i n g . c l o u d . c l i e n t . i p A d d r e s s ∗ ∗ ∗ ∗ : {****spring.cloud.client.ipAddress****}: ∗∗∗∗spring.cloud.client.ipAddress∗∗∗∗:{server.port}
eureka.instance.prefer-ip-address=true这时实例名字就是主机ip地址+端口号,如图:
3.那么问题来了,此时鼠标放在实例上url指向的依然是主机名+端口号,如下图。这样客户端是访问不到实例的,我们必须让实例url指向 主机ip+端口号
4.解决办法:
eureka.instance.hostname= ${spring.cloud.client.ipAddress}
这里我们通过设置主机名为主机ip,那么上图红框内容就会变成如下图:
这样我们就可以通过ip+端口号访问实例了。
总结:springcloud是为分布式提供了一站式服务,其中eureka用于服务治理,它是基于Netflix Eureka 做了封装。上面记录的这个实例无法访问的问题是我们做微服务时遇到的一个问题,网上找了很久也没找到解答,最后还是自己解决的。
访问实例时url指向 → 主机名+端口号 ,这样是访问不到的,只能通过 主机IP+端口号才能访问,解决办法:
eureka.instance.hostname= ${spring.cloud.client.ipAddress}