Spring cloud gateway工作流程原理解析
spring cloud gateway的包結(jié)構(gòu)(在Idea 2019.3中展示)
這個(gè)包是spring-cloud-gateway-core.這里是真正的spring-gateway的實(shí)現(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這個(gè)jar包.
其中actuate中定義了GatewayControllerEndpoint,它提供了對(duì)外訪問的接口.
// 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); #該類包括三個(gè)屬性,路由列表,默認(rèn)過濾器列表和MediaType列表.路由列表中的路由定義RouteDefinition. 過濾器中定義的FilterDefinition.
discovery定義了注冊(cè)中心的一些操作.
event定義了一系列事件,都繼承自ApplicationEvent.
filter定義了spring gateway實(shí)現(xiàn)的一些過濾器,包括gatewayfilter,globalfilter.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Nacos實(shí)現(xiàn)Spring Cloud Gateway實(shí)現(xiàn)動(dòng)態(tài)路由的方法
- spring cloud gateway網(wǎng)關(guān)路由分配代碼實(shí)例解析
- spring cloud gateway整合sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流
- spring cloud gateway請(qǐng)求跨域問題解決方案
- 創(chuàng)建網(wǎng)關(guān)項(xiàng)目(Spring Cloud Gateway)過程詳解
- springboot2.0和springcloud Finchley版項(xiàng)目搭建(包含eureka,gateWay,F(xiàn)reign,Hystrix)
- Spring Cloud Gateway重試機(jī)制原理解析
相關(guān)文章
詳解Java線程池如何實(shí)現(xiàn)優(yōu)雅退出
這篇文章我們將從源碼角度深度解析線程池是如何優(yōu)雅的退出程序的,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)java線程池有一定幫助,需要的可以參考一下2022-07-07SpringBoot項(xiàng)目請(qǐng)求不中斷動(dòng)態(tài)更新代碼的實(shí)現(xiàn)
在開發(fā)中,有時(shí)候不停機(jī)動(dòng)態(tài)更新代碼熱部署是一項(xiàng)至關(guān)重要的功能,它可以在請(qǐng)求不中斷的情況下下更新代碼,這種方式不僅提高了開發(fā)效率,還能加速測(cè)試和調(diào)試過程,本文將詳細(xì)介紹如何在 Spring Boot 項(xiàng)目在Linux系統(tǒng)中實(shí)現(xiàn)熱部署,特別關(guān)注優(yōu)雅關(guān)閉功能的實(shí)現(xiàn)2024-09-09Java微信公眾平臺(tái)開發(fā)(3) 接收消息的分類及實(shí)體的創(chuàng)建
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第三步,接收消息的分類及實(shí)體的創(chuàng)建,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot整合MyBatis Plus實(shí)現(xiàn)基本CRUD與高級(jí)功能
Spring Boot是一款用于快速構(gòu)建Spring應(yīng)用程序的框架,而MyBatis Plus是MyBatis的增強(qiáng)工具,本文將詳細(xì)介紹如何在Spring Boot項(xiàng)目中整合MyBatis Plus,并展示其基本CRUD功能以及高級(jí)功能的實(shí)現(xiàn)方式,需要的朋友可以參考下2024-02-02SpringBoot測(cè)試時(shí)卡在Resolving Maven dependencies的問題
這篇文章主要介紹了SpringBoot測(cè)試時(shí)卡在Resolving Maven dependencies的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java AQS(AbstractQueuedSynchronizer)源碼解析
AbstractQueuedSynchronizer被稱為隊(duì)列同步器,簡(jiǎn)稱為大家熟知的AQS,這個(gè)類可以稱作concurrent包的基礎(chǔ)。本文將通過剖析源碼來看看AQS是如何工作的,感興趣的可以了解一下2023-02-02Java 基于TCP Socket 實(shí)現(xiàn)文件上傳
這篇文章主要介紹了Java 基于TCP Socket 實(shí)現(xiàn)文件上傳的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12fastjson對(duì)JSONObject中的指定字段重新賦值的實(shí)現(xiàn)
這篇文章主要介紹了fastjson對(duì)JSONObject中的指定字段重新賦值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11