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 依賴更新
升級前:
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> |
升級后:
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> |
eureka client
因為配置中心需要作為服務注冊到注冊中心,所以需要升級 eureka client,其他依賴沒有變動。
eureka client 依賴更新
升級前:
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> |
升級后:
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid> </dependency> |
spring cloud
注冊中心里面的客戶端實例ip顯示不正確
因為 spring cloud 獲取服務客戶端 ip 地址配置變更了。
升級前:
1
|
${spring.cloud.client.ipaddress} |
升級后:
1
|
${spring.cloud.client.ip-address} |
spring security
一般注冊中心、配置中心都會使用安全加密,就會依賴 spring-boot-starter-security
組件,升級后有幾下兩個問題。
1、用戶名和密碼無法登錄
因為 spring security 的參數進行了變更。
升級前:
1
2
3
4
|
security: user: name: password: |
升級后:
1
2
3
4
5
|
spring: security: user: name: password: |
2、注冊中心沒有注冊實例
如圖所示,沒有注冊實例,兩個注冊中心無法互相注冊。
因為 spring security 默認開啟了所有 csrf 攻擊防御,需要禁用 /eureka 的防御。
在 application 入口類增加忽略配置:
1
2
3
4
5
6
7
8
9
|
@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)
1
2
3
4
5
6
7
8
9
10
|
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(); } |
重寫之后:
1
2
3
4
5
6
7
8
9
10
|
@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 不能自由切換。
升級前:
1
|
spring-boot:run -drun.profiles=profile1 |
升級后:
1
|
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:
1
2
3
4
5
6
7
8
|
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
這個參數已經不能被識別了
我們來看看源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 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); } |
發現原來的ipaddress已經改為ip-address,那么我們在配置中心做相應的改正即可。
注:改為ip-address
不會對之前的老版本的項目產生影響,會自動解析并正確賦值
總結
以上都是踩完所有的坑總結出來的解決方案,實際解決問題的過程遠要復雜的多。版本變化有點大,本次已成功升級了 spring cloud 基礎依賴,及注冊中心(eureka server)、配置中心(config server)。
其他像 gateway 代替了 zuul, 及其他組件再慢慢升級,spring cloud 的快速發展令升級變得非常蛋疼,本文記錄了升級過程中踩過的所有的坑。。。
坑死了,已經保證編譯、運行正常,其他還有什么坑不知道,剛升級完 finchley 這個正式版本,spring cloud 剛剛又發布了 finchley.sr1,感覺 spring cloud 變成了學不動系列了。。。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/javastack/p/9446837.html