spring boot升级到2.x的坑

升级到spring boot 2.x后,发现了好多坑,现记录下来。

1、pom文件依赖的变化

1.x中,依赖是这样的:

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

2.x,依赖是这样的:

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

猜测是将eureka移到netflix包中了。目前发现依赖变化的有:eureka、feign、hystrix

 

2、Repository取消了findOne(id)方法

1.x中,有findOne(id)方法

User user = userRepository.findOne(Long id)

2.x中,取消了findOne(id)方法,可以使用getOne(id)或者findById(id)

User user = userRepository.getOne(id);
User user = userRepository.findById(id).get();

 

3、使用eureka注册中心时,eureka客户端注册不了,报错如下:

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

原因是升级后,security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false。需要增加的配置类如下:

复制代码
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }

}
复制代码

 

4、springboot 2.1以上的版本,会出现启动时不打印日志的问题(一直卡着,但是服务启动成功),控制台打印如下:

问题原因:springboot高版本排除了log4j依赖,需要手动添加 

解决办法:在pom文件中增加依赖:

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
复制代码

 

5、配置context-path:

1.x中配置:

server:
  port: 8091
  context-path: /order

2.x中配置:

server:
  port: 8091
  servlet:
    context-path: /order

 

posted @   仅此而已-远方  阅读(2608)  评论(0)    收藏  举报
编辑推荐:
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 记一次 .NET某云HIS系统 CPU爆高分析
阅读排行:
· AI浏览器自动化实战
· C#/.NET/.NET Core技术前沿周刊 | 第 34 期(2025年4.7-4.13)
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· Chat to MySQL 最佳实践:MCP Server 服务调用
· .NET周刊【3月第4期 2025-03-23】
点击右上角即可分享
微信分享提示