一、Eureka
1.1、Eureka Environment的配置:
eureka.environment: 字符串
參考文檔:
https://github.com/Netflix/eureka/wiki/Configuring-Eureka
1.2. Eureka DataCenter的配置
eureka.datacenter: cloud
https://github.com/Netflix/eureka/wiki/Configuring-Eureka
這邊說:配置-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-Communication
1.4. Eureka注冊服務慢的問題如何解決?
eureka.instance.leaseRenewalIntervalInSeconds
參考文檔:
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
二、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;
三、 Feign
3.1. 自定義配置時,@Configuration和@ComponentScan包不應重疊
3.2. @FeignClient所在的接口中,不支持@GetMapping等組合注解
3.3. 使用@PathVariable時,需要指定其value
3.4. Feign暫不支持復雜對象作為一個參數