marven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: neo_route
uri: http://www.ityouknow.com
predicates:
- Path=/spring-cloud
各字段含義如下:
id:我們自定義的路由 ID,保持唯一
uri:目標服務地址
predicates:路由條件,Predicate 接受一個輸入參數,返回一個布爾值結果。
該接口包含多種默認方法來將 Predicate 組合成其他復雜的邏輯(比如:與,或,非)。
filters:過濾規則,本示例暫時沒用。
上面這段配置的意思是,配置了一個 id 為 neo_route 的路由規則,當訪問地址 http://localhost:8080/spring-cloud時會自動轉發到地址:http://www.ityouknow.com/spring-cloud。配置完成啟動項目即可在瀏覽器訪問進行測試,當我們訪問地址http://localhost:8080/spring-cloud 時會展示頁面展示如下:
原文參考:[
springcloud(十五):Spring Cloud 終于按捺不住推出了自己的服務網關 Gateway]( https://www.cnblogs.com/ityouknow/p/10141740.html