1. 程式人生 > 其它 >怎麼關閉eureka的服務_SpringCloud微服務踩坑系列之二

怎麼關閉eureka的服務_SpringCloud微服務踩坑系列之二

技術標籤:怎麼關閉eureka的服務

一.關於Eureka和Config服務的logback.xml配置檔案問題

注意:在eureka和config服務中,如果使用logback.xml配置檔案,會導致在服務啟動的時候出現如下warn警告資訊,尤其是config服務,更嚴重的會導致服務無法啟動.

2018-10-09 15:09:32.738  WARN 8176 --- [ost-startStop-1] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2018-10-09 15:09:34.113  WARN 8176 --- [           main] o.s.c.n.a.ArchaiusAutoConfiguration      : No spring.application.name found, defaulting to 'application'
2018-10-09 15:09:34.113  WARN 8176 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2018-10-09 15:09:34.971  WARN 8176 --- [           main] c.n.eureka.cluster.PeerEurekaNodes       : The replica size seems to be empty. Check the route 53 DNS Registry

二.關於"Process finished with exit code 0"的問題

在SpringBoot專案啟動時,控制檯中可能會出現"Process finished with exit code 0"這樣的日誌資訊,這樣的資訊意味著你的程式正常執行完畢並退出。

可以科普一下exit code,在大部分程式語言中都適用.

exit code 0 表示程式執行成功,正常退出;

exit code 1 表示程式執行執行過程中遇到了某些問題或者錯誤,非正常退出.

在SpringBoot啟動時如果出現"Process finished with exit code 0",有可能是專案中沒有新增spring-boot-starter-web依賴包導致的.

解決方法,引入依賴:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

三.RENEWALS ARE LESSER THAN THE THRESHOLD問題

啟動兩個client,過了一會,停了其中一個,訪問註冊中心時,介面上顯示了上面的警告資訊:

Eureka server和client之間每隔30秒會進行一次心跳通訊,告訴server,client還活著。由此引出兩個名詞:

Renews threshold:server期望在每分鐘中收到的心跳次數
Renews (last min):上一分鐘內收到的心跳次數。

前文說到禁止註冊server自己為client,不管server是否禁止,閾值(threshold)是1。client個數為n,閾值為1+2*n(此為一個server且禁止自注冊的情況)
如果是多個server,且開啟了自注冊,那麼就和client一樣,是對於其他的server來說就是client,是要*2的

我開了兩個server,自注冊,相關資料如下

6218610878a7889fbfc27173b328d1cc.png

閾值:1+2*1
renews:
1)自注冊 2 + 2*1
2)非自注冊:2*1

Eurake有一個配置引數eureka.server.renewalPercentThreshold,定義了renews 和renews threshold的比值,預設值為0.85。當server在15分鐘內,比值低於percent,即少了15%的微服務心跳,server會進入自我保護狀態,Self-Preservation。在此狀態下,server不會刪除註冊資訊,這就有可能導致在呼叫微服務時,實際上服務並不存在。
這種保護狀態實際上是考慮了client和server之間的心跳是因為網路問題,而非服務本身問題,不能簡單的刪除註冊資訊

stackoverflow上,有人給出的建議是:
1、在生產上可以開自注冊,部署兩個server
2、在本機器上測試的時候,可以把比值調低,比如0.49
3、或者簡單粗暴把自我保護模式關閉

eureka.server.enableSelfPreservation=false

四.TransportException: Cannot execute request on any known server問題

升級到新版的SpringCloud之後,當eureka註冊中心配置了security的依賴

<!--注意:SpringBoot專案遵循的是約定大於配置的原則,當此處添加了security的依賴後,就會自動啟用eureka的安全驗證功能,否則就不開啟-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-security</artifactId>
</dependency>

之後,可能會導致一些問題,比如就可能會導致在啟動eureka客戶端的時候產生"TransportException: Cannot execute request on any known server"異常,使得eureka客戶端無法連線註冊中心.原因是新版的security預設啟用了csrf檢驗,會對註冊到註冊中心的例項進行csrf攔截,所以在客戶端連線eureka註冊中心的時候,預設情況下會導致Cannot execute request on any known server問題

解決辦法:

1.在eureka服務端程式碼中配置security的csrf檢驗為false;
2.客戶端中以使用者名稱密碼形式登入:
  client:
    service-url:
      defaultZone:  http://admin:[email protected]:1001/eureka/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * 新版的security預設啟用了csrf檢驗,所以在客戶端連線eureka註冊中心的時候,預設情況下會導致    
     * Cannot execute request on any known server問題.
     * 解決辦法:
     *    1.在eureka服務端程式碼中配置security的csrf檢驗為false;
     *    2.客戶端中以使用者名稱密碼形式登入:
     *      client:
     *        service-url:
     *          defaultZone:  http://admin:[email protected]:1001/eureka/
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /*直接關閉crsf功能:http.csrf().disable();*/
        /*對eureka的訪問放行*/
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}
eureka:
  client:
    service-url:
      #defaultZone:  http://localhost:1001/eureka/
      defaultZone:  http://admin:[email protected]:1001/eureka/