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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-11-24 工具类系列---【java8新特性-字符串拼接工具StringJoiner类】