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 servereureka 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因为配置中心需要作为服务注册到注册中心,所以需要升级 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>
spring cloud注册中心里面的客户端实例ip显示不正确
因为 spring cloud 获取服务客户端 ip 地址配置变更了。
升级前:
${spring.cloud.client.ipaddress}
升级后:
${spring.cloud.client.ip-address}
spring security一般注册中心、配置中心都会使用安全加密,就会依赖 spring-boot-starter-security 组件,升级后有几下两个问题。
1、用户名和密码无法登录
因为 spring security 的参数进行了变更。
升级前:
security: user: name: password:
升级后:
spring: security: user: name: password:
2、注册中心没有注册实例
如图所示,没有注册实例,两个注册中心无法互相注册。
因为 spring security 默认开启了所有 csrf 攻击防御,需要禁用 /eureka 的防御。
在 application 入口类增加忽略配置:
@enablewebsecuritystatic 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)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();}
重写之后:
@enablewebsecuritystatic class websecurityconfig extends websecurityconfigureradapter { @override protected void configure(httpsecurity http) throws exception { http.csrf().ignoringantmatchers("/**").and().authorizerequests().anyrequest() .authenticated().and().httpbasic(); }}
其实就是把 formlogin() 干掉了,又回到之前的 basic auth 认证方式,如下图所示。
现在我们又可以使用以下命令加解密了。
恢复 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
总结以上都是踩完所有的坑总结出来的解决方案,实际解决问题的过程远要复杂的多。版本变化有点大,本次已成功升级了 spring cloud 基础依赖,及注册中心(eureka server)、配置中心(config server)。
相关推荐:
有关spring boot的启动流程的相关介绍
spring boot之单元测试详解
以上就是基于 spring boot 2.x 的 finchley 版本解析的详细内容。