0409-服务注册与发现-Eurek Ribbon Feign常见问题及解决

1、Eureka

1.一、Eureka Environment的配置:

eureka.environment: 字符串

参考文档:git

https://github.com/Netflix/eureka/wiki/Configuring-Eurekagithub

1.2. Eureka DataCenter的配置

eureka.datacenter: cloud

https://github.com/Netflix/eureka/wiki/Configuring-Eurekaspring

这边说:配置-Deureka.datacenter=cloud,这样eureka将会知道是在AWS云上数组

用点的方式写法与缩进能够同时使用缓存

1.3. Eureka开启自我保护的提示

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.服务器

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,再也不删除服务注册表中的数据(也就是不会注销任何微服务)。 网络

https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communicationapp

1.4. Eureka注册服务慢的问题如何解决?

eureka.instance.leaseRenewalIntervalInSecondside

参考文档:spring-boot

http://cloud.spring.io/spring-cloud-static/Camden.SR1/#_why_is_it_so_slow_to_register_a_service

原文:

Why is it so Slow to Register a Service?

Being an instance also involves a periodic heartbeat to the registry (via the client’s serviceUrl) with default duration 30 seconds. A service is not available for discovery by clients until the instance, the server and the client all have the same metadata in their local cache (so it could take 3 heartbeats). You can change the period using eureka.instance.leaseRenewalIntervalInSeconds and this will speed up the process of getting clients connected to other services. In production it’s probably better to stick with the default because there are some computations internally in the server that make assumptions about the lease renewal period.

翻译:

做为实例还涉及到与注册中心的周期性心跳,默认持续时间为30秒(经过serviceUrl)。在实例、服务器、客户端都在本地缓存中具备相同的元数据以前,服务不可用于客户端发现(因此可能须要3次心跳)。你可使用eureka.instance.leaseRenewalIntervalInSeconds 配置,这将加快客户端链接到其余服务的过程。在生产中,最好坚持使用默认值,由于在服务器内部有一些计算,他们对续约作出假设。

1.5. 如何解决Eureka Server不踢出已关停的节点的问题?

单例STS下,正常退出是能够剔除的,若是强制关闭,是不剔除的

集群下,

server端:

eureka.server.enable-self-preservation (设为false,关闭自我保护主要) eureka.server.eviction-interval-timer-in-ms     清理间隔(单位毫秒,默认是60*1000)

client端:

eureka.client.healthcheck.enabled = true                           开启健康检查(须要spring-boot-starter-actuator依赖) eureka.instance.lease-renewal-interval-in-seconds =10 租期更新时间间隔(默认30秒) eureka.instance.lease-expiration-duration-in-seconds =30  租期到期时间(默认90秒)

示例:

服务器端配置:

eureka: server: enableSelfPreservation: false evictionIntervalTimerInMs: 4000

客户端配置:

eureka: instance: leaseRenewalIntervalInSeconds: 10 leaseExpirationDurationInSeconds: 30

注意:更改Eureka更新频率将打破服务器的自我保护功能,生产模式不容许关闭

https://github.com/spring-cloud/spring-cloud-netflix/issues/373

1.6. Eureka配置instanceId显示IP 

在Spring Cloud中,服务的Instance ID的默认值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}} ,也就是机器主机名:应用名称:应用端口 。所以在Eureka Server首页中看到的服务的信息相似以下:itmuch:microservice-provider-user:8000 。若是想要自定义这部分的信息怎么办?

eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
 instance: preferIpAddress: true instance-id: ${spring.cloud.client.ipAddress}:${server.port}

1.7. Eureka配置最佳实践总结

https://github.com/spring-cloud/spring-cloud-netflix/issues/203

2、Ribbon

2.1. 自定义配置时,@Configuration和@ComponentScan包不该重叠

2.2. 使用RestTemplate时,想要得到一个List时,应该用数组,而不该该直接用List

// wrong // List<User> list = restTemplate.getForObject("http://microservice-provider-user/list-all",List.class); // for (User u : list) { // System.out.println(u); // } // right
        User[] users = restTemplate.getForObject("http://microservice-provider-user/list-all", User[].class); List<User> list2 = Arrays.asList(users); return list2;

3、 Feign

3.1. 自定义配置时,@Configuration和@ComponentScan包不该重叠

3.2. @FeignClient所在的接口中,不支持@GetMapping等组合注解

3.3. 使用@PathVariable时,须要指定其value

3.4. Feign暂不支持复杂对象做为一个参数

相关文章
相关标签/搜索