點擊上方藍色字體,選擇「標星公眾號」
優質文章,第一時間送達
Gateway是什麼?Spring Cloud Gateway是Spring官方基於Spring 5.0,Spring Boot 2.0和Project Reactor等技術開發的網關,Spring Cloud Gateway旨在為微服務架構提供一種簡單而有效的統一的API路由管理方式。Spring Cloud Gateway作為Spring Cloud生態系中的網關,目標是替代ZUUL,其不僅提供統一的路由方式,並且基於Filter鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。
為什麼用Gateway? Spring Cloud Gateway 可以看做是一個 Zuul 1.x 的升級版和代替品,比 Zuul 2 更早的使用 Netty 實現異步 IO,從而實現了一個簡單、比 Zuul 1.x 更高效的、與 Spring Cloud 緊密配合的 API 網關。
Spring Cloud Gateway 裡明確的區分了 Router 和 Filter,並且一個很大的特點是內置了非常多的開箱即用功能,並且都可以通過 SpringBoot 配置或者手工編碼鏈式調用來使用。
Spring Cloud的親兒子, Zuul 1.x停止更新,Zuul 2 尚未完善
代碼如下(示例):
<?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>
<artifactId>cloud2020</artifactId>
<groupId>com.cloud.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-gateway-gateway9527</artifactId>
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<dependencies>
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定義的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.cloud.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--一般基礎配置類-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.application.yml文件代碼如下(示例):
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #開啟從註冊中心動態創建路由的功能,利用微服務名進行路由
routes:
- id: payment_routh #payment_route #路由的ID,沒有固定規則但要求唯一,建議配合服務名
#uri: http://localhost:8001 #匹配後提供服務的路由地址
uri: lb://cloud-payment-service #匹配後提供服務的路由地址
predicates:
- Path=/payment/get/** # 斷言,路徑相匹配的進行路由
- id: payment_routh2 #payment_route #路由的ID,沒有固定規則但要求唯一,建議配合服務名
#uri: http://localhost:8001 #匹配後提供服務的路由地址
uri: lb://cloud-payment-service #匹配後提供服務的路由地址
predicates:
- Path=/payment/lb/** # 斷言,路徑相匹配的進行路由
#- Header=X-Request-Id, \d+ # 請求頭要有X-Request-Id屬性並且值為整數的正則表達式
eureka:
instance:
hostname: cloud-gateway-service
client: #服務提供者provider註冊進eureka服務列表內
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka
4.主啟動類 GateWayMain9527代碼如下(示例):
package com.cloud.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
public static void main(String[] args) {
SpringApplication.run(GateWayMain9527.class, args);
}
}
網關測試添加網關前添加網關後版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連結和本聲明。
本文連結:
https://blog.csdn.net/weixin_44837082/article/details/113694746
鋒哥最新SpringCloud分布式電商秒殺課程發布
👇👇👇
感謝點讚支持下哈