spring cloud 2020.0.* 踩坑记录
1.网关问题
1.1 spring-cloud-starter-gateway和spring-boot-starter-web冲突问题
-
问题:启动网关报错
Error starting ApplicationContext. To display the conditions report re-run your application with debug enabled. 2019-12-31 10:26:35.211 ERROR 13124 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type org.springframework.http.codec.ServerCodecConfigurer that could not be found. Action: Consider defining a bean of type org.springframework.http.codec.ServerCodecConfigurer in your configuration. Process finished with exit code 1
-
分析:gateway构建与Spring 5+,基于Spring boot 2.x响应式的、非阻塞式的API,同时,他支持webSockets和spring框架紧密集成 原因:启动时默认使用了spring-boot-starter-web的内置容器,不支持非阻塞 解决:如果gateway需要使用web,建议使用spring-webflux模块,webflux有一个全新的非阻塞的函数式Reactive Web框架,可以用来构建异步的、非阻塞的事件驱动服务,webflux参考这个大佬的解释
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
1.2 Spring Cloud Gateway + Nacos 无法发现服务
-
问题:报503 Service Unavailable 分析:springcloud2020弃用了Ribbon,因此Alibaba在2021版本nacos中删除了Ribbon的jar包,因此无法通过lb路由到指定微服务,出现了503情况。 解决:手动加入loadbalancer
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
2.spring cloud 2020配置文件 bootstrap.yml默认不生效
-
问题:bootstrap.yml配置文件不生效 分析:有提到,想用bootstrap,需要将spring.cloud.bootstrap.enabled设为true(注意这个必须设为系统变量或者环境变量)或 引入一个依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
该依赖会引入一个名为 org.springframework.cloud.bootstrap.marker.Marker 的类 ,进而导致以下判断为假: 进而执行后边的bootstrap文件内容加载逻辑。
-
解决:引入一个依赖bootstrap
<!-- bootstrap 启动器 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>