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 @ 2019-04-25 08:39  仅此而已-远方  阅读(2568)  评论(0编辑  收藏  举报