Eurek Ribbon Feign常见问题及解决
生活随笔
收集整理的這篇文章主要介紹了
Eurek Ribbon Feign常见问题及解决
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Eureka的首頁我們可以看到System Status,Environment,test,那我們怎么去修改這些信息呢,在Spring Cloud文檔里面是沒有提到這一塊的,但是在Eureka的文檔里面是有看到Configuring Eureka Clienthttps://github.com/Netflix/eureka/wiki/Configuring-EurekaIf you are running in the cloud environment, you will need to pass in the java commandline property -Deureka.datacenter=cloud so that the Eureka Client/Server knows to initialize the information specific to AWS cloud.你啟動的時候用-D的一個參數(shù),你可以指定-Deureka.datacenter=cloud,Eureka Client/Server他就知道,去初始化這個信息,AWS要的一些信息eureka.datacenter=cloudeureka.environment=productlocalhost:8761/
server.port=8761
#eureka.instance.hostname=eureka1#spring.application.name=microservice-discovery-eureka
#eureka.server.evictionIntervalTimerInMs=60000
eureka.client.serviceUrl.defaultZone=http://admin:1234@localhost:8761/eureka
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false#eureka.server.enableSelfPreservation=truesecurity.basic.enabled=true
security.user.name=admin
security.user.password=1234spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=truelogging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%neureka.datacenter=cloud
eureka.environment=product
Eureka他有的時候會出現(xiàn)一個紅字,相信大家都有遇到過,那這個紅字是什么呢,他的紅字內容是這個東西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進入了自動保護模式,那什么是自我保護模式,自我保護模式有什么特性呢When the Eureka server comes up, it tries to get all of the instance registry information from a neighboring node. If there is a problem getting the information from a node, the server tries all of the peers before it gives up. If the server is able to successfully get all of the instances,it sets the renewal threshold that it should be receiving based on that information. If any time,the renewals falls below the percent configured for that value (below 85% within 15 mins), the server stops expiring instances to protect the current instance registry information.In Netflix, the above safeguard is called as self-preservation mode and is primarily used asa protection in scenarios where there is a network partition between a group of clients and the Eureka Server. In these scenarios, the server tries to protect the information it already has. There may be scenarios in case of a mass outage that this may cause the clients to get the instances that do not exist anymore. The clients must make sure they are resilient to eureka server returning an instance that is non-existent or un-responsive. The best protection in these scenarios is to timeout quickly and try other servers.https://github.com/Netflix/eureka/wiki/Understanding-Eureka-Peer-to-Peer-Communication他說當Eureka服務器啟動時,他嘗試從相鄰節(jié)點獲取所有實例注冊表信息,在高可用的Eureka里面,他不是會相互復制嗎,如果從節(jié)點獲取的信息有問題,服務器在放棄之前嘗試所有對等體,如果服務器能夠成功獲取實例,他根據(jù)該信息設置其接收的更新閾值,在任何時間,續(xù)訂低于該值配置的百分比,服務器將停止,本應到期的實例,按理來說過一段時間沒有接收到心跳,他就應該去expiring,但是在保護模式他stop expiring,這是一個Eureka Server,這是一個Eureka Client,client每30秒給server搞心跳信息,server如果沒有90秒沒有接收到心跳信息,把這個client卡擦掉,但是假設server進入了自我保護模式,self-preservation,現(xiàn)在進入了自我保護模式,雖然他一直沒有接收到心跳,他懷疑是eureka server本身,他可能有問題,所以這個時候他就不把client的節(jié)點給踢掉了,這里有一個服務注冊節(jié)點,服務注冊列表把client給刪掉,現(xiàn)在就不刪了,他懷疑是eureka本身出了問題,在Netflix中,上述保護措施稱為自我保護模式,主要用戶在一組客戶端和Eureka服務器之間存在網(wǎng)絡分區(qū)的保護,因為他有保護模式呢,所以有的時候還比較麻煩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. 看到這一段,就說明進入保護模式了,只要進入保護模式,但是在開發(fā)的時候,我們往往只是做測試,現(xiàn)在我想把USER給踢掉,他不踢,STS會調用SpringBoot的shutdown hook,停止的一個鉤子,鉤子會主動地下線,你幫我下掉吧,是這個意思,netstat -ano | findstr 3306eureka他有一個配置eureka.server.enable-self-preservation 他可以把自我模式關掉,那這個時候我就可以做測試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: falseevictionIntervalTimerInMs: 4000Interval是一個定時器,但是在生產環(huán)境中,千萬不要把自我保護模式關掉
eureka:instance:leaseRenewalIntervalInSeconds: 10leaseExpirationDurationInSeconds: 30注意:
更改Eureka更新頻率將打破服務器的自我保護功能
https://github.com/spring-cloud/spring-cloud-netflix/issues/373eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/instance:preferIpAddress: trueinstance-id: ${spring.cloud.client.ipAddress}:${server.port}https://github.com/spring-cloud/spring-cloud-netflix/issues/203 Documentation: changing Eureka renewal frequency *WILL* break the self-preservation feature of the server #373他說這個東西你不要去修改,它會打破自我保護的特性,我們在這邊是應用名稱,應用的端口,那你這個時候就得關閉自我保護的模式,我這邊想顯示IP:端口怎么辦,instance-id: ${spring.cloud.client.ipAddress}:${server.port}我們可以用這個配置microservice-simple-provider-user:10.40.8.144:7900我立馬就知道應用的IP是多少,端口是多少,這只是一個拓展,1.7. Eureka配置最佳實踐總結
https://github.com/spring-cloud/spring-cloud-netflix/issues/203配置談不上什么最佳實踐,因為任何一種配置他不可能滿足所有的要求,那就要看你這個配置能否滿足要求,能滿足你的要求就是最佳實踐,只是做一個參考,像github標注為documention的,一般都是質量還不錯的文檔,可以做一些參考
第二個就是Ribbon,@Configuration和@ComponentScan這個包不能夠重疊,RestTemplate有一個坑,其實它不是Ribbon的坑,它是RestTemplate的坑@GetMapping("list-all")public List<User> listAll() {ArrayList<User> list = Lists.newArrayList();User user = new User(1L, "zhangsan");User user2 = new User(2L, "zhangsan");User user3 = new User(3L, "zhangsan");list.add(user);list.add(user2);list.add(user3);return list;}localhost:7900/list-all[{"id":1,"username":null,"name":"zhangsan","age":null,"balance":null},{"id":2,"username":null,"name":"zhangsan","age":null,"balance":null},{"id":3,"username":null,"name":"zhangsan","age":null,"balance":null}
]在ribbon項目里面@GetMapping("/list-all")public List<User> listAll() {// wrong// List<User> list = this.restTemplate.getForObject("http://microservice-provider-user/list-all", List.class);// for (User user : list) {// System.out.println(user.getId());// }// rightUser[] users = this.restTemplate.getForObject("http://microservice-simple-provider-user/list-all", User[].class);List<User> list2 = Arrays.asList(users);for (User user : list2) {System.out.println(user.getId());}return list2;}localhost:8010/list-all[{"id":1,"username":null,"name":"zhangsan","age":null,"balance":null},{"id":2,"username":null,"name":"zhangsan","age":null,"balance":null},{"id":3,"username":null,"name":"zhangsan","age":null,"balance":null}
]
?
總結
以上是生活随笔為你收集整理的Eurek Ribbon Feign常见问题及解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eureka常用配置详解
- 下一篇: 超时机制,断路器模式简介