1. 程式人生 > >Spring Cloud 升級最新 Finchley 版本,踩了所有的坑

Spring Cloud 升級最新 Finchley 版本,踩了所有的坑

Spring Boot 2.x 已經發布了很久,現在 Spring Cloud 也釋出了 基於 Spring Boot 2.x 的 Finchley 版本,現在一起為專案做一次整體框架升級。

升級前 => 升級後

Spring Boot 1.5.x => Spring Boot 2.0.2

Spring Cloud Edgware SR4 => Spring Cloud Finchley.RELEASE

Eureka Server

Eureka Server 依賴更新

升級前:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

升級後:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Eureka Client

因為配置中心需要作為服務註冊到註冊中心,所以需要升級 Eureka Client,其他依賴沒有變動。

Eureka Client 依賴更新

升級前:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

升級後:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Spring Cloud

註冊中心裡面的客戶端例項IP顯示不正確

因為 Spring Cloud 獲取服務客戶端 IP 地址配置變更了。

升級前:

${spring.cloud.client.ipAddress}

升級後:

${spring.cloud.client.ip-address}

Spring Security

一般註冊中心、配置中心都會使用安全加密,就會依賴 spring-boot-starter-security 元件,升級後有幾下兩個問題。

1、使用者名稱和密碼無法登入

因為 Spring Security 的引數進行了變更。

升級前:

security:
  user:
    name:
    password:

升級後:

spring:
  security:
     user:
       name: 
       password:

2、註冊中心沒有註冊例項

如圖所示,沒有註冊例項,兩個註冊中心無法互相註冊。

因為 Spring Security 預設開啟了所有 CSRF 攻擊防禦,需要禁用 /eureka 的防禦。

在 Application 入口類增加忽略配置:

@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

3、配置中心無法加解密

升級後發現訪問配置中心無法讀取到配置,也無法加解密配置資訊,訪問配置中心連結直接跳轉到了登入頁面。

現在想變回之前的 basic auth 認證方式,找原始碼發現是自動配置跳到了登入頁面,現在重寫一下。

自動配置原始碼: org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)

protected void configure(HttpSecurity http) throws Exception {
    logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");

    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin().and()
        .httpBasic();
}

重寫之後:

@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/**").and().authorizeRequests().anyRequest()
                .authenticated().and().httpBasic();
    }

}

其實就是把 formLogin() 幹掉了,又回到之前的 basic auth 認證方式,如下圖所示。

現在我們又可以使用以下命令加解密了。

如解密: curl http://xx.xx.xx.xx:7100/decrypt -d secret -u user:password

恢復 basic auth 之後,之前的服務需要加密連線配置中心的又正常運行了。

Maven

升級到 Spring Boot 2.x 之後發現 Spring Boot 的 Maven 啟動外掛不好用了,主要是 Profile 不能自由切換。

升級前:

spring-boot:run -Drun.profiles=profile1

升級後:

spring-boot:run -Dspring-boot.run.profiles=profile1

具體的請參考: https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

總結

以上都是踩完所有的坑總結出來的解決方案,實際解決問題的過程遠要複雜的多。版本變化有點大,本次已成功升級了 Spring Cloud 基礎依賴,及註冊中心(Eureka Server)、配置中心(Config Server)。

其他像 Gateway 代替了 Zuul, 及其他元件再慢慢升級,Spring Cloud 的快速發展令升級變得非常蛋疼,本文記錄了升級過程中踩過的所有的坑。。。

坑死了,已經保證編譯、執行正常,其他還有什麼坑不知道,剛升級完 Finchley 這個正式版本,Spring Cloud 剛剛又釋出了 Finchley.SR1,感覺 Spring Cloud 變成了學不動系列了。。。