1. 程式人生 > 其它 >GateWay的簡介和簡單使用

GateWay的簡介和簡單使用

簡介

# 0.原文翻譯- https://spring.io/projects/spring-cloud-gateway- 
    這個專案提供了一個在springmvc之上構建API閘道器的庫。
    springcloudgateway旨在提供一種簡單而有效的方法來路由到api,併為api提供橫切關注點,比如:安全性、監控/度量和彈性。

​# 1.特性- 基於springboot2.x 和 spring webFlux 和 Reactor 構建 響應式非同步非阻塞IO模型- 動態路由- 請求過濾

1.開發閘道器動態路由

閘道器配置有兩種方式一種是快捷方式(Java程式碼編寫閘道器),一種是完全展開方式(配置檔案方式)[推薦]

1.引入依賴
<!--引入gateway閘道器依賴-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2.編寫閘道器配置
server:
  port: 8088

spring:
  application:
    name: GATEWAY
  cloud:
    consul:
      host: localhost
      port: 8500
    gateway:
      routes:
        - id: category_route	# 指定路由唯一標識
          uri: http://localhost:8085 # 指定路由服務的地址
          predicates:
            - Path=/category	# 指定路由規則

        - id: product_route
          uri: http://localhost:8084
          predicates:
            - Path=/product

3.啟動gateway閘道器專案
直接爆出如下錯誤:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration 
required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.
在啟動日誌中發現,gateway為了效率使用webflux進行非同步非阻塞模型的實現,因此和原來的web包衝突,去掉原來的web即可

4.測試路徑即可
就會發現閘道器會將不同的訪問路徑轉發到不同的微服務API中