Spring Cloud网关不调用原始服务

时间:2018-09-25 07:30:09

标签: java spring spring-boot spring-cloud-gateway

我是第一次使用spring cloud网关。我的服务端点是http://localhost:8080/student/getlist

问题是,当我尝试使用云网关调用我的服务时,它给出了404。eureka正确显示了服务URL

我的网关属性在下面

spring.application.name=gateway
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
server.port=8085
management.endpoints.web.exposure.include=*

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes.id=student-service
spring.cloud.gateway.routes.uri=lb://student-service
spring.cloud.gateway.routes.predicates.Path=/student/**

下面是网关中的执行器路线

[{"route_id":"CompositeDiscoveryClient_GATEWAY","route_definition":{"id":"CompositeDiscoveryClient_GATEWAY","predicates":[{"name":"Path","args":{"pattern":"/GATEWAY/**"}}],"filters":[{"name":"RewritePath","args":{"regexp":"/GATEWAY/(?<remaining>.*)","replacement":"/${remaining}"}}],"uri":"lb://GATEWAY","order":0},"order":0},{"route_id":"CompositeDiscoveryClient_STUDENT-SERVICE","route_definition":{"id":"CompositeDiscoveryClient_STUDENT-SERVICE","predicates":[{"name":"Path","args":{"pattern":"/STUDENT-SERVICE/**"}}],"filters":[{"name":"RewritePath","args":{"regexp":"/STUDENT-SERVICE/(?<remaining>.*)","replacement":"/${remaining}"}}],"uri":"lb://STUDENT-SERVICE","order":0},"order":0}]

im试图通过以下方式通过网关调用服务端点

http://localhost:8085/student/getlist

URL上方,但这不起作用。 我究竟做错了什么。没有任何微服务的上下文路径。 示例代码在

https://github.com/ojith97/sample.git

2 个答案:

答案 0 :(得分:0)

尝试将属性更改为

spring.cloud.gateway.routes[0].id=student-service
spring.cloud.gateway.routes[0].uri=lb://student-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/student/**

答案 1 :(得分:0)

您必须使用此方法使网关知道eureka服务器

eureka.client.service-url.defaultZone = http:// user:pass @ localhost:8761 / eureka

然后阻止网关注册到eureka

eureka.client.register-with-eureka = false

第二个选项非常重要,因为它会导致使用lb:servicename结构进行负载平衡时出现404错误。