Spring Cloud 在版本 2020.0.0 开始,去除了 Zuul 网关的使用,改用 Spring Cloud Gateway 作为网关。
Spring Cloud Gateway 基于 Spring WebFlux 框架实现,相对于 Zuul 来说,性能更高。
Spring Cloud Gateway 适用于许多不同的使用场景,包括但不限于:
本文讲述如何在 Spring Cloud 中使用 Nacos 作为注册中心,通过 Spring Cloud Gateway 实现 API 路由的功能。
由于需要使用 Nacos 作为注册中心,网关和微服务都注册到 Nacos服务上,因此,需要先启动 Nacos服务。
见上一篇:
在 Spring Cloud 项目 GoboyCloud 基础上创建一个 Spring Boot 子项目,添加pom.xml 依赖:
<?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">
<parent>
<groupId>com.cloud.goboy</groupId>
<artifactId>goboycloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>goboy-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-gateway</name>
<description>Spring Cloud Gateway</description>
<packaging>jar</packaging>
<dependencies>
<!-- 引入nacos 注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--API网关Gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
server:
port: 9090
spring:
application:
name: goboy-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:9001
gateway:
routes:
- id: nacos-provider
uri: lb://nacos-provider
predicates:
- Path=/provider/**
filters:
- StripPrefix=1
网关端口设置为 9090。由于需要连接 Nacos 注册中心,需要提供服务名称 goboy-gateway
,以及配置 Nacos 注册中心地址 127.0.0.1:9001。
接下来是网关的重要配置 spring.cloud.gateway.routes:
- Path=/provider/**
:这个断言指定了请求的路径必须以 "/provider/" 开头,且可以有任意后缀。只有满足这个条件的请求才会被应用这个路由规则。- StripPrefix=1
:这个过滤器指定了要去掉请求路径的前缀。在这里,它去掉了一个路径段,因此如果请求是 "/provider/example",则经过这个过滤器后,会变成 "/example"。package com.cloud.goboy.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class GoboyGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GoboyGatewayApplication.class, args);
}
}
复用文章《Spring Cloud 使用 Nacos 注册中心》服务提供者 nacos-provider 作为路由转发的微服务。
启动的实例如下图所示:
访问http://localhost:9000/provider/provider/hello
会将请求路由至 nacos-provider 的微服务,且请求接口地址为 /provider/hello,浏览器输出:
hello
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。