001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
一、前提
升级前 => 升级后
Spring Boot 1.5.x => Spring Boot 2.0.4.RELEASE
Spring Cloud Edgware SR3 => Spring Cloud Finchley.SR1
1.1、Eureka Server
ureka 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>
1.2、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>
1.3、Spring Cloud
注册中心里面的客户端实例IP显示不正确
因为 Spring Cloud 获取服务客户端 IP 地址配置变更了。
升级前:
${spring.cloud.client.ipAddress}
升级后:
${spring.cloud.client.ip-address}
1.4、Spring Security
一般注册中心、配置中心都会使用安全加密,就会依赖 spring-boot-starter-security
组件,升级后有几个问题。
1.4.1、用户名和密码无法登录
因为 Spring Security 的参数进行了变更。
升级前:
security:
user:
name:
password:
升级后:
spring:
security:
user:
name:
password:
客户端访问时候需要增加basic认证
示例如:https://github.com/bjlhx15/spring-cloud-base
启动服务注册中心:discovery-eureka-ha-security1、discovery-eureka-ha-security2
启动服务提供者:provider-business-service1、provider-business-service1
使用原始方式调用【restTemplate】comsumer-business-service1-org
注意配置restTemplate的注入
@Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.basicAuthorization("admin", "111111").build(); }
1.4.2、使用security注册中心没有注册实例
如图所示,没有注册实例,两个注册中心无法互相注册。
因为 Spring Security 默认开启了所有 CSRF 攻击防御,需要禁用 /eureka 的防御。
在 Application 入口类增加忽略eureka配置:
package com.lhx.springcloud.discovery.configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } }
禁用全部
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
1.4.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 之后,之前的服务需要加密连接配置中心的又正常运行了。
1.5、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
Gateway 代替了 Zuul
相关文章
- Spring Boot自动配置
- 如何运行Spring Boot应用
- 如何配置和使用Spring框架的bean
- Spring透过ApplicationListener来触发contextrefreshedevent事件
- spring: ?.运算符
- Spring Boot 之 HelloWorld详解
- spring boot下使用logback或log4j生成符合Logstash标准的JSON格式
- [Spring boot] A quick REST API Guide
- spring boot单元测试之十五:用mockmvc测试返回异步结果的controller(spring boot 2.4.4)
- spring cloud:搭建基于consul的服务提供者集群(spring cloud hoxton sr8 / spring boot 2.3.4)
- spring boot:用rocketmq发送延时消息用来取消订单(spring boot 2.3.3)
- spring boot 集成 kaptcha
- spring boot(一)
- [Kotlin Spring boot] Connect template with a backing bean
- spring cloud:搭建基于consul的服务提供者集群(spring cloud hoxton sr8 / spring boot 2.3.4)
- spring cloud:通过client访问consul集群(spring cloud hoxton sr8 / spring boot 2.3.4)
- Spring整合ActiveMQ完成消息队列MQ编程
- Docker with Spring Boot
- 【面试】Spring面试题
- Spring Boot 分库分表入门
- spring boot 热部署 实现 前端部分热更新 详细操作
- 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】
- spring cloud 与spring boot的版本对应总结
- Spring Boot + Spring Cloud 集成 Consul 服务注册发现
- 毕业设计 Spring Boot的家庭食谱管理系统(含源码+论文)
- spring boot下使用logback或log4j生成符合Logstash标准的JSON格式
- Quick Guide to Microservices with Spring Boot 2.0, Eureka and Spring Cloud
- 【Spring注解驱动开发】在@Import注解中使用ImportSelector接口导入bean
- 020-Spring Boot 监控和度量
- Spring Boot构建系统
- spring-boot-starter-jdbc与数据访问
- 【Spring Boot】SpringBoot 如何保证接口安全?老鸟们都是这么玩的!
- spring boot中打包插件spring-boot-maven-plugin和maven-jar-plugin的关联