001-Spring Cloud Edgware.SR3 升級最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升級2.0.4.RELEASE注意問題點


一、前提

升級前 => 升級后

Spring Boot 1.5.x => Spring Boot 2.0.4.RELEASE

Spring Cloud Edgware SR3 => Spring Cloud Finchley.SR1

1.1、Eureka Server

ureka 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>

1.2、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>

1.3、Spring Cloud

注冊中心里面的客戶端實例IP顯示不正確

因為 Spring Cloud 獲取服務客戶端 IP 地址配置變更了。

升級前:

${spring.cloud.client.ipAddress}

升級后:

${spring.cloud.client.ip-address}

1.4、Spring Security

一般注冊中心、配置中心都會使用安全加密,就會依賴 spring-boot-starter-security 組件,升級后有幾個問題。

1.4.1、用戶名和密碼無法登錄

因為 Spring Security 的參數進行了變更。

升級前:

security:
  user:
    name:
    password:

升級后:

spring:
  security:
     user:
       name: 
       password:

客戶端訪問時候需要增加basic認證

示例如:https://github.com/bjlhx15/spring-cloud-base

  啟動服務注冊中心:discovery-eureka-ha-security1、discovery-eureka-ha-security2

  啟動服務提供者:provider-business-service1、provider-business-service1

  使用原始方式調用【restTemplate】comsumer-business-service1-org

  注意配置restTemplate的注入

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.basicAuthorization("admin", "111111").build();
    }

 

1.4.2、使用security注冊中心沒有注冊實例

如圖所示,沒有注冊實例,兩個注冊中心無法互相注冊。 

 

因為 Spring Security 默認開啟了所有 CSRF 攻擊防御,需要禁用 /eureka 的防御。

在 Application 入口類增加忽略eureka配置: 

package com.lhx.springcloud.discovery.configuration;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

禁用全部

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
 
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}

1.4.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 之后,之前的服務需要加密連接配置中心的又正常運行了。

1.5、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

Gateway 代替了 Zuul

 


免責聲明!

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



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