场景
业务需要用到在nacos注册的第三方服务,页面直接调用服务,所以采用gateway转发请求的方式
过程
-
创建一个空的springboot项目
-
pom文件依赖,主要注意springboot和springcloud版本对应关系
点击查看代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>client-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
<dependencies>
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>stcsnapshots</id>
<name>stcsnapshots</name>
<url>http://10.10.10.149:8081/nexus/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>stcreleases</id>
<name>stcreleases</name>
<url>http://10.10.10.149:8081/nexus/content/repositories/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>cst-snapshots</id>
<name>cst-snapshots</name>
<url>http://maven.cst.asiainfo.com/repository/cst-snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>abc-snapshots</id>
<name>abc-snapshots</name>
<url>http://maven.cst.asiainfo.com/repository/abc-snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>cstmaven</id>
<name>cstmaven</name>
<url>http://maven.cst.asiainfo.com/repository/cstmaven/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>mgpd</id>
<name>mgpd</name>
<url>http://maven.cst.asiainfo.com/repository/mgpd-group/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>mavenpublic</id>
<name>mavenpublic</name>
<url>http://maven.cst.asiainfo.com/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<profiles>
</profiles>
</project>
- 配置application文件
点击查看代码
server.port=8080
spring.application.name=nacos-gateway
spring.cloud.nacos.discovery.server-addr=ip:port
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=myName
# 开启注册
spring.cloud.nacos.discovery.enabled=true
# gateway开启根据服务中心路由转发
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes[0].id=third-api
spring.cloud.gateway.routes[0].uri=lb://third-service-name
spring.cloud.gateway.routes[0].predicates[0]=Path=/third-api/**
# 1的意思是转发的时候会吧path中的serviceName ,也就是第一个去掉
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
- 启动类添加注解
点击查看代码
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动测试
在nacos上能看到注册的gateway服务
通过http://localhost:8080/third-api/test可以获取第三方服务的测试数据
标签:spring,nacos,转发,cst,com,gateway,cloud
From: https://www.cnblogs.com/idanyang/p/17817513.html