一,注册中心页面 微服务不展示ip 信息
在微服务配置文件yml 添加代码
server:
port: 9002
spring:
application:
name: service-order
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
jpa:
database: MySQL
show-sql: true
open-in-view: true
eureka:
client:
service-url:
defaultZone: http://localhost:9003/eureka/,http://localhost:9004/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中展示注册服务id
添加如下代码即可:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中展示注册服务id
${spring.cloud.client.ip-address} :获取ip地址
二,微服务注册慢(微服务心跳机制 时间问题)
默认情况下,服务注册到Eureka Server
的过程较慢。
SpringCloud
官方文档中给出了详细的原因
![](https://i-blog.csdnimg.cn/blog_migrate/f31913cf572f3fafd0320b6a20f87606.png)
大致含义:服务的注册涉及到心跳,默认心跳间隔为
30s。在实例、服务器、客户端都在本地缓存中具 有相同的元数据之前,服务不可用于客户端发现(所以可能需要
3
次心跳)。可以通过配置
eureka.instance.leaseRenewalIntervalInSeconds
(心跳频率)加快客户端连接到其他服务的过程。在生产中,最好坚持使用默认值,因为在服务器内部有一些计算,他们对续约做出假设。
三,微服务节点剔除问题
默认情况下,由于Eureka Server
剔除失效服务间隔时间为
90s且存在自我保护的机制。所以不能有效而 迅速的剔除失效节点,这对开发或测试会造成困扰。
微服务删掉后,由于微服务的失效服务机制在90秒,所以导致,删掉一个微服务后,90秒内,这个服务还在注册中心,我们可以设置短点时间。
Eureka Server
:
配置关闭自我保护,设置剔除无效节点的时间间隔
server:
port: 9003
spring:
application:
name: eureka-server
# eureka 配置
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: true # 是否将自己注册到注册中心
fetch-registry: true # 是否从eureka 中获取注册信息
service-url:
defaultZone: http://${eureka.instance.hostname}:9004/eureka/ #eureka服务地址;如果是集群则是其它服务器地址,后面要加/eureka
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #默认注册中心排查到90秒后,微服务没有发送心跳,剔除服务,时间太长了。剔除服务时间间隔,单位:毫秒
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #默认注册中心排查到90秒后,微服务没有发送心跳,剔除服务,时间太长了。剔除服务时间间隔,单位:毫秒
Eureka Client
:
配置开启健康检查,并设置续约时间
server:
port: 9001
spring:
application:
name: service-product
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springclouddemo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
jpa:
database: MySQL
show-sql: true
open-in-view: true
eureka:
client:
service-url:
defaultZone: http://localhost:9003/eureka/,http://localhost:9004/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #eureka client 发送心跳给server端后,续约到期时间(默认为90秒)。
lease-renewal-interval-in-seconds: 5 # 发送心跳续约间隔(每一个心跳的间隔)
lease-expiration-duration-in-seconds: 10 #eureka client 发送心跳给server端后,续约到期时间(默认为90秒)。
lease-renewal-interval-in-seconds: 5 # 发送心跳续约间隔(每一个心跳的间隔)