当前位置: 首页 > 知识库问答 >
问题:

Spring Cloud API网关路由不工作

庄子平
2023-03-14

我使用以下技术设计了一个微服务原型

  1. 尤里卡服务器(Discovery Server)
  2. Spring boot(后端服务)
  3. Spring Cloud API网关

上述服务正在启动,API网关后端服务Eureka服务器中注册

server.port=8080
eureka.client.serviceUrl.defaultZone = http://localhost:8083/eureka
spring.application.name=ApiGateway
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=lb://MICROSERVICE1
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**
server.port=8081
server.address=127.0.0.1
eureka.client.serviceUrl.defaultZone = http://localhost:8083/eureka
spring.application.name=MicroService1
error.whitelabel.enabled= false
@RestController
@RequestMapping("/service1")
public class HomeController {
    @GetMapping("/message")
    public String hello() {
        return "response from micro service1";
    }

}

当我向gate way发送请求时,它显示以下错误

2020-12-16 22:26:09.770 ERROR 16700 --- [ctor-http-nio-3] a.w.r.e.AbstractErrorWebExceptionHandler : [d3334561-1]  500 Server Error for HTTP GET "/service1/message"

java.net.UnknownHostException: failed to resolve 'LAPTOP-KU56B6A8' after 3 queries 
    at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013) ~[netty-resolver-dns-4.1.55.Final.jar:4.1.55.Final]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/service1/message" [ExceptionHandlingWebHandler]

如何解决上述问题。

共有1个答案

狄易安
2023-03-14

我修改了API Gate Way路由配置,如下所示

spring.cloud.gateway.routes[0].id=service1
spring.cloud.gateway.routes[0].uri=http://localhost:8081/service1/
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**

现在一切正常

 类似资料:
  • 我正在使用Spring Cloud。我的Spring boot应用程序、、和中有四个服务。我尝试使用API Gateway调用和。当我从API Gateway调用My时,它可以正常工作,但不起作用。当我尝试获取时,我收到未找到错误。我是Spring Cloud的新人。这是我的代码。 下面是我的网址,最后一个网址是404 实体 学院控制员 应用程序. yml 学生管理员实体 应用程序. yml 应用

  • 我正在使用Spring Cloud(hoxton.sr10)和Spring Boot(2.2.6.release) 我在尤里卡服务器8761注册了我的服务 gateway mainClass java

  • 我正在学习如何使用SpringCloud构建API网关。我已经浏览了有关如何传递参数的文档,所有示例似乎都显示了它们是硬编码的。但如果我有一个动态值呢? 例如,我有这种类型的请求:http://localhost:8080/people/lookup?searchKey=jdoe, 如何通过“jdoe”部分? 我尝试了下面的代码,只有当我硬编码代码中的值时,它才能工作。 我不确定如何使用提供的生成

  • 我想通过在eureka(应用程序名称)注册的服务ID在我的Spring云网关(无zuul)启用默认路由,但我总是得到404错误。 在我的聊天服务的引导程序中。yml我已经定义了应用程序名 在应用程序属性中: 当我进入eureka的仪表板时,我可以看到我的聊天服务和网关。 Eureka在网关应用程序中的配置与聊天服务相同,但我也有以下配置: 接下来,我还尝试添加显式路由,但这并不起作用,但如果我有发

  • 我有一个Spring云网关、一个eureka服务注册中心和两个微服务器。我使用http://localhost:8081/auth直接向sevice发送请求没有问题。当我想使用网关http://localhost:8080/auth时,我总是得到404错误响应。服务和网关都连接到eureka服务器。下面是我的代码: 网关 Application.Properties: 主要: 主要: 我已经遍历了