记录一下尝试使用gateway时碰到的各种问题。

1、gateway提示与spring-boot-starter-web冲突

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at com.yichen.gateway.GatewayApplication.main(GatewayApplication.java:25) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:205) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:177) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:158) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	... 9 common frames omitted
  • 这个错误提示好像跟直接在spring-cloud-starter-netflix-eureka-server中排除spring-boot-starter-tomcat类似。
  • 我也考虑过是版本不对应的问题,但是仍没有解决,最后我是想到用spring-cloud-starter-netflix-eureka-client 来替换原来的spring-cloud-starter-netflix-eureka-server,最终是可以了,而且不用排除任何东西


2、提示错误 failed to convert java.lang.String to org.springframework.cloud.gateway.handler.predicate.PredicateDefinition

错误参考:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'spring.cloud.gateway.routes[0].predicates[0]' to org.springframework.cloud.gateway.handler.predicate.PredicateDefinition:

    Property: spring.cloud.gateway.routes[0].predicates[0]
    Value: Path:/user/**
    Origin: class path resource [application.yml]:13:11
    Reason: failed to convert java.lang.String to org.springframework.cloud.gateway.handler.predicate.PredicateDefinition

Action:

Update your application's configuration

我的yml中出错地方如下:
我的yml局部

这里明显少了一个空格,注意:**我划线的地方是写错的,具体看3,



3、The elements [spring.cloud.gateway.routes[0].predicates[0].path] were left unbound.


上一小节中我加了空格就会变成现在这个问题。
错误内容如下:

APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@3aca2579 type = java.util.List<org.springframework.cloud.gateway.handler.predicate.PredicateDefinition>, value = 'provided', annotations = array<Annotation>[@javax.validation.constraints.NotEmpty(message={javax.validation.constraints.NotEmpty.message}, groups=[], payload=[]), @javax.validation.Valid()]] failed:

    Property: spring.cloud.gateway.routes[0].predicates[0].path
    Value: /user/**
    Origin: class path resource [application.yml]:13:17
    Reason: The elements [spring.cloud.gateway.routes[0].predicates[0].path] were left unbound.

Action:

Update your application's configuration

改正方法是把冒号改为等号。
正确实现

4、gateway正常运行,但是无法通过url访问到

需要确保routes写正确,如下所示。
正确填写
如此,我可以通过 http://localhost:5555/user/users/show(通过gateway) 访问 http://localhost:8761/users/show(直接访问对应的springboot项目的controller)

5、 暴露端点,即可以查看gateway有哪些端点

1、添加maven依赖如下

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、yml添加配置信息

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

3、通过如下两个url进行访问(端口根据以及ip根据自己设定进行调整)

  • http://localhost:5555/actuator
  • http://localhost:5555/actuator/gateway/routes

6、项目参考

链接:https://pan.baidu.com/s/1Sf6ysKOcIYynbsQle9uxCg
提取码:5cyz

运行其中的eu2,user,gateway2即可。
访问链接:
1、http://localhost:8761/ 查看eureka注册信息
2、http://localhost:8081/uses/show 通过springboot 查看数据信息
3、http://localhost:5555/user/users/show 通过gateway访问

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐