新聞中心
簡介
Spring Cloud Gateway ,相比之前我們使用的 Zuul(1.x) 它有哪些優(yōu)勢呢?Zuul(1.x) 基于 Servlet,使用阻塞 API,它不支持任何長連接,如 WebSockets。Spring Cloud Gateway 使用非阻塞 API,支持 WebSockets,支持限流等新特性。本文首先用官方的案例帶領大家來體驗下Spring Cloud的一些簡單的功能。
創(chuàng)新互聯(lián)公司始終堅持【策劃先行,效果至上】的經(jīng)營理念,通過多達十年累計超上千家客戶的網(wǎng)站建設總結了一套系統(tǒng)有效的營銷解決方案,現(xiàn)已廣泛運用于各行各業(yè)的客戶,其中包括:成都自上料攪拌車等企業(yè),備受客戶稱贊。
創(chuàng)建工程
創(chuàng)建工程springcloud工程,名為springcloud-gateway-hello
pom.xml 內(nèi)容如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.2.RELEASE
com.gf
springcloud-gateway-hello
0.0.1-SNAPSHOT
springcloud-gateway-hello
Demo project for Spring Boot
1.8
Greenwich.RELEASE
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
##創(chuàng)建一個簡單的路由
Spring Cloud Gateway 使用路由來處理對下游服務的請求。創(chuàng)建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可以很容易地用于創(chuàng)建路由。除了創(chuàng)建路由之外,RouteLocatorBuilder還允許你在路由中添加各種 predicates(斷言)和 filters,以便根據(jù)特定條件更改請求和響應。
上面創(chuàng)建的route可以讓請求“/get”請求都轉發(fā)到“http://httpbin.org/get”。在route配置上,我們添加了一個filter,該filter會將請求添加一個header,key為hello,value為world。
啟動項目,訪問http://127.0.0.1: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,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會將工程的請求轉發(fā)到“http://httpbin.org/get”,并且在轉發(fā)之前,加上一個filter,該filter會將請求添加一個header,key為hello,value為world。
使用Hystrix
在spring cloud gateway中可以使用Hystrix。Hystrix是 spring cloud中一個服務熔斷降級的組件,在微服務系統(tǒng)有著十分重要的作用。
Hystrix 在 spring cloud gateway中是以filter的形式使用的,代碼如下:
@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”。
現(xiàn)在寫的一個“/fallback”的l邏輯:
@RequestMapping("/fallback")
public String fallback() {
return "fallback";
}
使用curl執(zhí)行以下命令:
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 的請求執(zhí)行了hystrix的fallback的邏輯。
源碼:https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter13/springcloud-gateway-hello
網(wǎng)頁名稱:SpringCloudGateway入門
本文地址:http://www.dlmjj.cn/article/ipsppo.html