Spring Cloud升级最新Finchley版本的所有坑

 更新时间:2018年08月27日 10:51:46   作者:Java技术栈  
这篇文章主要介绍了Spring Cloud升级最新Finchley版本的所有坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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 变成了学不动系列了。。。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 关于log4j日志扩展---自定义PatternLayout

    关于log4j日志扩展---自定义PatternLayout

    这篇文章主要介绍了关于log4j日志扩展---自定义PatternLayout,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 如何解决maven搭建一直处于running:..状态问题

    如何解决maven搭建一直处于running:..状态问题

    在使用Maven搭建项目时,有时会遇到一直处于加载状态的情况,通过修改设置可以解决这个问题,具体步骤为:1. 打开File->Settings->Build, Execution, Deployment->Maven->running,然后在VMOptions中填写"-DarchetypeCatalog=internal"
    2024-09-09
  • Java四种常用线程池的详细介绍

    Java四种常用线程池的详细介绍

    今天小编就为大家分享一篇关于Java四种常用线程池的详细介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Spring Data JPA框架快速入门之自定义Repository接口

    Spring Data JPA框架快速入门之自定义Repository接口

    Spring Data JPA是Spring基于JPA规范的基础上封装的⼀套 JPA 应⽤框架,可使开发者⽤极简的代码即可实现对数据库的访问和操作,本篇我们来了解Spring Data JPA框架的自定义Repository接口
    2022-04-04
  • javafx 如何将项目打包为 Windows 的可执行文件exe

    javafx 如何将项目打包为 Windows 的可执行文件exe

    文章介绍了三种将JavaFX项目打包为.exe文件的方法:方法1使用jpackage(适用于JDK14及以上版本),方法2使用Launch4j(适用于所有JDK版本),方法3使用InnoSetup(用于创建安装包),每种方法都有其特点和适用范围,可以根据项目需求选择合适的方法,感兴趣的朋友一起看看吧
    2025-01-01
  • Spring Cloud @RefreshScope 原理及使用

    Spring Cloud @RefreshScope 原理及使用

    这篇文章主要介绍了Spring Cloud @RefreshScope 原理及使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Springboot插件开发实战分享

    Springboot插件开发实战分享

    这篇文章主要介绍了Springboot插件开发实战分享,文章通过新建aop切面执行类MonitorLogInterceptor展开详细的相关内容,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05
  • MyBatisPlus数据权限控制实现的三种方式

    MyBatisPlus数据权限控制实现的三种方式

    数据权限是保障数据安全的重要手段,本文主要介绍了MyBatisPlus数据权限控制实现的三种方式,具有一定的参考价值,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • spring boot整合Cucumber(BDD)的方法

    spring boot整合Cucumber(BDD)的方法

    本篇文章主要介绍了spring boot整合Cucumber(BDD)的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Java实现的快速查找算法示例

    Java实现的快速查找算法示例

    这篇文章主要介绍了Java实现的快速查找算法,结合具体实例形式分析了快速查找算法的原理与相关实现技巧,需要的朋友可以参考下
    2017-09-09

最新评论