知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
SpringCloud Gateway是基于WebFlux框架實現的網關伺服器
gateway網關路由配置有兩種方式
這兩種方式都可以實現網關路由,還可以同時使用,寫在配置配置檔案中對於運維更友好。
<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.netkiller</groupId> <artifactId>gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gateway</name> <url>http://www.netkiller.cn</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath /> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> </project>
package cn.netkiller.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
resources/application.yml
server: port: 8080 spring: application: name: spring-cloud-gateway cloud: gateway: routes: - id: linux uri: http://www.netkiller.cn predicates: - Path=/linux logging: level: org.springframework.cloud.gateway: TRACE org.springframework.http.server.reactive: DEBUG org.springframework.web.reactive: DEBUG reactor.ipc.netty: DEBUG
package com.springcloud.gateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/linux") .uri("http://www.netkiller.cn")) .build(); } }
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("path_route", r -> r.path("/ch/history/").uri("https://new.qq.com")).build(); }
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("query_route", r -> r.query("id", "1000").uri("https://news.netkiller.cn/news")).build(); }
curl http://localhost:8080/?id=1000
傳遞參數
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("query_route", r -> r.query("q").uri("https://cn.bing.com/search")).build(); }
http://localhost:8080/?q=netkiller
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("query_route", r -> r.query("q").and().path("/search").uri("https://cn.bing.com")) .build(); }
http://localhost:8080/search?q=netkiller