Spring Cloud Gateway ,相比以前咱們使用的 Zuul(1.x) 它有哪些優點呢?Zuul(1.x) 基於 Servlet,使用阻塞 API,它不支持任何長鏈接,如 WebSockets。Spring Cloud Gateway 使用非阻塞 API,支持 WebSockets,支持限流等新特性。本文首先用官方的案例帶領你們來體驗下Spring Cloud的一些簡單的功能。html
建立工程springcloud工程,名爲springcloud-gateway-hellojava
pom.xml 內容以下:git
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.gf</groupId> <artifactId>springcloud-gateway-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-gateway-hello</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
##建立一個簡單的路由
Spring Cloud Gateway 使用路由來處理對下游服務的請求。建立RouteLocator的Bean,在本案例將把全部請求路由到 http://httpbin.org。路由能夠經過多種方式配置:程序員
@SpringBootApplication public class SpringcloudGatewayHelloApplication { public static void main(String[] args) { SpringApplication.run( SpringcloudGatewayHelloApplication.class, args ); } @Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get") .filters(f -> f.addRequestHeader("Hello", "World")) .uri("http://httpbin.org:80")) .build(); } }
上述myRoutes方法RouteLocatorBuilder能夠很容易地用於建立路由。除了建立路由以外,RouteLocatorBuilder還容許你在路由中添加各類 predicates(斷言) 和 filters,以便根據特定條件更改請求和響應。github
上面建立的route可讓請求「/get」請求都轉發到「http://httpbin.org/get」。在route配置上,咱們添加了一個filter,該filter會將請求添加一個header,key爲hello,value爲world。web
啓動項目,訪問http://127.0.0.1:8080/get,顯示以下:spring
{ "args": { }, "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN,zh;q=0.9,zh-TW;q=0.8", "Cache-Control": "max-age=0", "Connection": "close", "Forwarded": "proto=http;host=\"127.0.0.1:8080\";for=\"127.0.0.1:55607\"", "Hello": "World", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", "X-Forwarded-Host": "127.0.0.1:8080" }, "origin": "127.0.0.1, 124.74.78.150", "url": "http://127.0.0.1:8080/get" }
可見當向gateway工程請求「/get」,gateway會將工程的請求轉發到「http://httpbin.org/get」,而且在轉發以前,加上一個filter,該filter會將請求添加一個header,key爲hello,value爲world。shell
在spring cloud gateway中可使用Hystrix。Hystrix是 spring cloud中一個服務熔斷降級的組件,在微服務系統有着十分重要的做用。
Hystrix 在 spring cloud gateway中是以filter的形式使用的,代碼以下:apache
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(p -> p .path("/get") .filters(f -> f.addRequestHeader("Hello", "World")) .uri("http://httpbin.org:80")) .route(p -> p .host("*.hystrix.com") .filters(f -> f.hystrix(config -> config .setName("mycmd") .setFallbackUri("forward:/fallback"))) .uri("http://httpbin.org:80")) .build(); }
在上面的代碼中,咱們使用了另一個router,該router使用host去斷言請求是否進入該路由,當請求的host爲 「*.hystrix.com」,都會進入該router,該router中有一個hystrix的filter,該filter能夠配置名稱、和指向性fallback的邏輯的地址,好比本案例中重定向到了「/fallback」。json
如今寫的一個「/fallback」的l邏輯:
@RequestMapping("/fallback") public String fallback() { return "fallback"; }
使用curl執行如下命令:
curl --dump-header - --header 'Host: www.hystrix.com' http://localhost:8080/delay/3
返回的響應爲:
HTTP/1.1 200 OK Content-Type: text/plain;charset=UTF-8 Content-Length: 8 fallback
可見,帶host www.hystrix.com 的請求執行了hystrix的fallback的邏輯。
源碼:https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter13/springcloud-gateway-hello
歡迎關注個人公衆號《程序員果果》,關注有驚喜~~