Gateway系列---【gateway集成nacos-discovery】

1.pom添加nacos-discovery依赖

注意:Spring Cloud版本如果Hoxton.M2 RELEASED版本之前的,Nacos Discovery默认是集成了Ribbon的,但是最新Alibaba-Nacos-Discovery在Hoxton.M2 RELEASED版本
之后弃用了Ribbon,使用Spring Cloud Loadbalancer作为客户端的负载均衡组件。从Spring Cloud 2020版本开始,Spring Cloud移除了 Ribbon,使用Spring Cloud Loa
dbalancer作为客户端的负载均衡组件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>fast-cloud-2021.x-2.6.13</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>fast-gateway</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!--gateway是由springcloud开发的,所以要引入springcloud依赖;网关是由webflux+netty+reactor开发的,所以不需要tomcat,不需要starter-web-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <--!如果接口报503,请检查pom中是否有spring-cloud-loadbalancer坐标-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
</project>

2.修改application.yml

server:
  port: 8080
spring:
  application:
    name: fast-gateway
  cloud:
    gateway:
      routes:
          #如果接口报503,请检查pom中是否有spring-cloud-loadbalancer坐标
        - id: fast-auth
          # 网关转发地址,要和注册到nacos上的服务名一致,fast-auth服务也要注册到nacos上,加上下面nacos的配置就行。
          uri: lb://fast-auth
          predicates:
            - Path=/fast-auth/**
#          filters:
#            - StripPrefix=1
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos

或者下面这个配置,上面和下面这个配置是同样效果,设置 spring.cloud.gateway.discovery.locator.enabled: true 的功能是在 Spring Cloud Finchley 版本中引入的。在这个版本及其之后的版本中,你可以通过设置该属性来启用动态服务发现,这样 Spring Cloud Gateway 就能自动将注册中心中的服务注册为路由。

注意:要转发的服务这里以fast-auth为例,server.servlet.context-path不能设置,用默认才可以。

server:
  port: 8080
spring:
  application:
    name: fast-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos

3.验证是否成功

访问fast-auth服务的test接口:localhost:8081/test,fast-auth的server.servlet.context-path: /fast-auth,通过网关访问:localhost:8080/fast-auth/test,能访问通即可,如果接口报503,请检查pom中是否有spring-cloud-loadbalancer坐标。

4.扩展点:RewritePath的用法

server:
  port: 8080
spring:
  application:
    name: fast-gateway
  cloud:
    gateway:
      routes:
          #如果接口报503,请检查pom中是否有spring-cloud-loadbalancer坐标
        - id: fast-auth
          # 网关转发地址,要和注册到nacos上的服务名一致
          uri: lb://fast-auth
          predicates:
            - Path=/fast-auth1/**  #匹配以 /fast-auth1 开头的请求
          filters:
            - StripPrefix=1 #去掉路径中的第一个部分,即 /fast-auth1
            - RewritePath=/(?<segment>.*), /fast-auth2/$\{segment} # StripPrefix=1去除了uri的第一层,将路径重写为带有 context-path 的路径
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos

posted on   少年攻城狮  阅读(27)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-11-24 工具类系列---【java8新特性-字符串拼接工具StringJoiner类】
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示