Spring Cloud升級最新Finchley版本的所有坑
Spring Boot 2.x 已經(jīng)發(fā)布了很久,現(xiàn)在 Spring Cloud 也發(fā)布了 基于 Spring Boot 2.x 的 Finchley 版本,現(xiàn)在一起為項(xiàng)目做一次整體框架升級。
升級前 => 升級后
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
因?yàn)榕渲弥行男枰鳛榉?wù)注冊到注冊中心,所以需要升級 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
注冊中心里面的客戶端實(shí)例IP顯示不正確
因?yàn)?Spring Cloud 獲取服務(wù)客戶端 IP 地址配置變更了。
升級前:
${spring.cloud.client.ipAddress}
升級后:
${spring.cloud.client.ip-address}
Spring Security
一般注冊中心、配置中心都會使用安全加密,就會依賴 spring-boot-starter-security
組件,升級后有幾下兩個(gè)問題。
1、用戶名和密碼無法登錄
因?yàn)?Spring Security 的參數(shù)進(jìn)行了變更。
升級前:
security: user: name: password:
升級后:
spring: security: user: name: password:
2、注冊中心沒有注冊實(shí)例
如圖所示,沒有注冊實(shí)例,兩個(gè)注冊中心無法互相注冊。
因?yàn)?Spring Security 默認(rèn)開啟了所有 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、配置中心無法加解密
升級后發(fā)現(xiàn)訪問配置中心無法讀取到配置,也無法加解密配置信息,訪問配置中心鏈接直接跳轉(zhuǎn)到了登錄頁面。
現(xiàn)在想變回之前的 basic auth 認(rèn)證方式,找源碼發(fā)現(xiàn)是自動配置跳到了登錄頁面,現(xiàn)在重寫一下。
自動配置源碼:
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(); } }
其實(shí)就是把 formLogin()
干掉了,又回到之前的 basic auth 認(rèn)證方式,如下圖所示。
現(xiàn)在我們又可以使用以下命令加解密了。
如解密:
curl http://xx.xx.xx.xx:7100/decrypt -d secret -u user:password
恢復(fù) basic auth 之后,之前的服務(wù)需要加密連接配置中心的又正常運(yùn)行了。
Maven
升級到 Spring Boot 2.x 之后發(fā)現(xiàn) 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
Failed to bind properties under ‘eureka.instance.instance-id' to java.lang.String:
Description: Failed to bind properties under 'eureka.instance.instance-id' to java.lang.String: Property: eureka.instance.instance-id Value: ${spring.cloud.client.ipAddress}:${spring.application.name}:${spring.application.instance_id:${server.port}} Origin: "eureka.instance.instance-id" from property source "bootstrapProperties" Reason: Could not resolve placeholder 'spring.cloud.client.ipAddress' in value "${spring.cloud.client.ipAddress}:${spring.application.name}:${spring.application.instance_id:${server.port}}"
spring.cloud.client.ipAddress
這個(gè)參數(shù)已經(jīng)不能被識別了
我們來看看源碼:
# org.springframework.cloud.client.HostInfoEnvironmentPostProcessor @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment); LinkedHashMap<String, Object> map = new LinkedHashMap<>(); map.put("spring.cloud.client.hostname", hostInfo.getHostname()); map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress()); MapPropertySource propertySource = new MapPropertySource( "springCloudClientHostInfo", map); environment.getPropertySources().addLast(propertySource); }
發(fā)現(xiàn)原來的ipAddress已經(jīng)改為ip-address,那么我們在配置中心做相應(yīng)的改正即可。
注:改為ip-address
不會對之前的老版本的項(xiàng)目產(chǎn)生影響,會自動解析并正確賦值
總結(jié)
以上都是踩完所有的坑總結(jié)出來的解決方案,實(shí)際解決問題的過程遠(yuǎn)要復(fù)雜的多。版本變化有點(diǎn)大,本次已成功升級了 Spring Cloud 基礎(chǔ)依賴,及注冊中心(Eureka Server)、配置中心(Config Server)。
其他像 Gateway 代替了 Zuul, 及其他組件再慢慢升級,Spring Cloud 的快速發(fā)展令升級變得非常蛋疼,本文記錄了升級過程中踩過的所有的坑。。。
坑死了,已經(jīng)保證編譯、運(yùn)行正常,其他還有什么坑不知道,剛升級完 Finchley 這個(gè)正式版本,Spring Cloud 剛剛又發(fā)布了 Finchley.SR1,感覺 Spring Cloud 變成了學(xué)不動系列了。。。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java JDBC批量執(zhí)行executeBatch方法詳解
這篇文章主要介紹了Java JDBC批量執(zhí)行executeBatch方法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08詳解Java多態(tài)對象的類型轉(zhuǎn)換與動態(tài)綁定
這篇文章主要介紹了詳解Java多態(tài)對象的類型轉(zhuǎn)換與動態(tài)綁定,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09Java并發(fā)編程之Semaphore(信號量)詳解及實(shí)例
這篇文章主要介紹了Java并發(fā)編程之Semaphore(信號量)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06SpringBoot學(xué)習(xí)篇之@Valid與@Validated的區(qū)別
@Valid是使用Hibernate?validation的時(shí)候使用,@Validated是只用Spring?Validator校驗(yàn)機(jī)制使用,下面這篇文章主要給大家介紹了關(guān)于SpringBoot學(xué)習(xí)篇之@Valid與@Validated區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-11-11Springmvc調(diào)用存儲過程,并返回存儲過程返還的數(shù)據(jù)方式
這篇文章主要介紹了Springmvc調(diào)用存儲過程,并返回存儲過程返還的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)隊(duì)列簡單定義與用法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之循環(huán)隊(duì)列簡單定義與用法,簡要描述了循環(huán)隊(duì)列的概念、原理,并結(jié)合實(shí)例形式分析了java循環(huán)隊(duì)列的定義與使用方法,需要的朋友可以參考下2017-10-10