SpringCloud入門學習(SpringCloud整合Gateway實現網關服務)

SpringCloud入門學習(SpringCloud整合Gateway實現網關服務)

本篇將在 上一篇 的基礎上,介紹SpringCloud整合Gateway實現服務轉發。

  1. 新建一個moudle 名爲gateway

pom依賴如下,注意不要添加 spring-boot-starter-web 否則會啓動失敗

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
</dependencies>
  1. 啓動類添加註解 @EnableEurekaClient
  2. 配置文件,具體各個配置是什麼意思,我在配置文件已經寫的很清楚了,就不再贅述了,如下:
server:
  port: 8888
spring:
  profiles:
    active: path-route      #使用哪個配置文件
  application:
    name: HELLO-GATEWAY     #服務名

---         #三個橫線表示再創建一個配置文件
spring:
  profiles: path-route            #配置文件名 和 spring.profiles.active 相對應
  cloud:
    #設置路由規則
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01        #代表從註冊中心獲取服務,且以lb(load-balance)負載均衡方式轉發
        predicates:                     #斷言
        - Path=/service01/**            #表示將以/service01/**開頭的請求轉發到uri爲lb://MICROSERVICE01的地址上
        - After=2019-06-20T00:00:00.789-07:00[America/Denver] #表示在該時間點之後的時間,發出的請求會被路由到uri
#        filters:
#        - StripPrefix=1                #表示將Path的路徑/service01在轉發前去掉,如果設置StripPrefix=2,表示將/service01/*去掉 以此類推... 同時將spring.cloud.gateway.discovery.locator.enabled改爲false,如果不改的話,之前的localhost:8799/client01/test01這樣的請求地址也能正常訪問,因爲這時爲每個服務創建了2個router

      discovery:
        locator:
          #表示gateway開啓服務註冊和發現功能,
          #並且spring cloud gateway自動根據服務發現爲每一個服務創建了一個router,這個router將以服務名開頭的請求路徑轉發到對應的服務
          enabled: true
          #表示將請求路徑的服務名配置改成小寫  因爲服務註冊的時候,向註冊中心註冊時將服務名轉成大寫的了
          lower-case-service-id: true
          
---
spring:
  profiles: after-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示在該時間點之後的時間,發出的請求會被路由到uri
        - After=2019-06-20T00:00:00.789-07:00[America/Denver]
#        - After=1561098916602  也可以用long類型的時間戳格式

---
spring:
  profiles: before-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示在該時間點之前的時間,發出的請求會被路由到uri
        - Before=2019-12-20T00:00:00.789-07:00[America/Denver]

---
spring:
  profiles: between-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示在該時間點之間的時間,發出的請求會被路由到uri
        - Between=2019-02-20T00:00:00.789-07:00[America/Denver],2019-12-20T00:00:00.789-07:00[America/Denver]

---
spring:
  profiles: header-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示當請求的請求頭中有 key=Hello,value=World,發出的請求會被路由到uri
        - Header=Hello, World
        #可以是正則表達式 例如 - Header=Hello, \d+

---
spring:
  profiles: cookie-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示當請求帶有名爲Hello,值爲World的Cookie時,發出的請求會被路由到uri
        - Cookie=Hello, World

---
spring:
  profiles: host-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示當請求帶有host爲**.host.test時,發出的請求會被路由到uri
        - Host=**.host.test

---
spring:
  profiles: method-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示GET請求,都會被路由到uri
        - Method=GET

---
spring:
  profiles: query-route
  cloud:
    gateway:
      routes:
      - id: gateway
        uri: lb://MICROSERVICE01
        predicates:
        #表示請求帶有參數key=a, value=b時,該請求會被路由到uri
        - Query=a, b
  1. 根據配置文件的不同,請求路徑也會不一樣,由於本文只是涉及gateway的簡單使用,我也沒有做什麼太深入的研究,就到這裏啦。

本片源碼 -> 點我查看

下一篇,將介紹 SpringCloud整合Zipkin實現服務調用的鏈路追蹤

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章