1.pom添加nacos-discovery依赖
<?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
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坐标。
标签:spring,nacos,auth,---,fast,gateway,cloud From: https://www.cnblogs.com/hujunwei/p/18566637