In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you "sample Analysis of Zuul routing configuration in Spring Cloud", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "sample Analysis of Zuul routing configuration in Spring Cloud".
Zuul routing configuration
Routing configuration seems simple, but there are some rules that need to be explained. The section explains the routing configuration of Zuul based on the previously built cluster project.
Simple routing
Spring Cloud implements several filters in the "routing" phase of Zuul that determine how routing works. Among them, the most basic is SimpleHostRoutingFilter, after the filter is run, all HTTP requests will be forwarded to the "source service" (HTTP service), which is called simple routing in this book. The example in section 7.2 of this chapter actually uses a simple route for request forwarding. The following is the configuration of simple routing, using both path and url:
Zuul: routes: routeTest: path: / routeTest/163 url: http://www.163.com
For ease of configuration, path can be omitted, routeId is used as path by default, and path configuration is omitted in the following configuration:
Zuul: routes: route163: url: http://www.163.com
In fact, to trigger a simple route, the value of the configured url needs to start with the string "http:" or "https:".
Zuul: routes: noRoute163: url: www.163.com
SimpleHostRoutingFilter, a filter for simple routing, is forwarded using HttpClient. The filter converts the relevant data of HttpServletRequest (HTTP methods, parameters, request headers, etc.) into the request instance (HttpRequest) of HttpClient, and then forwards it using CloseableHttpClient.
In this process, in order to ensure the performance of forwarding, the connection pool function of HttpClient is used. Connection pooling is involved and needs to be configured. When using simple routing, you can configure the following two items to modify the properties of the HttpClient connection pool:
Zuul.host.maxTotalConnections: the maximum number of connections to the target host. The default value is 200. Configuring this item is equivalent to calling the setMaxTotal method of PoolingHttpClientConnectionManager.
Zuul.host.maxPerRouteConnections: the initial number of connections per host. The default is 20. Configuring this item is equivalent to calling the setDefaultMaxPerRoute method of PoolingHttpClientConnectionManager.
In addition to simple routing, jump routing is also supported. When the An address of the gateway is accessed externally, it will be forward to B address, and the filter for handling jump routing is SendForwardFilter. Next, do a simple test to add a controller to the gateway project (zuul-gateway), as shown in listing 7-5.
Listing 7-5:
Codes\ 07\ 03\ zuul-gateway\ src\ main\ java\ org\ crazyit\ cloud\ web\ SourceController.java
@ RestControllerpublic class SourceController {@ RequestMapping (value = "/ source/hello/ {name}", method = RequestMethod.GET) public String hello (@ PathVariable ("name") String name) {return "hello" + name;}}
The simplest "hello" service is provided in the controller, which is used as a "source service" and is configured for forwarding in application.yml. The configuration items are as follows:
Zuul: routes: helloRoute: path: / test/** url: forward:/source/hello
When the external access to the "/ test" address, will automatically forward to the "/ source/hello" address, open the browser, type: http://localhost:8080/test/anugs, you can see that the browser will return the string "hello angus", and the source service will be called. The implementation of hop routing is relatively simple, in fact, the forward method of RequestDispatcher is called to jump.
Ribbon routing
In 7.3.2, we have been exposed to Ribbon routing. When the gateway registers with the Eureka server as an Eureka client, the request can be forwarded to the services of the cluster by configuring serviceId. Ribbon route filters can be executed using the following configuration:
Zuul: routes: sale: path: / sale/** serviceId: zuul-sale-service
Similar to simple routes, serviceId can also be omitted. When omitted, routeId will be used as serviceId. The following configuration snippet has the same effect as the above configuration:
Zuul: routes: zuul-sale-service: path: / sale/**
It is important to note that if the url configuration item provided is not in a simple route format (which does not start with http: or htpps:) or a hop route format (forward:), then the Ribbon route filter will be executed to treat url as a serviceId, and the following configuration snippet will have the same effect as the previous configuration:
Zuul: routes: sale: path: / sale/** url: zuul-sale-service Custom routing rules
If the above routing configuration does not meet the actual needs, consider using custom routing rules. The implementation is relatively simple by creating a PatternServiceRouteMapper in the configuration class, as shown in listing 7-6.
Listing 7-6:codes\ 07\ 03\ zuul-gateway\ src\ main\ java\ org\ crazyit\ cloud\ MyConfig.java
Configurationpublic class MyConfig {@ Bean public PatternServiceRouteMapper patternServiceRouteMapper () {return new PatternServiceRouteMapper ("(zuul)-(?. +)-(service)", "${module} / * *");}}
For the PatternServiceRouteMapper instance created, the first parameter of the constructor is the regular expression of serviceId, and the second parameter is the path of the route. Requests to access "module/**" will be routed to the "zuul-module-service" microservice.
Further, in the above routing rules, if you want one or more services not to be routed, you can use the zuul.ignoredServices attribute, for example, based on listing 7-6, if you want to exclude the zuul-sale-service and zuul-book-service modules, you can configure "zuul.ignoredServices: zuul-sale-service, zuul-book-service".
In addition to the above-mentioned zuul.ignoredServices configuration that ignores routing, you can also use zuul.ignoredPatterns to set the URL that is not routed, as shown in the following configuration snippet:
Zuul: ignoredPatterns: / sale/noRoute routes: sale: path: / sale/** serviceId: zuul-sale-service
Requests to access the "/ sale" path are routed to "zuul-sale-service" for processing, with the exception of "/ sale/noRoute".
The above is all the contents of the article "sample Analysis of Zuul routing configuration in Spring Cloud". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.