Spring cloud gateway工作流程原理解析
spring cloud gateway的包結(jié)構(gòu)(在Idea 2019.3中展示)
這個(gè)包是spring-cloud-gateway-core.這里是真正的spring-gateway的實(shí)現(xiàn)的地方.
為了證明,我們打開(kāi)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ì)外訪問(wèn)的接口.
// 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) { //...... }//以下方法是繼承于父類(lèi),抽象類(lèi)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')@Validatedpublic 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); #該類(lèi)包括三個(gè)屬性,路由列表,默認(rèn)過(guò)濾器列表和MediaType列表.路由列表中的路由定義RouteDefinition. 過(guò)濾器中定義的FilterDefinition.
discovery定義了注冊(cè)中心的一些操作.
event定義了一系列事件,都繼承自ApplicationEvent.
filter定義了spring gateway實(shí)現(xiàn)的一些過(guò)濾器,包括gatewayfilter,globalfilter.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 小區(qū)后臺(tái)管理系統(tǒng)項(xiàng)目前端html頁(yè)面模板實(shí)現(xiàn)示例2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)3. asp讀取xml文件和記數(shù)4. 前端從瀏覽器的渲染到性能優(yōu)化5. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息6. ASP新手必備的基礎(chǔ)知識(shí)7. PHP中file_get_contents設(shè)置header請(qǐng)求頭,curl傳輸選項(xiàng)參數(shù)詳解說(shuō)明8. asp知識(shí)整理筆記2(問(wèn)答模式)9. python—sys模塊之獲取參數(shù)的操作10. python opencv通過(guò)按鍵采集圖片源碼
