1 引言
Gateway顾名思义就是“网关”的意思,旨在为微服务提供统一的访问入口,然后转发到各个微服务,通常可以在网关中统一做安全认证、监控、限流等等功能,避免每个微服务都重复实现这些功能。
2 代码
本章演示的项目基于Spring Cloud Alibaba 快速学习之 Nacos中的项目进行迭代,源码链接在文章末尾列出。
新增三个子项目:gateway、gateway-order、gateway-user
- 父级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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>server-a</module>
<module>user-a</module>
<module>server-b</module>
<module>rocketmq-producer</module>
<module>rocketmq-consumer-a</module>
<module>rocketmq-consumer-b</module>
<module>gateway</module>
<module>gateway-user</module>
<module>gateway-order</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 为Spring Boot项目提供一系列默认的配置和依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath/>
</parent>
<dependencies>
<!-- Spring Boot核心-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot单元测试和集成测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot构建Web应用程序-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2.1 gateway
提供网关服务
1 gateway/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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>gateway</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Nacos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.1.2</version>
</dependency>
<!-- gateway 网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>4.1.5</version>
</dependency>
<!-- spring cloud 负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>4.1.4</version>
</dependency>
</dependencies>
</project>
2 gateway/src/main/resources/application.yml
server:
port: 8070
spring:
application:
name: gateway-server
main:
web-application-type: reactive #解决和spring-boot-starter-web冲突
cloud:
nacos:
config:
serverAddr: 127.0.0.1:8848 #nacos服务地址
gateway:
globalcors: # 全局的跨域处理
cors-configuration:
'[/**]':
allowCredentials: true
allowedOrigins: "*"
allowedMethods: "*"
allowedHeaders: "*"
routes: # 路由配置
- id: id1
uri: lb://gateway-order # 对应的nacos服务名称
predicates: # 断言规则
- Path=/order/**
- id: id2
uri: lb://gateway-user
predicates:
- Path=/user/**
点击查看更多断言规则
3 gateway/src/main/java/org/example/MainGateway.java
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient //spring cloud开启服务发现功能
@SpringBootApplication
public class MainGateway {
public static void main(String[] args) {
SpringApplication.run(MainGateway.class, args);
}
}
4 gateway/src/main/java/org/example/conf/GatewayGlobalFilter.java
这个类就是gateway全局过滤器,安全认证等功能可以在这里实现。
package org.example.conf;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* 全局过滤器
*/
@Component
public class GatewayGlobalFilter implements GlobalFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
System.out.println("url:" + path);
return chain.filter(exchange);
}
}
2.2 gateway-order
提供测试接口服务
1 gateway-order/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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>gateway-order</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Nacos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.1.2</version>
</dependency>
</dependencies>
</project>
2 gateway-order/src/main/resources/application.yml
server:
port: 8071
spring:
application:
name: gateway-order
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 #nacos服务地址
3 gateway-order/src/main/java/org/example/MainGatewayOrder.java
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient //spring cloud开启服务发现功能
@SpringBootApplication
public class MainGatewayOrder {
public static void main(String[] args) {
SpringApplication.run(MainGatewayOrder.class, args);
}
}
4 gateway-order/src/main/java/org/example/controller/TestController.java
package org.example.controller;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/order")
public class TestController {
private static final String serverName = "this is gateway-order";
@GetMapping("/get")
public String get() {
return serverName;
}
@GetMapping("/get/{string}")
public String get(@PathVariable String string) {
return serverName + ":" + string;
}
}
2.3 gateway-user
提供测试接口服务
1 gateway-user/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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>gateway-user</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Nacos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.1.2</version>
</dependency>
</dependencies>
</project>
2 gateway-user/src/main/resources/application.yml
server:
port: 8072
spring:
application:
name: gateway-user
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848 #nacos服务地址
3 gateway-user/src/main/java/org/example/MainGatewayUser.java
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient //spring cloud开启服务发现功能
@SpringBootApplication
public class MainGatewayUser {
public static void main(String[] args) {
SpringApplication.run(MainGatewayUser.class, args);
}
}
4 gateway-user/src/main/java/org/example/controller/TestController.java
package org.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class TestController {
private static final String serverName = "this is gateway-user";
@GetMapping("/get")
public String get() {
return serverName;
}
@GetMapping("/get/{string}")
public String get(@PathVariable String string) {
return serverName + ":" + string;
}
}
3 测试
1,启动nacos,在分别启动三个子项目。然后访问nacos控制台:http://192.168.31.220:8848/nacos/index.html,可以看到三个项目都已注册。
2,打开浏览器访问:http://localhost:8070/user/get,可以看到被转发到了gateway-user的/user/get接口。
3,打开浏览器访问:http://localhost:8070/order/get,可以看到被转发到了gateway-order的/order/get接口。
4 源码
Gitee代码链接