Spring Cloud Gateway初體驗
版權宣告:本文為博主原創文章,歡迎轉載,轉載請註明作者、原文超連結 ,博主地址:http://blog.csdn.net/forezp。 https://blog.csdn.net/forezp/article/details/83792388
轉載請標明出處:
ofollow,noindex" target="_blank">https://www.fangzhipeng.com/springcloud/2018/11/06/sc-f-gateway1/
本文出自方誌朋的部落格
這篇文章講述瞭如何簡單地使用Spring Cloud Gateway,來源於Spring Cloud官方案例,地址https://spring.io/guides/gs/gateway 。
Spring Cloud Gateway是Spring Cloud官方推出的第二代閘道器框架,取代Zuul閘道器。閘道器作為流量的,在微服務系統中有著非常作用,閘道器常見的功能有路由轉發、許可權校驗、限流控制等作用。本文首先用官方的案例帶領大家來體驗下Spring Cloud的一些簡單的功能,在後續文章我會使用詳細的案例和原始碼解析來詳細講解Spring Cloud Gateway.
建立工程
本案例的的原始碼下載於 官方案例 ,也可以在我的Github上下載。工程使用的Spring Boot版本為2.0.5.RELEASE,Spring Cloud版本為Finchley.SR1。
新建一個工程,取名為sc-f-gateway-first-sight在工程的pom檔案引用工程所需的依賴,包括spring boot和spring cloud,以及gateway的起步依賴spring-cloud-starter-gateway,程式碼如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
** 注:詳細的pom檔案依賴,可以見原始碼。**
建立一個簡單的路由
在spring cloud gateway中使用RouteLocator的Bean進行路由轉發,將請求進行處理,最後轉發到目標的下游服務。在本案例中,會將請求轉發到http://httpbin.org:80這個地址上。程式碼如下:
@SpringBootApplication @RestController public class Application { public static void main(String[] args) { SpringApplication.run(Application.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的bean去建立路由,除了建立路由RouteLocatorBuilder可以讓你新增各種 predicates 和 filters ,predicates斷言的意思,顧名思義就是根據具體的請求的規則,由具體的route去處理,filters是各種過濾器,用來對請求做各種判斷和修改。
上面建立的route可以讓請求“/get”請求都轉發到“ http://httpbin.org/get”。在route配置上,我們添加了一個filter,該filter會將請求新增一個header,key為hello,value為world。
啟動springboot專案,在瀏覽器上http://localhost:8080/get,瀏覽器顯示如下:
{ "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,en;q=0.8", "Cache-Control": "max-age=0", "Connection": "close", "Cookie": "_ga=GA1.1.412536205.1526967566; JSESSIONID.667921df=node01oc1cdl4mcjdx1mku2ef1l440q1.node0; screenResolution=1920x1200", "Forwarded": "proto=http;host=\"localhost:8080\";for=\"0:0:0:0:0:0:0:1:60036\"", "Hello": "World", "Host": "httpbin.org", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36", "X-Forwarded-Host": "localhost:8080" }, "origin": "0:0:0:0:0:0:0:1, 210.22.21.66", "url": "http://localhost:8080/get" }
可見當我們向gateway工程請求“/get”,gateway會將工程的請求轉發到“ http://httpbin.org/get”,並且在轉發之前,加上一個filter,該filter會將請求新增一個header,key為hello,value為world。
注意HTTPBin展示了請求的header hello和值world。
使用Hystrix
在spring cloud gateway中可以使用Hystrix。Hystrix是 spring cloud中一個服務熔斷降級的元件,在微服務系統有著十分重要的作用。
Hystrix是 spring cloud gateway中是以filter的形式使用的,程式碼如下:
@Bean public RouteLocator myRoutes(RouteLocatorBuilder builder) { String httpUri = "http://httpbin.org:80"; return builder.routes() .route(p -> p .path("/get") .filters(f -> f.addRequestHeader("Hello", "World")) .uri(httpUri)) .route(p -> p .host("*.hystrix.com") .filters(f -> f .hystrix(config -> config .setName("mycmd") .setFallbackUri("forward:/fallback"))) .uri(httpUri)) .build(); }
在上面的程式碼中,我們使用了另外一個router,該router使用host去斷言請求是否進入該路由,當請求的host有“*.hystrix.com”,都會進入該router,該router中有一個hystrix的filter,該filter可以配置名稱、和指向性fallback的邏輯的地址,比如本案例中重定向到了“/fallback”。
現在寫的一個“/fallback”的l邏輯:
@RequestMapping("/fallback") public Mono<String> fallback() { return Mono.just("fallback"); }
Mono是一個Reactive stream,對外輸出一個“fallback”字串。
使用curl執行以下命令:
curl --dump-header - --header 'Host: www.hystrix.com' http://localhost:8080/delay/3
返回的響應為:
fallback
可見,帶hostwww.hystrix.com的請求執行了hystrix的fallback的邏輯。
本文通過官方的一個簡單的案例,來講解了spring cloud gateway的簡單用法,在spring cloud gateway中有2個重要的概念 predicates 和 filters ,它們個將會在後續文章講解。敬請期待。
原始碼下載
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-gateway-first-sight
掃碼關注公眾號有驚喜
(轉載本站文章請註明作者和出處 方誌朋的部落格 )