【Finchley】【升级变动】Spring Cloud 升级到Finchley版本后须要注意的地方

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

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(要链接注册中心的项目)

升级前: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>

Feign

升级前: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>

Ribbon

升级前: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>

Hystrix

升级前:

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

Zuul

升级前:

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

 

Spring Cloud

注册中内心面的客户端实例IP显示不正确,由于 Spring Cloud 获取服务客户端 IP 地址配置变动了。

升级前:

${spring.cloud.client.ipAddress}

升级后:

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

Spring Security

通常注册中心、配置中心都会使用安全加密,就会依赖 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 以后,以前的服务须要加密链接配置中心的又正常运行了。

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

相关文章
相关标签/搜索