spring cloud gateway和nacos

問尤龍の時光 2021-09-19 19:23:25 阅读数:557

spring cloud gateway nacos

nacos安裝准備工作參考spring cloud整合nacos

1、pom引入依賴

主要是spring-cloud-starter-gateway

<?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>cn.com.wenyl.alibaba</groupId>
<artifactId>gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2、配置文件

2.1、bootstrap.yml

spring:
profiles:
active: dev
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
group: DEFAULT_GROUP
file-extension: yml

2.2、application.yml

server:
port: 8080
management:
endpoints:
web:
exposure:
include: '*'

2.3、nacos配置信息

image.png

spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: nacos-provider
uri: lb://nacos-provider
predicates:
- Path=/nacos-provider/**
filters:
- StripPrefix=1
- id: service-consumer
uri: lb://service-consumer
predicates:
- Path=/service-consumer/**
filters:
- StripPrefix=1

2.4、配置規則

Spring Cloud Gateway通過路由謂詞工廠來進行路徑規則配置

2.4.1、After配置

after配置只有一個時間參數,用於匹配指定時間之後的請求

spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]

2.4.2、Before配置

after配置只有一個時間參數,用於匹配指定時間之前的請求

spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]

2.4.3、Between配置

after配置有兩個時間參數,用於匹配指定時間範圍之間的請求

spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

2.4.4、Cookie配置

有兩個參數,第一個為Cookie的name,第二個為匹配對應值的正則錶達式,匹配符合條件的請求

spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p

2.4.5、Header配置

有兩個參數,第一個為Header的name,第二個為匹配對應值的正則錶達式,匹配符合條件的請求

spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+

2.4.6、Host配置

處理指定域名服務的請求

spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org

2.4.7、Method配置

通過HTTP方法來匹配

spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST

2.4.8、Path配置

通過請求路徑匹配

spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}

2.4.9、Query配置

query用於包含指定參數值的請求,有兩個參數,第一個是參數name,第二個是用於匹配值得正則錶達式

spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.

2.4.10、RemoteAddr配置

根據請求發起的IP進行攔截處理

spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24

2.4.11、Weight配置

根據權重進行配置,一共兩個參數,第一個是組名,第二個是在改組中的占比(相加為10)

spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2

錶示一個占比80%,一個占比20%


標題:spring cloud gateway和nacos
作者: wenyl
地址: http://www.wenyoulong.com/articles/2021/09/18/1631946018525.html

版权声明:本文为[問尤龍の時光]所创,转载请带上原文链接,感谢。 https://gsmany.com/2021/09/20210919192325184c.html