Spring cloud gateway工作流程原理解析
spring cloud gateway的包結(jié)構(gòu)(在Idea 2019.3中展示)
這個包是spring-cloud-gateway-core.這里是真正的spring-gateway的實現(xiàn)的地方.
為了證明,我們打開spring-cloud-starter-gateway的pom文件
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gateway-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies>
除了cloud-start,starter-webflux就是cloud-gateway-core.所以后面我們就分析
cloud-gateway-core這個jar包.
其中actuate中定義了GatewayControllerEndpoint,它提供了對外訪問的接口.
// TODO: Flush out routes without a definition @GetMapping("/routes") public Flux<Map<String, Object>> routes() { return this.routeLocator.getRoutes().map(this::serialize); } @GetMapping("/routes/{id}") public Mono<ResponseEntity<Map<String, Object>>> route(@PathVariable String id) { //...... } //以下方法是繼承于父類,抽象類AbstractGatewayControllerEndpoint @PostMapping("/refresh") public Mono<Void> refresh() { this.publisher.publishEvent(new RefreshRoutesEvent(this)); return Mono.empty(); } @GetMapping("/globalfilters") public Mono<HashMap<String, Object>> globalfilters() { return getNamesToOrders(this.globalFilters); } @GetMapping("/routefilters") public Mono<HashMap<String, Object>> routefilers() { return getNamesToOrders(this.GatewayFilters); } @GetMapping("/routepredicates") public Mono<HashMap<String, Object>> routepredicates() { return getNamesToOrders(this.routePredicates); } @PostMapping("/routes/{id}") @SuppressWarnings("unchecked") public Mono<ResponseEntity<Object>> save(@PathVariable String id, @RequestBody RouteDefinition route) {} @DeleteMapping("/routes/{id}") public Mono<ResponseEntity<Object>> delete(@PathVariable String id) { return this.routeDefinitionWriter.delete(Mono.just(id)) .then(Mono.defer(() -> Mono.just(ResponseEntity.ok().build()))) .onErrorResume(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build())); } @GetMapping("/routes/{id}/combinedfilters") public Mono<HashMap<String, Object>> combinedfilters(@PathVariable String id) { // TODO: missing global filters }
config包里定義了一些Autoconfiguration和一些properties.讀取配置文件就在這里完成.
我們這里看一下GatewayProperties.java
@ConfigurationProperties("spring.cloud.gateway") @Validated public class GatewayProperties { /** * List of Routes. */ @NotNull @Valid private List<RouteDefinition> routes = new ArrayList<>(); /** * List of filter definitions that are applied to every route. */ private List<FilterDefinition> defaultFilters = new ArrayList<>(); private List<MediaType> streamingMediaTypes = Arrays .asList(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_STREAM_JSON); #該類包括三個屬性,路由列表,默認(rèn)過濾器列表和MediaType列表.路由列表中的路由定義RouteDefinition. 過濾器中定義的FilterDefinition.
discovery定義了注冊中心的一些操作.
event定義了一系列事件,都繼承自ApplicationEvent.
filter定義了spring gateway實現(xiàn)的一些過濾器,包括gatewayfilter,globalfilter.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Nacos實現(xiàn)Spring Cloud Gateway實現(xiàn)動態(tài)路由的方法
- spring cloud gateway網(wǎng)關(guān)路由分配代碼實例解析
- spring cloud gateway整合sentinel實現(xiàn)網(wǎng)關(guān)限流
- spring cloud gateway請求跨域問題解決方案
- 創(chuàng)建網(wǎng)關(guān)項目(Spring Cloud Gateway)過程詳解
- springboot2.0和springcloud Finchley版項目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix)
- Spring Cloud Gateway重試機(jī)制原理解析
相關(guān)文章
SpringBoot項目請求不中斷動態(tài)更新代碼的實現(xiàn)
在開發(fā)中,有時候不停機(jī)動態(tài)更新代碼熱部署是一項至關(guān)重要的功能,它可以在請求不中斷的情況下下更新代碼,這種方式不僅提高了開發(fā)效率,還能加速測試和調(diào)試過程,本文將詳細(xì)介紹如何在 Spring Boot 項目在Linux系統(tǒng)中實現(xiàn)熱部署,特別關(guān)注優(yōu)雅關(guān)閉功能的實現(xiàn)2024-09-09Java微信公眾平臺開發(fā)(3) 接收消息的分類及實體的創(chuàng)建
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第三步,接收消息的分類及實體的創(chuàng)建,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot整合MyBatis Plus實現(xiàn)基本CRUD與高級功能
Spring Boot是一款用于快速構(gòu)建Spring應(yīng)用程序的框架,而MyBatis Plus是MyBatis的增強(qiáng)工具,本文將詳細(xì)介紹如何在Spring Boot項目中整合MyBatis Plus,并展示其基本CRUD功能以及高級功能的實現(xiàn)方式,需要的朋友可以參考下2024-02-02SpringBoot測試時卡在Resolving Maven dependencies的問題
這篇文章主要介紹了SpringBoot測試時卡在Resolving Maven dependencies的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Java AQS(AbstractQueuedSynchronizer)源碼解析
AbstractQueuedSynchronizer被稱為隊列同步器,簡稱為大家熟知的AQS,這個類可以稱作concurrent包的基礎(chǔ)。本文將通過剖析源碼來看看AQS是如何工作的,感興趣的可以了解一下2023-02-02Java 基于TCP Socket 實現(xiàn)文件上傳
這篇文章主要介紹了Java 基于TCP Socket 實現(xiàn)文件上傳的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12fastjson對JSONObject中的指定字段重新賦值的實現(xiàn)
這篇文章主要介紹了fastjson對JSONObject中的指定字段重新賦值的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11