整理Spring Cloud 基于Spring Boot2.x.x版本升级后的改变

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/makyan/article/details/88639874

以前搞的Spring Cloud项目,是基于spring boot 1.5.x的,对应Spring Cloud 版本是Edgware,现在重新梳理Spring Cloud 相关知识点,发现很多都跟以前不一样了,比如 Feign中,1.0版本系列,是不允许使用@GetMapping注解的,现在使用Finchley版本,就支持。

以下基于Finchley版本整理了版本带来相关改变!欢迎各位纠正补充!

一、 版本升级后的改变

编号 改变项 Finchley以前版本 Finchley以后版本
1 对应Spring Boot版本 Spring Boot 1.5.x Spring Boot 2.x.x
2 Eureka Server 依赖 spring-cloud-starter-eureka-server spring-cloud-starter-netflix-eureka-server
3 Eureka Client 依赖 spring-cloud-starter-eureka spring-cloud-starter-netflix-eureka-client
4 Spring Cloud 注册中心中的客户端实例IP显示方式 ${spring.cloud.client.ipAddress} ${spring.cloud.client.ip-address}
5 spring-boot-starter-security 组件,注册中心、配置中心安全登录的配置方式,升级后配置文件属性前多了spring security. user.name:“name” security.user. password:“password” spring.security.user.name:“name” spring.security.user. password:“password”
6 注册中心没有注册实例 Spring Security 没有开启 CSRF 攻击防御 Spring Security 默认开启了所有 CSRF 攻击防御,需要禁用 /eureka 的防御。
7 配置中心无法加解密 升级后发现访问配置中心无法读取到配置,也无法加解密配置信息,访问配置中心链接直接跳转到了登录页面。
8 Maven,升级到 Spring Boot 2.x 之后发现 Spring Boot 的 Maven 启动插件不好用了,主要是 Profile 不能自由切换。 spring-boot:run -Drun.profiles=profile1 spring-boot:run -Dspring-boot.run.profiles=profile1
9 Feign的使用 Feign中接口的访问不能使用@GetMapping注解,只能使用@RequestMapping注解, 都支持
10 网关 zuul Gateway 代替了zuul
11 Hystrix 健康监测信息的显示 默认开启健康监测状态已经信息显示,即浏览器输入/actuator/health以及/actuator/hystrix.stream时,能显示健康监测状态及明细 Spring boot 2.x 使用了endpoint,所以不显示
12 Feign 使用Hystrix Feign整合的Hystrix 默认是开启Hystrix的 Feign整合的Hystrix 默认没有Hystrix

二、版本升级后的相关说明

(一)关于第5点的yml配置方式

Finchley以前版本:

security:
  user:
    name:
    password:

Finchley以后版本:

spring:
  security:
     user:
       name: 
       password:

(二)关于第6点

升级后需做关闭关闭csrf,//开启认证,如下代码:

package com.futurecloud.security;
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 FuturecloudWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); //关闭csrf
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
    }

}

( 三)关于第7点,配置中心无法加解密

升级后发现访问配置中心无法读取到配置,也无法加解密配置信息,访问配置中心链接直接跳转到了登录页面。
现在想变回之前的 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 之后,之前的服务需要加密连接配置中心的又正常运行了。


(四)关于第11点,的解决方法
Spring boot 2.x 使用了endpoint,导致/actuator/health,/actuator/hystrix.stream都无法访问,报404错误。
解决办法就是,通过配置,让endpoint允许显示/actuator/health,/actuator/hystrix.stream信息,
解决方式一:在application.yml中添加如下配置:


management:
  endpoint:
      health:    
        show-details: always   #允许显示/actuator/health信息

management:
  endpoints:
    web:
      exposure:
        include: ["hystrix-stream"]   #允许显示/actuator/hystrix.stream信息

management:
  endpoints:
    web:
      exposure:
        include: '*'   #允许显示所有信息,包括/actuator/health,/actuator/hystrix.stream

解决方式二:在Hystrix监控项目的启动类中添加方法,设置允许显示的信息


(四)关于第12点,的解决方法
在application.yml配置文件中开启Feign的Hystrix。


#开启hystrix配置
feign:
  hystrix:
    enabled: true

还有很多不同之处,后期使用中再来补充…

猜你喜欢

转载自blog.csdn.net/makyan/article/details/88639874