Spring Cloud升級(jí)最新Finchley版本的所有坑
Spring Boot 2.x 已經(jīng)發(fā)布了很久,現(xiàn)在 Spring Cloud 也發(fā)布了 基于 Spring Boot 2.x 的 Finchley 版本,現(xiàn)在一起為項(xiàng)目做一次整體框架升級(jí)。
升級(jí)前 => 升級(jí)后
Spring Boot 1.5.x => Spring Boot 2.0.2
Spring Cloud Edgware SR4 => Spring Cloud Finchley.RELEASE
Eureka Server
Eureka Server 依賴(lài)更新
升級(jí)前:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
升級(jí)后:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
Eureka Client
因?yàn)榕渲弥行男枰鳛榉?wù)注冊(cè)到注冊(cè)中心,所以需要升級(jí) Eureka Client,其他依賴(lài)沒(méi)有變動(dòng)。
Eureka Client 依賴(lài)更新
升級(jí)前:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
升級(jí)后:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
Spring Cloud
注冊(cè)中心里面的客戶(hù)端實(shí)例IP顯示不正確
因?yàn)?Spring Cloud 獲取服務(wù)客戶(hù)端 IP 地址配置變更了。
升級(jí)前:
${spring.cloud.client.ipAddress}
升級(jí)后:
${spring.cloud.client.ip-address}
Spring Security
一般注冊(cè)中心、配置中心都會(huì)使用安全加密,就會(huì)依賴(lài) spring-boot-starter-security 組件,升級(jí)后有幾下兩個(gè)問(wèn)題。
1、用戶(hù)名和密碼無(wú)法登錄
因?yàn)?Spring Security 的參數(shù)進(jìn)行了變更。
升級(jí)前:
security: user: name: password:
升級(jí)后:
spring:
security:
user:
name:
password:
2、注冊(cè)中心沒(méi)有注冊(cè)實(shí)例
如圖所示,沒(méi)有注冊(cè)實(shí)例,兩個(gè)注冊(cè)中心無(wú)法互相注冊(cè)。

因?yàn)?Spring Security 默認(rèn)開(kāi)啟了所有 CSRF 攻擊防御,需要禁用 /eureka 的防御。
在 Application 入口類(lèi)增加忽略配置:
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
3、配置中心無(wú)法加解密
升級(jí)后發(fā)現(xiàn)訪問(wèn)配置中心無(wú)法讀取到配置,也無(wú)法加解密配置信息,訪問(wèn)配置中心鏈接直接跳轉(zhuǎn)到了登錄頁(yè)面。

現(xiàn)在想變回之前的 basic auth 認(rèn)證方式,找源碼發(fā)現(xiàn)是自動(dòng)配置跳到了登錄頁(yè)面,現(xiàn)在重寫(xiě)一下。
自動(dòng)配置源碼:
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();
}
重寫(xiě)之后:
@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
升級(jí)到 Spring Boot 2.x 之后發(fā)現(xiàn) Spring Boot 的 Maven 啟動(dòng)插件不好用了,主要是 Profile 不能自由切換。
升級(jí)前:
spring-boot:run -Drun.profiles=profile1
升級(jí)后:
spring-boot:run -Dspring-boot.run.profiles=profile1
具體的請(qǐng)參考: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)不能被識(shí)別了
我們來(lái)看看源碼:
# 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)原來(lái)的ipAddress已經(jīng)改為ip-address,那么我們?cè)谂渲弥行淖鱿鄳?yīng)的改正即可。
注:改為ip-address不會(huì)對(duì)之前的老版本的項(xiàng)目產(chǎn)生影響,會(huì)自動(dòng)解析并正確賦值
總結(jié)
以上都是踩完所有的坑總結(jié)出來(lái)的解決方案,實(shí)際解決問(wèn)題的過(guò)程遠(yuǎn)要復(fù)雜的多。版本變化有點(diǎn)大,本次已成功升級(jí)了 Spring Cloud 基礎(chǔ)依賴(lài),及注冊(cè)中心(Eureka Server)、配置中心(Config Server)。
其他像 Gateway 代替了 Zuul, 及其他組件再慢慢升級(jí),Spring Cloud 的快速發(fā)展令升級(jí)變得非常蛋疼,本文記錄了升級(jí)過(guò)程中踩過(guò)的所有的坑。。。
坑死了,已經(jīng)保證編譯、運(yùn)行正常,其他還有什么坑不知道,剛升級(jí)完 Finchley 這個(gè)正式版本,Spring Cloud 剛剛又發(fā)布了 Finchley.SR1,感覺(jué) Spring Cloud 變成了學(xué)不動(dòng)系列了。。。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于log4j日志擴(kuò)展---自定義PatternLayout
這篇文章主要介紹了關(guān)于log4j日志擴(kuò)展---自定義PatternLayout,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
如何解決maven搭建一直處于running:..狀態(tài)問(wèn)題
在使用Maven搭建項(xiàng)目時(shí),有時(shí)會(huì)遇到一直處于加載狀態(tài)的情況,通過(guò)修改設(shè)置可以解決這個(gè)問(wèn)題,具體步驟為:1. 打開(kāi)File->Settings->Build, Execution, Deployment->Maven->running,然后在VMOptions中填寫(xiě)"-DarchetypeCatalog=internal"2024-09-09
Spring?Data?JPA框架快速入門(mén)之自定義Repository接口
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開(kāi)發(fā)者?極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的訪問(wèn)和操作,本篇我們來(lái)了解Spring?Data?JPA框架的自定義Repository接口2022-04-04
javafx 如何將項(xiàng)目打包為 Windows 的可執(zhí)行文件exe
文章介紹了三種將JavaFX項(xiàng)目打包為.exe文件的方法:方法1使用jpackage(適用于JDK14及以上版本),方法2使用Launch4j(適用于所有JDK版本),方法3使用InnoSetup(用于創(chuàng)建安裝包),每種方法都有其特點(diǎn)和適用范圍,可以根據(jù)項(xiàng)目需求選擇合適的方法,感興趣的朋友一起看看吧2025-01-01
Spring Cloud @RefreshScope 原理及使用
這篇文章主要介紹了Spring Cloud @RefreshScope 原理及使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Springboot插件開(kāi)發(fā)實(shí)戰(zhàn)分享
這篇文章主要介紹了Springboot插件開(kāi)發(fā)實(shí)戰(zhàn)分享,文章通過(guò)新建aop切面執(zhí)行類(lèi)MonitorLogInterceptor展開(kāi)詳細(xì)的相關(guān)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
MyBatisPlus數(shù)據(jù)權(quán)限控制實(shí)現(xiàn)的三種方式
數(shù)據(jù)權(quán)限是保障數(shù)據(jù)安全的重要手段,本文主要介紹了MyBatisPlus數(shù)據(jù)權(quán)限控制實(shí)現(xiàn)的三種方式,具有一定的參考價(jià)值,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05

