In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of a sample analysis of SpringCloudGateway development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Routing introduction:
1.SpringCloudGateWay is used to replace zuul as the API gateway. There are three important nouns in gateway: filter, assertion, routing.
Filters and assertions are part of routing, which is the process of distributing requests to various services after a series of processing.
Routing process: first, the assertion and the route will be loaded. After receiving the request, it will match to the first loaded assertion according to the order in which the assertion is loaded. Only the request that matches the assertion will enter the route. The service that does not match will treat the request as a normal access request.
2: how the route loads assertions:
There are four ways to load assertions, namely, configuration file, java encoding, database and registry
The first configuration file:
What is mainly introduced in the official document is the loading method of the configuration file.
Official address: https://cloud.spring.io/spring-cloud-gateway/reference/html/#gateway-starter
There are three elements of a general assertion: id,uri,predicate.
Id is the identity of the assertion, uri is the ip+ port, and predicate is the rule for assertion matching.
3: example:
Create a new springboot project and introduce springcloudgateway dependencies
Org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-gateway
Register three global filters in the startup class
@ SpringBootApplicationpublic class GateWayApplication {public static void main (String [] args) {SpringApplication.run (GateWayApplication.class, args);} @ Bean @ Order (- 1) public GlobalFilter a () {return (exchange, chain)-> {return chain.filter (exchange) .then (Mono.fromRunnable (()-> {System.out.println (- 1);});} @ Bean @ Order (0) public GlobalFilter b () {return (exchange, chain)-> {return chain.filter (exchange) .then (Mono.fromRunnable (()-> {System.out.println (0);}));} @ Bean @ Order (1) public GlobalFilter c () {return (exchange, chain)-> {return chain.filter (exchange) .then (Mono.fromRunnable (()-> {System.out.println (1);}));};}}
Configure two routes in the profile class
Server.port: 7777spring: application: name: gateway cloud: gateway: discovery: locator: enabled: true lower-case-service-id: true routes:-id: method_route uri: http://127.0.0.1:9999 predicates:-Method=GET-id: method_route uri: http://127.0.0.1:8006 predicates:-Method=GET
When a request is sent, the first route is matched when the request arrives, so it can be known that the order of route matching will be based on the order in which it is loaded.
4:SpringCloudGateWay gets a route from the registry
In the official document, we can see a paragraph like this.
Configuring Predicates and Filters For DiscoveryClient Routes
By default the Gateway defines a single predicate and filter for routes created via a DiscoveryClient.
The default predicate is a path predicate defined with the pattern / serviceId/**, where serviceId is the id of the service from the DiscoveryClient.
The default filter is rewrite path filter with the regex / serviceId/ (?. *) and the replacement / ${remaining}. This just strips the service id from the path before the request is sent downstream.
If you would like to customize the predicates and/or filters used by the DiscoveryClient routes you can do so by setting spring.cloud.gateway.discovery.locator.predicates [x] and spring.cloud.gateway.discovery.locator.filters [y]. When doing so you need to make sure to include the default predicate and filter above, if you want to retain that functionality. Below is an example of what this looks like.
Address: https://cloud.spring.io/spring-cloud-gateway/reference/html/#_global_filters
Spring.cloud.gateway.discovery.locator.predicates [0] .name: Pathspring.cloud.gateway.discovery.locator.predicates [0] .args [pattern]: "'/'+ serviceId+'/**'" spring.cloud.gateway.discovery.locator.predicates [1] .name: Hostspring.cloud.gateway.discovery.locator.predicates [1] .args [pattern]: "'* * .foo.com'" spring.cloud.gateway.discovery.locator.filters [0] .name: Hystrixspring.cloud.gateway. Discovery.locator.filters [0] .args [name]: serviceIdspring.cloud.gateway.discovery.locator.filters [1] .name: RewritePathspring.cloud.gateway.discovery.locator.filters [1] .args [regexp]: "'/'+ serviceId +'/ (?. *)'" spring.cloud.gateway.discovery.locator.filters [1] .args [replacement]: "/ ${remaining}'"
According to the documentation, the configuration of assertions and filters can be obtained from the registry in this way
5:SpringGateWay configures routing from the database
Public class DBRouteDefinitionRepository implements RouteDefinitionRepository
After RouteDefinitionRepository is implemented in the project, springgateway will use the class you implemented to load the route. If not, it will load the route by default.
Public class DBRouteDefinitionRepository implements RouteDefinitionRepository {/ / Save route private final Map routes = synchronizedMap (new LinkedHashMap ()); private Logger log = LoggerFactory.getLogger (DBRouteDefinitionRepository.class); / / initial standard private boolean init_flag = true; / / private final GatewayProperties properties; private DynamicRouteServiceImpl service; public DBRouteDefinitionRepository (GatewayProperties properties) {this.properties = properties; this.service = new DynamicRouteServiceImpl () } @ Override public Flux getRouteDefinitions () {if (init_flag) {List routeDefinitions = new ArrayList (); List rs = new ArrayList (); try {routeDefinitions = service.quertAllRoutes (); / / load route rs = this.properties.getRoutes () from the database; / / get the route for (RouteDefinition rse: rs) {routeDefinitions.add (rse) of the configuration file } routes.clear (); routeDefinitions.forEach (x-> routes.put (x.getId (), x)); init_flag=false;} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace (); log.error ("Init Route Fail,Can't get Routes.", e);} return Flux.fromIterable (routeDefinitions) } else {return Flux.fromIterable (routes.values ());} @ Override public Mono delete (Mono routeId) {return routeId.flatMap (id-> {if (routes.containsKey (id)) {routes.remove (id); return Mono.empty ();} return Mono.defer (()-> Mono.error (new NotFoundException ("RouteDefinition not found:" + routeId));}) } @ Override public Mono save (Mono route) {return route.flatMap (r-> {routes.put (r.getId (), r); return Mono.empty ();});}}
This is my own implementation of the class, this class can obtain the routing configuration from the database and configuration files, and the routing configuration from the database can be obtained according to individual requirements
@ Validatedpublic class RouteDefinition {@ NotEmpty private String id = UUID.randomUUID (). ToString (); @ NotEmpty @ Valid private List predicates = new ArrayList (); @ Valid private List filters = new ArrayList (); @ NotNull private URI uri; private int order = 0; public RouteDefinition () {} public RouteDefinition (String text) {int eqIdx = text.indexOf ('='); if (eqIdx
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.