zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    redis数据同步之redis-shake
    spring拦截机制中Filter(过滤器)、interceptor(拦截器)和Aspect(切面)的使用及区别
    MySQL之MVCC与幻读
    Notepad
    mac环境,python+selenium环境搭建,解决chromedriver报错问题
    大规模抓取的抓取效率和抓取技巧问题
    安卓逆向8,解决app抓包抓不到的问题
    [loj6033]棋盘游戏
    [ccBB]Billboards
    [loj6051]PATH
  • 原文地址:https://www.cnblogs.com/xuwenjin/p/10733965.html
Copyright © 2011-2022 走看看