這篇文章給大家分享的是有關(guān)Spring Cloud Gateway怎么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
成都創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)潼南,十多年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575Spring Cloud Gateway介紹
前段時間剛剛發(fā)布了Spring Boot 2正式版,Spring Cloud Gateway基于Spring Boot 2,是Spring Cloud的全新項目,該項目提供了一個構(gòu)建在Spring 生態(tài)之上的API網(wǎng)關(guān),包括:Spring 5,Spring Boot 2和Project Reactor。 Spring Cloud Gateway旨在提供一種簡單而有效的途徑來發(fā)送API,并為他們提供橫切關(guān)注點,例如:安全性,監(jiān)控/指標和彈性。當前最新的版本是v2.0.0.M8,正式版最近也會到來。
Spring Cloud Gateway的特征:
Java 8
Spring Framework 5
Spring Boot 2
動態(tài)路由
內(nèi)置到Spring Handler映射中的路由匹配
基于HTTP請求的路由匹配 (Path, Method, Header, Host, etc…)
過濾器作用于匹配的路由
過濾器可以修改下游HTTP請求和HTTP響應(yīng) (Add/Remove Headers, Add/Remove Parameters, Rewrite Path, Set Path, Hystrix, etc…)
通過API或配置驅(qū)動
支持Spring Cloud DiscoveryClient配置路由,與服務(wù)發(fā)現(xiàn)與注冊配合使用
vs Netflix Zuul
Zuul基于servlet 2.5(使用3.x),使用阻塞API。 它不支持任何長連接,如websockets。而Gateway建立在Spring Framework 5,Project Reactor和Spring Boot 2之上,使用非阻塞API。 Websockets得到支持,并且由于它與Spring緊密集成,所以將會是一個更好的開發(fā)體驗。
Spring Cloud Gateway入門實踐
筆者最近研讀了Spring Cloud Gateway的源碼,大部分功能的實現(xiàn)也寫了源碼分析的文章,但畢竟正式版沒有發(fā)布,本文算是一篇入門實踐,展示常用的幾個功能,期待最近的正式版本發(fā)布。
示例啟動兩個服務(wù):Gateway-Server和user-Server。模擬的場景是,客戶端請求后端服務(wù),網(wǎng)關(guān)提供后端服務(wù)的統(tǒng)一入口。后端的服務(wù)都注冊到服務(wù)發(fā)現(xiàn)Consul(搭建zk,Eureka都可以,筆者比較習(xí)慣使用consul)。網(wǎng)關(guān)通過負載均衡轉(zhuǎn)發(fā)到具體的后端服務(wù)。
用戶服務(wù)
用戶服務(wù)注冊到Consul上,并提供一個接口/test。
依賴
需要的依賴如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
配置文件
spring: application: name: user-service cloud: consul: host: 192.168.1.204 port: 8500 discovery: ip-address: ${HOST_ADDRESS:localhost} port: ${SERVER_PORT:${server.port}} healthCheckPath: /health healthCheckInterval: 15s instance-id: user-${server.port} service-name: user server: port: 8005 management: security: enabled: false
暴露接口
@SpringBootApplication @RestController @EnableDiscoveryClient public class GatewayUserApplication { public static void main(String[] args) { SpringApplication.run(GatewayUserApplication.class, args); } @GetMapping("/test") public String test() { return "ok"; } }
暴露/test接口,返回ok即可。
網(wǎng)關(guān)服務(wù)
網(wǎng)關(guān)服務(wù)提供多種路由配置、路由斷言工廠和過濾器工廠等功能。
依賴
需要引入的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency> //依賴于webflux,必須引入 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gateway-core</artifactId> </dependency> //服務(wù)發(fā)現(xiàn)組件,排除web依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> <version>2.0.0.M6</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </exclusion> </exclusions> </dependency> //kotlin依賴 <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> <optional>true</optional> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> <version>${kotlin.version}</version> <optional>true</optional> </dependency>
如上引入了kotlin相關(guān)的依賴,這里需要支持kotlin的路由配置。Spring Cloud Gateway的使用需要排除web相關(guān)的配置,引入的是webflux的引用,應(yīng)用啟動時會檢查,必須引入。
路由斷言工廠
路由斷言工廠有多種類型,根據(jù)請求的時間、host、路徑、方法等等。如下定義的是一個基于路徑的路由斷言匹配。
@Bean public RouterFunction<ServerResponse> testFunRouterFunction() { RouterFunction<ServerResponse> route = RouterFunctions.route( RequestPredicates.path("/testfun"), request -> ServerResponse.ok().body(BodyInserters.fromObject("hello"))); return route; }
當請求的路徑為/testfun時,直接返回ok的狀態(tài)碼,且響應(yīng)體為hello字符串。
過濾器工廠
網(wǎng)關(guān)經(jīng)常需要對路由請求進行過濾,進行一些操作,如鑒權(quán)之后構(gòu)造頭部之類的,過濾的種類很多,如增加請求頭、增加請求參數(shù)、增加響應(yīng)頭和斷路器等等功能。
@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) { //@formatter:off return builder.routes() .route(r -> r.path("/image/webp") .filters(f -> f.addResponseHeader("X-AnotherHeader", "baz")) .uri("http://httpbin.org:80") ) .build(); //@formatter:on }
如上實現(xiàn)了當請求路徑為/image/webp時,將請求轉(zhuǎn)發(fā)到http://httpbin.org:80,并對響應(yīng)進行過濾處理,增加響應(yīng)的頭部X-AnotherHeader: baz。
自定義路由
上面兩小節(jié)屬于API自定義路由,還可以通過配置進行定義:
spring: cloud: gateway: locator: enabled: true default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar routes: # ===================================== - id: default_path_to_http uri: blueskykong.com order: 10000 predicates: - Path=/**
如上的配置定義了路由與過濾器。全局過濾器將所有的響應(yīng)加上頭部X-Response-Default-Foo: Default-Bar。定義了id為default_path_to_http的路由,只是優(yōu)先級比較低,當該請求都不能匹配時,將會轉(zhuǎn)發(fā)到blueskykong.com。
kotlin自定義路由
Spring Cloud Gateway可以使用kotlin自定義路由:
@Configuration class AdditionalRoutes { @Bean fun additionalRouteLocator(builder: RouteLocatorBuilder): RouteLocator = builder.routes { route(id = "test-kotlin") { path("/image/png") filters { addResponseHeader("X-TestHeader", "foobar") } uri("http://httpbin.org:80") } } }
當請求的路徑是/image/png,將會轉(zhuǎn)發(fā)到http://httpbin.org:80,并設(shè)置了過濾器,在其響應(yīng)頭中加上了X-TestHeader: foobar頭部。
服務(wù)發(fā)現(xiàn)組件
與服務(wù)注冊于發(fā)現(xiàn)組件進行結(jié)合,通過serviceId轉(zhuǎn)發(fā)到具體的服務(wù)實例。在前面的配置已經(jīng)引入了相應(yīng)的依賴。
@Bean public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient) { return new DiscoveryClientRouteDefinitionLocator(discoveryClient); }
將DiscoveryClient注入到DiscoveryClientRouteDefinitionLocator的構(gòu)造函數(shù)中,關(guān)于該路由定義定位器,后面的源碼分析會講解,此處不展開。
spring: cloud: gateway: locator: enabled: true default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar routes: # ===================================== - id: service_to_user uri: lb://user order: 8000 predicates: - Path=/user/** filters: - StripPrefix=1
上面的配置開啟了DiscoveryClient定位器的實現(xiàn)。路由定義了,所有請求路徑以/user開頭的請求,都將會轉(zhuǎn)發(fā)到user服務(wù),并應(yīng)用路徑的過濾器,截取掉路徑的第一部分前綴。即訪問/user/test的實際請求轉(zhuǎn)換成了lb://user/test。
websocket
還可以配置websocket的網(wǎng)關(guān)路由:
spring: cloud: gateway: default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar routes: - id: websocket_test uri: ws://localhost:9000 order: 9000 predicates: - Path=/echo
啟動一個ws服務(wù)端wscat --listen 9000,將網(wǎng)關(guān)啟動(網(wǎng)關(guān)端口為9090),進行客戶端連接即可wscat --connect ws://localhost:9090/echo。
客戶端的訪問
上述實現(xiàn)的功能,讀者可以自行下載源碼進行嘗試。筆者這里只展示訪問用戶服務(wù)的結(jié)果:
網(wǎng)關(guān)成功負載均衡到user-server,并返回了ok。響應(yīng)的頭部中包含了全局過濾器設(shè)置的頭部X-Response-Default-Foo: Default-Bar
感謝各位的閱讀!關(guān)于“Spring Cloud Gateway怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
文章標題:SpringCloudGateway怎么用-創(chuàng)新互聯(lián)
標題URL:http://aaarwkj.com/article18/codhdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、電子商務(wù)、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、網(wǎng)站策劃、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容