Spring Boot 2.x 已經發布了好久,如今 Spring Cloud 也發佈了 基於 Spring Boot 2.x 的 Finchley 版本,如今一塊兒爲項目作一次總體框架升級。html
Spring Boot 1.5.4 => Spring Boot 2.0.5web
Spring Cloud Dalston SR1 => Spring Cloud Finchley.SR1spring
升級前:安全
<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>
升級前:curl
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
升級後:maven
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
升級前:ide
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
升級後:spring-boot
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
升級前:ui
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency>
升級後:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
升級前:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
升級後:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
升級前:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
升級後:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
註冊中內心面的客戶端實例IP顯示不正確,由於 Spring Cloud 獲取服務客戶端 IP 地址配置變動了。
升級前:
${spring.cloud.client.ipAddress}
升級後:
${spring.cloud.client.ip-address}
通常註冊中心、配置中心都會使用安全加密,就會依賴 spring-boot-starter-security
組件,升級後有幾下兩個問題。
由於 Spring Security 的參數進行了變動。
升級前:
security:
user:
name:
password:
升級後:
spring:
security:
user:
name:
password:
如圖所示,沒有註冊實例,兩個註冊中心沒法互相註冊。
由於 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); } }
升級後發現訪問配置中心沒法讀取到配置,也沒法加解密配置信息,訪問配置中心連接直接跳轉到了登陸頁面。
如今想變回以前的 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 以後,以前的服務須要加密鏈接配置中心的又正常運行了。
升級到 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