Spring Cloud升级最新Finchley版本的所有坑
作者:Java技术栈 发布时间:2021-09-02 07:21:51
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 Server
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
因为配置中心需要作为服务注册到注册中心,所以需要升级 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 入口类增加忽略配置:
@EnableWebSecurity
static 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();
}
重写之后:
@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
Failed to bind properties under ‘eureka.instance.instance-id' to java.lang.String:
Description:
Failed to bind properties under 'eureka.instance.instance-id' to java.lang.String:
Property: eureka.instance.instance-id
Value: ${spring.cloud.client.ipAddress}:${spring.application.name}:${spring.application.instance_id:${server.port}}
Origin: "eureka.instance.instance-id" from property source "bootstrapProperties"
Reason: Could not resolve placeholder 'spring.cloud.client.ipAddress' in value "${spring.cloud.client.ipAddress}:${spring.application.name}:${spring.application.instance_id:${server.port}}"
spring.cloud.client.ipAddress
这个参数已经不能被识别了
我们来看看源码:
# org.springframework.cloud.client.HostInfoEnvironmentPostProcessor
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
InetUtils.HostInfo hostInfo = getFirstNonLoopbackHostInfo(environment);
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("spring.cloud.client.hostname", hostInfo.getHostname());
map.put("spring.cloud.client.ip-address", hostInfo.getIpAddress());
MapPropertySource propertySource = new MapPropertySource(
"springCloudClientHostInfo", map);
environment.getPropertySources().addLast(propertySource);
}
发现原来的ipAddress已经改为ip-address,那么我们在配置中心做相应的改正即可。
注:改为ip-address
不会对之前的老版本的项目产生影响,会自动解析并正确赋值
总结
以上都是踩完所有的坑总结出来的解决方案,实际解决问题的过程远要复杂的多。版本变化有点大,本次已成功升级了 Spring Cloud 基础依赖,及注册中心(Eureka Server)、配置中心(Config Server)。
其他像 Gateway 代替了 Zuul, 及其他组件再慢慢升级,Spring Cloud 的快速发展令升级变得非常蛋疼,本文记录了升级过程中踩过的所有的坑。。。
坑死了,已经保证编译、运行正常,其他还有什么坑不知道,刚升级完 Finchley 这个正式版本,Spring Cloud 刚刚又发布了 Finchley.SR1,感觉 Spring Cloud 变成了学不动系列了。。。
来源:http://www.cnblogs.com/javastack/p/9446837.html


猜你喜欢
- 前言Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util
- 要完成一个轮播图片,首先想到的应该是使用ViewPager来实现。ViewPager已经有了滑动的功能,我们只要让它自己滚动。再加上下方的小
- 记录单击、双击实现过程,进行简单的封装,便于复用,包括常用的软件双击退出。双击实现:记录第一次点击时间,在设定时间内再次点击,则返回监听事件
- 一.SQLite的介绍1.SQLite简介SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入
- Map简介将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。此接口取代 Dictionary 类,后者完全是一个抽象
- 今天想说的就是能够在我们操作数据库的时候更简单的更高效的实现,现成的CRUD接口直接调用,方便快捷,不用再写复杂的sql,带吗简单易懂,话不
- 因为某些需求,要在特定的时间执行一些任务,比如定时删除服务器存储的数据缓存,定时获取数据以及定时发送推送等等,这时就需要用到定时任务了。定时
- 前言相信很多人对枚举并不陌生,枚举可以很方便和直观的管理一组特定值。如果我们在页面上直接输出我们希望匹配的汉语意思或则其他满足我们需求的语句
- Http通信概述Http通信主要有两种方式POST方式和GET方式。前者通过Http消息实体发送数据给服务器,安全性高,数据传输大小没有限制
- 日常工作中 Map 绝对是我们 Java 程序员高频使用的一种数据结构,那 Map 都有哪些遍历方式呢?这篇文章阿粉就带大家看一下,看看你经
- 前言Zuul 是Netflix 提供的一个开源组件,致力于在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。也有很多公司使用它来作为
- 一、Druid简介Druid是阿里开源的数据库连接池,作为后起之秀,性能比dbcp、c3p0更高,使用也越来越广泛。当然Druid不仅仅是一
- android在网络上下载文件,供大家参考,具体内容如下步骤: 1.使用HTTP协议下载文件- 创建一个HttpURLConnection对
- class文件中的访问标志信息位于常量池下面的2个字节是access_flags 。 access_flags 描述的是当前类(或
- 1、同步消息即时性较强,重要的消息,且必须有回执的消息,例如短信,通知(转账成功)生产者:public class Producer { &
- 本文实例讲述了C#获取每个年,月,周的起始日期和结束日期的方法。分享给大家供大家参考,具体如下:我们在写程序的时候往往要计算出年,月,周的开
- 目录前言I. 字符串转列表1. jdk支持方式2. guava方式3. apache-commonsII. 列表转字符串1. StringB
- 一 Paint ,Canvaspublic class drawView extends View{ private Paint
- 本文实例为大家分享了java实现KFC点餐系统的具体代码,供大家参考,具体内容如下package KFC点餐系统;//food 类 publ
- 介绍本次设计的是一个有33个按钮的科学计算器。可以进行加,减,乘,除,开根号,阶乘,次方,百分号,对数,三角函数的计算。实现思路通过点击按钮