Self-praise: Dubbo Implementation Principles and Source Code Parsing — The Best Collection
Praise yourself: D Database Entity Design Collection
www.iocoder.cn/Spring-Clou… “Taro source” welcome to reprint, keep the summary, thank you!
This article is based on Spring-Cloud-Gateway 2.0.x M4
- 1. An overview of the
- 2. Filter HTTP API
- 2.1 Global Filter list
- 2.2 Routing Filter Factory List
- 3. Route HTTP API
- 2.1 Route List
- 2.2 Single Route Information
- 2.3 Filter of a Single route
- 2.4 Adding or Modifying a Route
- 2.5 Deleting a Route
- 2.6 Refreshing the Route Cache
- 666. The eggs
🙂🙂🙂 follow wechat public number:
- RocketMQ/MyCAT/Sharding-JDBC all source code analysis article list
- RocketMQ/MyCAT/Sharding-JDBC 中文 解 决 source GitHub address
- Any questions you may have about the source code will be answered carefully. Even do not know how to read the source can also ask oh.
- New source code parsing articles are notified in real time. It’s updated about once a week.
- Serious source communication wechat group.
1. An overview of the
This article focuses on sharing the gateway management HTTP API.
Org. Springframework. Cloud. Gateway. Actuate. GatewayWebfluxEndpoint, provide management gateway HTTP API. Constructor, code as follows:
@RestController @RequestMapping("${management.context-path:/application}/gateway") public class GatewayWebfluxEndpoint implements ApplicationEventPublisherAware { private static final Log log = LogFactory.getLog(GatewayWebfluxEndpoint.class); /** * Private RouteDefinitionLocator RouteDefinitionLocator; /** * private List<GlobalFilter> globalFilters; /** * private List<GatewayFilterFactory> gatewayFilters; /** * Memory RouteDefinitionLocator */ private RouteDefinitionWriter RouteDefinitionWriter; /** * private RouteLocator RouteLocator; / * * * application event publishing * / private ApplicationEventPublisher publisher; public GatewayWebfluxEndpoint(RouteDefinitionLocator routeDefinitionLocator, List<GlobalFilter> globalFilters, List<GatewayFilterFactory> GatewayFilters, RouteDefinitionWriter routeDefinitionWriter, RouteLocator routeLocator) { this.routeDefinitionLocator = routeDefinitionLocator; this.globalFilters = globalFilters; this.gatewayFilters = GatewayFilters; this.routeDefinitionWriter = routeDefinitionWriter; this.routeLocator = routeLocator; }}Copy the code
@RequestMapping
Annotation, HTTP API to"${management.context-path:/application}/gateway"
。routeDefinitionLocator
Attribute, route definition locator. inThe Spring – Cloud – Gateway source code parsing RouteDefinitionRouteLocator routing configuration of routing (2.2) -“Have detailed analysis.globalFilters
Property, global filter. inSpring-cloud-gateway source code parsing – (4.1) GatewayFilter overviewHave detailed analysis.gatewayFilters
Property, gateway filter factory. inThe GatewayFilterFactory filter factory (4.2)routeLocator
Property, route locator. inSpring-cloud-gateway: RouteLocator (2.1)Have detailed analysis.publisher
Property to apply the event publisher. inSpring5 – Events and Listeners in the Spring FrameworkThere is correlation analysis.
GatewayWebfluxEndpoint provides two classes of HTTP apis:
- Filter HTTP API
- Routing HTTP API
Recommended Spring Cloud books:
- Please support the legal version. Download piracy, is equal to the initiative to write low-level bugs.
- DD — Spring Cloud Micro Services
- Zhou Li — “Spring Cloud and Docker Micro-service Architecture Combat”
- Buy two books together, jingdong free delivery.
Spring Cloud
- Java Microservices Practices – Spring Boot
- Java Microservices Practices – Spring Cloud
- Java Microservices Practices – Spring Boot/Spring Cloud
2. Filter HTTP API
2.1 Global Filter list
@GetMapping("/globalfilters")
public Mono<HashMap<String, Object>> globalfilters() {
return getNamesToOrders(this.globalFilters);
}
private <T> Mono<HashMap<String, Object>> getNamesToOrders(List<T> list) {
return Flux.fromIterable(list).reduce(new HashMap<>(), this::putItem);
}
private HashMap<String, Object> putItem(HashMap<String, Object> map, Object o) {
Integer order = null;
if (o instanceof Ordered) {
order = ((Ordered)o).getOrder();
}
//filters.put(o.getClass().getName(), order);
map.put(o.toString(), order);
return map;
}
Copy the code
2.2 Routing Filter Factory List
@GetMapping("/routefilters")
public Mono<HashMap<String, Object>> routefilers() {
return getNamesToOrders(this.gatewayFilters);
}
Copy the code
3. Route HTTP API
2.1 Route List
@GetMapping("/routes")
public Mono<Map<String, List>> routes() {
Mono<List<RouteDefinition>> routeDefs = this.routeDefinitionLocator.getRouteDefinitions().collectList();
Mono<List<Route>> routes = this.routeLocator.getRoutes().collectList();
return Mono.zip(routeDefs, routes).map(tuple -> {
Map<String, List> allRoutes = new HashMap<>();
allRoutes.put("routeDefinitions", tuple.getT1());
allRoutes.put("routes", tuple.getT2());
return allRoutes;
});
}
Copy the code
2.2 Single Route Information
@GetMapping("/routes/{id}") public Mono<ResponseEntity<RouteDefinition>> route(@PathVariable String id) { //TODO: missing RouteLocator return this.routeDefinitionLocator.getRouteDefinitions() .filter(route -> route.getId().equals(id)) .singleOrEmpty() .map(route -> ResponseEntity.ok(route)) .switchIfEmpty(Mono.just(ResponseEntity.notFound().build())); }Copy the code
- from
TODO: missing RouteLocator
As you can see, getting a Route from a RouteLocator is not currently supported and only a RouteDefinition is returned. Wait for future version support.
2.3 Filter of a Single route
@GetMapping("/routes/{id}/combinedfilters")
public Mono<HashMap<String, Object>> combinedfilters(@PathVariable String id) {
//TODO: missing global filters
return this.routeLocator.getRoutes()
.filter(route -> route.getId().equals(id))
.reduce(new HashMap<>(), this::putItem);
}
Copy the code
- from
TODO: missing global filters
We can see that the filter currently returned does not include GlobalFilter and can be called/globalfilters
Look at it. Wait for future version support.
2.4 Adding or Modifying a Route
In the Spring — Cloud – Gateway source code parsing RouteDefinitionRepository memory of routing (1.3) – “” 5. GatewayWebfluxEndpoint” has a detailed analysis.
2.5 Deleting a Route
In the Spring — Cloud – Gateway source code parsing RouteDefinitionRepository memory of routing (1.3) – “” 5. GatewayWebfluxEndpoint” has a detailed analysis.
2.6 Refreshing the Route Cache
It is explained in detail in “5. CachingRouteLocator” in Spring-Cloud-Gateway Source Code Parsing — Routing (2.1) RouteLocator overview.
666. The eggs
Water more article, hahaha.
Fat friends, share a wave of friends can be good!