测试spring cloud zuul
关键代码如下,包含ZuulGatewayApplication文件,子pom文件和父pom文件。
@EnableZuulProxy
@SpringCloudApplication
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
}
pom文件(关键代码)
<!--
Eureka 客户端, 客户端向 Eureka Server 注册的时候会提供一系列的元数据信息, 例如: 主机, 端口, 健康检查url等
Eureka Server 接受每个客户端发送的心跳信息, 如果在某个配置的超时时间内未接收到心跳信息, 实例会被从注册列表中移除
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 服务网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
父pom文件(关键代码)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<properties>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
运行代码后出现两个错误及问题的解决方案。
- 问题1 :运行springcloud,提示bean被覆盖了,错误日志如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'proxyRequestHelper', defined in class path resource [org/springframework/cloud/netflix/zuul/ZuulProxyAutoConfiguration$NoActuatorConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/cloud/netflix/zuul/ZuulProxyAutoConfiguration$EndpointConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
解决方案:在application.yaml配置如下,但是这不是主要原因。在配置正确后报了第二个错误。
#其中allow-bean-definition-overriding是解决问题的关键。
server:
port: 9001
spring:
application: ad-gateway
main:
allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
- 问题2:spring-boot-autoconfigure/2.1.3.RELEASE找不到servlet方法,错误日志如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-03 16:45:02.347 ERROR 326972 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call the method org.springframework.boot.autoconfigure.web.ServerProperties$Servlet.getServletPrefix()Ljava/lang/String; but it does not exist. Its class, org.springframework.boot.autoconfigure.web.ServerProperties$Servlet, is available from the following locations:
jar:file:/D:/root/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.3.RELEASE/spring-boot-autoconfigure-2.1.3.RELEASE.jar!/org/springframework/boot/autoconfigure/web/ServerProperties$Servlet.class
It was loaded from the following location:
file:/D:/root/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.1.3.RELEASE/spring-boot-autoconfigure-2.1.3.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.autoconfigure.web.ServerProperties$Servlet
解决方案:
查看spring cloud官方文档,发现spring boot版本和spring cloud版本不一致导致的上述两个问题。官方文档版本匹配如下:
| Release Train | Boot Version |
|---|---|
| Greenwich | 2.1.x |
| Finchley | 2.0.x |
| Edgware | 1.5.x |
| Dalston | 1.5.x |
因此修改父pom文件的版本即可,代码如下:
将spring-cloud的版本改为Greenwich。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<properties>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>

3200

被折叠的 条评论
为什么被折叠?



