spring cloud gateway的包結構(在Idea 2019.3中展現)
這個包是spring-cloud-gateway-core.這裏是真正的spring-gateway的實現的地方.
爲了證實,咱們打開spring-cloud-starter-gateway的pom文件java
<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包.web
其中actuate中定義了GatewayControllerEndpoint,它提供了對外訪問的接口.spring
// 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.讀取配置文件就在這裏完成.
markdown
咱們這裏看一下GatewayProperties.javaapp
@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); #該類包括三個屬性,路由列表,默認過濾器列表和MediaType列表.路由列表中的路由定義RouteDefinition. 過濾器中定義的FilterDefinition.
discovery定義了註冊中心的一些操做.
event定義了一系列事件,都繼承自ApplicationEvent.
filter定義了spring gateway實現的一些過濾器,包括gatewayfilter,globalfilter.ide