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 @   仅此而已-远方  阅读(2606)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示