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 變成了學不動系列了。。。

@ All 碼農們:你們升級了嗎?有遇到什么樣的坑?歡迎留言!

推薦:Spring Boot & Cloud 最強技術教程

 

 

轉自:https://www.cnblogs.com/javastack/p/9446837.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM