升級到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