Spring cloud gateway 實現網關路由轉發和過濾功能簡單demo,源碼下載:https://download.csdn.net/download/m0_37732829/12558327
demo介紹
1. 網關
1.1.網關pom.xml配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
1.2.網關application.yml
spring:
cloud:
gateway:
routes:
- id: demo #路由的唯一id
uri: http://localhost:8080 #指向註冊中心的服務,使用lb:// 加上ServiceName,當然也可以通過http://localhost:8080指向
predicates: #要進行的斷言
- Path=/d1/** #路徑匹配
- Header=auth-name #請求頭匹配
- Header=auth-token
filters: #過濾器
- StripPrefix=1 #截取url,本例中就是會把/demo截掉,後面的部分纔是轉發的url
server:
port: 80
2. 被轉發的demo服務器配置
2.1.pom.xml配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2.2.application.yml
server:
port: 8080
3. 成果展示
1)啓動網關服務器gateway和測試服務器demo
2)打開postman,通過網關服務器gateway訪問demo服務器,配置請求頭auth-name和auth-token
springcloud-gateway的簡單路由轉發和請求過濾就完成了。