ToB企服应用市场:ToB评测及商务社交产业平台

标题: 在SpringCloud2023中快速集成SpringCloudGateway网关 [打印本页]

作者: 罪恶克星    时间: 2024-5-19 13:04
标题: 在SpringCloud2023中快速集成SpringCloudGateway网关
你好,这里是codetrend专栏“SpringCloud2023实战”。
本文主要简单介绍SpringCloud2023实战中SpringCoudGateway的搭建。
后续的文章将会介绍在微服务中使用熔断Sentinel、鉴权OAuth2、SSO等技能。
前言

网关的选型不多,目前spring支持和维护的项目是 Spring Cloud Gateway。
Spring Cloud Gateway作为一个轻量级、高性能、可定制的网关服务,具有与Spring生态系统的紧密集成、负载平衡、断路器等丰富的功能,适用于构建微服务架构中的网关层,提供统一的访问控制、路由转发和过滤处理等功能。
Gateway 具有以下优点:
前置条件

Gateway如何工作的


这张图大概说明了网关如何工作的。
客户端向 Spring Cloud Gateway 发送请求。如果网关处理器映射确定请求匹配某个路由,则将其发送到网关 Web 处理器。该处理器将请求通过特定于请求的过滤器链。
过滤器被分为前后两部分,原因是过滤器可以在署理请求发送之前和之后运行逻辑。
所有前置过滤器逻辑都会被执行。然后发出署理请求。署理请求发出后,将运行后置过滤器逻辑。
Gateway集成

引入pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <groupId>io.rainforest</groupId>
  7.         <artifactId>banana</artifactId>
  8.         <version>1.0</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>banana-gateway</artifactId>
  12.     <description>spring cloud gateway</description>
  13.     <packaging>jar</packaging>
  14.     <dependencies>
  15.         
  16.         <dependency>
  17.             <groupId>org.springframework.cloud</groupId>
  18.             <artifactId>spring-cloud-starter-gateway</artifactId>
  19.         </dependency>
  20.         <dependency>
  21.             <groupId>org.springframework.boot</groupId>
  22.             <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
  23.         </dependency>
  24.         
  25.         <dependency>
  26.             <groupId>org.springframework.cloud</groupId>
  27.             <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  28.         </dependency>
  29.         
  30.         <dependency>
  31.             <groupId>org.springframework.cloud</groupId>
  32.             <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  33.         </dependency>
  34.         
  35.         <dependency>
  36.             <groupId>io.rainforest</groupId>
  37.             <artifactId>banana-common-core</artifactId>
  38.         </dependency>
  39.         
  40.         <dependency>
  41.             <groupId>org.springdoc</groupId>
  42.             <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
  43.         </dependency>
  44.     </dependencies>
  45.     <build>
  46.         <plugins>
  47.             <plugin>
  48.                 <groupId>org.springframework.boot</groupId>
  49.                 <artifactId>spring-boot-maven-plugin</artifactId>
  50.             </plugin>
  51.         </plugins>
  52.     </build>
  53. </project>
复制代码
修改设置

  1. spring.application.name: gateway
  2. spring:
  3.   cloud:
  4.     zookeeper:
  5.       connect-string: localhost:2181
  6.     gateway:
  7.       discovery:
  8.         locator:
  9.           enabled: false
  10.           lowerCaseServiceId: true
  11.       routes:
  12.         - id: client1
  13.           uri: lb://client1
  14.           predicates:
  15.             - Path=/client1/**
  16. #          filters:
  17. #            - StripPrefix=0
  18.         - id: client2
  19.           uri: lb://client2
  20.           predicates:
  21.             - Path=/client2/**
  22.           filters:
  23.             - StripPrefix=0
  24.         - id: client3
  25.           uri: lb://client3
  26.           predicates:
  27.             - Path=/client3/**
  28.           filters:
  29.             - StripPrefix=0
  30. server:
  31.   port: 10100
  32.   servlet:
  33.     context-path: /gateway
复制代码
修改启动类

  1. package io.rainforest.banana.gateway;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient
  7. public class Application {
  8.         public static void main(String[] args) {
  9.                 SpringApplication.run(Application.class, args);
  10.         }
  11. }
复制代码
调用demo

不需要修改代码就可以开箱即用Gateway。此处仅展示如何调用。
实际情况中网关还需要使用它强大的Filter来实现各种功能。
在这里Gateway就相当于一个更加强大的Nginx。只做了路由分发。
完备源码信息查看可以在gitee或者github上搜索r0ad。
关于作者

来自一线全栈步伐员nine的探索与实践,持续迭代中。
欢迎关注或者点个小红心~

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4