SpringCloud Gateway 访问 nacos 服务时404

gateway application.yml 配置

server:
  port: 8219

#使用服务发现路由
spring:
  cloud:
    gateway:
      httpclient:
        pool:
          max-idle-time: 5000
          max-connections: 30
      discovery:
        locator:
          enabled: false
      routes:
      #设置路由的id,一般和服务名相同
      - id: service-order-waybill
        #设置路由的地址,lb 是nacos服务注册中心的服务名
        uri: lb://service-order-waybill
        #设置路由断言,代理servicerId为 auth-service 的 /auth/路径
        predicates:
        - Path=/waybill/**

前端访问的地址:http://localhost:8219
访问后报404的响应信息:
`Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Jul 07 20:51:52 CST 2022
There was an unexpected error (type=Not Found, status=404).
No message available`

问题解决:
在pom.xml中引入自己需要访问的接口所在的模块信息:

        <!--引入需要调用的模块-->
        <dependency>
            <groupId>com.gxa</groupId>
            <artifactId>service_order_waybill</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway 是一个基于 Spring Framework 5、Project Reactor 和 Spring Boot 2 的反应式 API 网关。它旨在为微服务架构提供一种简单而有效的方式来路由请求,并提供一些常见的网关功能,如过滤器、负载均衡、熔断等。 而 Nacos 是一个动态服务发现、配置管理和服务管理平台,它提供了一种简单易用的方式来管理和配置微服务。可以通过 Nacos 提供的配置中心功能,实现动态配置 Spring Cloud Gateway 的路由规则。 下面是使用 Nacos 配置文件的动态路由的步骤: 1. 添加依赖:在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 2. 配置 Nacos:在 application.properties 或 application.yml 文件中配置 Nacos 的地址和其他相关配置: ```yaml spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 ``` 3. 创建动态路由配置文件:在 Nacos 的配置中心创建一个配置文件,例如 `gateway-route.yaml`,内容如下: ```yaml spring: cloud: gateway: routes: - id: demo_route uri: http://httpbin.org:80 predicates: - Path=/get ``` 4. 启用动态路由配置:在 Spring Cloud Gateway 的配置类中添加 `@RefreshScope` 注解,以支持动态刷新配置: ```java @Configuration @RefreshScope public class GatewayConfig { // 配置相关的 Bean } ``` 5. 启动应用程序:启动 Spring Boot 应用程序,并确保 Nacos 服务正常运行。 6. 动态刷新路由配置:在 Nacos 的配置中心修改 `gateway-route.yaml` 文件中的路由信息,并发布配置。 7. 查看动态路由:访问 Spring Cloud Gateway 的管理端点 `/actuator/gateway/routes`,可以看到动态路由已经生效。 通过以上步骤,就可以实现基于 Nacos 配置文件的动态路由。需要注意的是,Nacos 配置中心支持监听配置变更,当配置文件发生变化Spring Cloud Gateway 会自动刷新路由配置,无需重启应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值