Spring Cloud 升级最新 Finchley 版本,踩了全部的坑!

Spring Boot 2.x 已经发布了好久,如今 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,如今一块儿为项目作一次总体框架升级。html

升级前 => 升级后web

Spring Boot 1.5.x => Spring Boot 2.0.2spring

Spring Cloud Edgware SR4 => Spring Cloud Finchley.RELEASE安全

Eureka Server

Eureka Server 依赖更新框架

升级前:curl

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

升级后:maven

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Eureka Client

由于配置中心须要做为服务注册到注册中心,因此须要升级 Eureka Client,其余依赖没有变更。ide

Eureka Client 依赖更新spring-boot

升级前:ui

<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 组件,升级后有几下两个问题。

一、用户名和密码没法登陆

由于 Spring Security 的参数进行了变动。

升级前:

security:
  user:
    name:
    password:

升级后:

spring:
  security:
     user:
       name: 
       password:

二、注册中心没有注册实例

如图所示,没有注册实例,两个注册中心没法互相注册。

由于 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);
	}
}

三、配置中心没法加解密

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

如今想变回以前的 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

总结

以上都是踩完全部的坑总结出来的解决方案,实际解决问题的过程远要复杂的多。版本变化有点大,本次已成功升级了 Spring Cloud 基础依赖,及注册中心(Eureka Server)、配置中心(Config Server)。

其余像 Gateway 代替了 Zuul, 及其余组件再慢慢升级,Spring Cloud 的快速发展令升级变得很是蛋疼,本文记录了升级过程当中踩过的全部的坑。。。

坑死了,已经保证编译、运行正常,其余还有什么坑不知道,刚升级完 Finchley 这个正式版本,Spring Cloud 刚刚又发布了 Finchley.SR1,感受 Spring Cloud 变成了学不动系列了。。。

@ All 码农们:大家升级了吗?有遇到什么样的坑?欢迎留言!

推荐:Spring Boot & Cloud 最强技术教程

相关文章
相关标签/搜索