SpringCloud微服务踩坑系列之一

偏执的太偏执、 2024-02-19 12:13 122阅读 0赞

之前我在公司的项目中开发使用的Spring Boot是1.5.x版本,现在 2.x 已经发布了挺久,而Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,所以我把我们公司的项目自己做了一次重构,对整体框架进行了一次升级改造,在改造过程中,也踩了不少的坑,特记录如下.

升级前 => 升级后

Spring Boot 1.5.x => Spring Boot 2.0.3

Spring Cloud Edgware SR4 => Spring Cloud Finchley.RELEASE

一.Eureka Server

Eureka Server 依赖更新

升级前:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  4. </dependency>

升级后:

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

二.Eureka Client

因为配置中心需要作为eureka的服务实例注册到eureka服务中心,所以需要升级 Eureka Client,其他依赖没有变动。

Eureka Client 依赖更新

升级前:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>

升级后:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

三.Spring Cloud

注册中心里面的客户端实例IP显示不正确

因为 Spring Cloud 获取服务客户端 IP 地址配置变更了。

升级前:

  1. ${spring.cloud.client.ipAddress}

升级后:

  1. ${spring.cloud.client.ip-address}

四.Spring Security

一般注册中心、配置中心都会使用安全加密,就会依赖 spring-boot-starter-security 组件,升级后有几下两个问题。

1、用户名和密码无法登录

因为 Spring Security 的参数进行了变更。

升级前:

  1. security:
  2. user:
  3. name:
  4. password:

升级后:

  1. spring:
  2. security:
  3. user:
  4. name:
  5. password:

另外需要注意:

SpringBoot项目遵循的是约定大于配置的原则,当pom.xml文件中添加了security的依赖后,就会自动启用eureka的安全验证功能,否则就不会开启.

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-security</artifactId>
  4. </dependency>

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

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

Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!

因为 Spring Security 默认开启了所有 CSRF ×××防御,需要禁用 /eureka 的防御。

在 Application 入口类增加忽略配置:

  1. @EnableWebSecurity
  2. static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. http.csrf().ignoringAntMatchers("/eureka/**");
  6. super.configure(http);
  7. }
  8. }

3、配置中心无法加解密

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

Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!

现在想变回之前的 basic auth 认证方式,找源码发现是自动配置跳到了登录页面,现在重写一下。

自动配置源码:
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)

  1. protected void configure(HttpSecurity http) throws Exception {
  2. logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");
  3. http
  4. .authorizeRequests()
  5. .anyRequest().authenticated()
  6. .and()
  7. .formLogin().and()
  8. .httpBasic();
  9. }

重写之后:

  1. @EnableWebSecurity
  2. static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  3. @Override
  4. protected void configure(HttpSecurity http) throws Exception {
  5. http.csrf().ignoringAntMatchers("/**").and().authorizeRequests().anyRequest()
  6. .authenticated().and().httpBasic();
  7. }
  8. }

其实就是把 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 不能自由切换。

升级前:

  1. spring-boot:run -Drun.profiles=profile1

升级后:

  1. spring-boot:run -Dspring-boot.run.profiles=profile1

发表评论

表情:
评论列表 (有 0 条评论,122人围观)

还没有评论,来说两句吧...

相关阅读

    相关 服务系列SpringCloud01

    SpringCloud01 1.认识微服务 随着互联网行业的发展,对服务的要求也越来越高,服务架构也从单体架构逐渐演变为现在流行的微服务架构。这些架构之间有怎样的差