温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-09-10 13:48:29 来源:亿速云 阅读:142 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关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 Cloud升级最新Finchley版本的坑有哪些

因为 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、配置中心无法加解密

升级后发现访问配置中心无法读取到配置,也无法加解密配置信息,访问配置中心链接直接跳转到了登录页面。

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

现在想变回之前的 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 认证方式,如下图所示。

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

现在我们又可以使用以下命令加解密了。

如解密:
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升级最新Finchley版本的坑有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI