gateway 常见错误总结

记录一下尝试使用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.
  • 这个错误提示,一开始以为是与spring-boot-starter-web的冲突,然后我把它排除了,后面又报其他错误,具体可以参考 spring-cloud-starter-netflix-eureka-server与web产生的问题。
  • 但是我用了它的方法,再次产生了问题,问题如下:
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中出错地方如下:
gateway 常见错误总结_第1张图片

这里明显少了一个空格,注意:**我划线的地方是写错的,具体看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, value = 'provided', annotations = array[@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

改正方法是把冒号改为等号。
gateway 常见错误总结_第2张图片

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

需要确保routes写正确,如下所示。
gateway 常见错误总结_第3张图片
如此,我可以通过 http://localhost:5555/user/users/show(通过gateway) 访问 http://localhost:8761/users/show(直接访问对应的springboot项目的controller)

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

1、添加maven依赖如下


	org.springframework.boot
	spring-boot-starter-actuator

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访问

你可能感兴趣的:(problems,java,gateway,spring,cloud)