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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to customize routing function in Spring Cloud". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to customize routing function in Spring Cloud".
Using API gateway, traffic ingress can be unified, multiple domain names of microservices can be shielded from clients, load balancing can be achieved, and unified authentication, interface access control and traffic control can be achieved.
As a gateway, it proxies all traffic and requires higher performance. Since the core functions of the gateway are routing and proxy back-end services, which mainly deal with request forwarding, if synchronous blocking is used, the longer the response time of the back-end interface is, the greater the impact on the performance of the gateway. Nginx as a leader in reverse proxy, there is no doubt about the performance of Kong gateway developed based on nginx. But as internal gateways, we need better customization, and choosing Java gateways is easier for us to control.
After choosing a programming language, we need to consider performance issues. However, the performance loss of the gateway is nothing more than I _ peg O blocking, and only pure asynchrony can achieve better performance, so we choose a gateway that is at least based on the reactive programming library that implements the Reactive Streams specification.
As the gateway in the spring cloud ecosystem, Spring cloud gateway is developed based on Spring Boot and Project Reactor. It not only realizes asynchronous non-blocking, but also integrates with other components in the spring cloud ecosystem more quickly, which is the reason why we choose Spring cloud gateway.
We develop the internal micro-service gateway based on Spring Cloud Gateway and combine with the registry to realize automatic service discovery routing.
When the project was recently deployed on the Kubernetes cluster of the test environment, it was found that the route failed. After debugging the source code, it is found that because there is no dependency on importing Ribbon, Gateway registers a non-load balancer route filter (NoLoadBalancerClientFilter). When the routing rule configuration starts with "lb://", the route filter directly responds to 503 (the target service is not reachable).
Gateway uses global filters to implement the routing function, which is called in the order of the sort values specified by the global filter, and each filter terminates the call to the request.
Gateway allows each global filter to override the route URL. By updating the rewritten URL to the requested attribute and passing it down, the downstream filter can re-modify the route URL, and the original route URL is obtained from the routing rule.
/ / get the route from ServerWebExchange#getAttribute urlURI url = exchange.getAttribute (GATEWAY_REQUEST_URL_ATTR)
Gateway uses a linked list to string the routing URL before each filter is replaced. If you want to get the change of the URL replaced by the target service URL process, you can get the linked list and traverse the linked list.
/ record urladdOriginalRequestUrl (exchange, url) before modification; / / public static void addOriginalRequestUrl (ServerWebExchange exchange, URI url) {exchange.getAttributes () .computeIfAbsent (GATEWAY_ORIGINAL_REQUEST_URL_ATTR, s-> new LinkedHashSet ()); LinkedHashSet uris = exchange .getRequiredAttribute (GATEWAY_ORIGINAL_REQUEST_URL_ATTR); uris.add (url);}
After the Gateway receives a request, the request is transmitted from the FilteringWebHandler to the global filter chain (GatewayFilterChain) and down the filter chain. After the request URL is replaced by the intermediate filter, the final request forwarding filter (ForwardRoutingFilter) sends the request to the DispatcherHandler to initiate the request to the target service and sends the result response to the caller.
After understanding the cause of routing failure and after trying to route starter packets that rely on Ribbon still failed, the author decided to implement the routing function and use the load balancing algorithm implemented by Ribbon to achieve load balancing in the gateway.
Because the project is a micro-service project developed based on Spring Cloud Kubernetes, using Kubernetes cloud native service as a "registry", the service does not need to register, it can read Service or Endpoints resources through kubernete API, and spring-cloud-kubernete-core provides API for reading Service or Endpoints resources, so it is lighter to implement its own routing function than relying on a bunch of insignificant jar packages.
Load balancing is secondary. If you do not need to implement load balancing at the gateway, you can change the service discovery mode to Service, and read the Cluster IP; of Service resources in Kubernetes. If you want to achieve load balancing at the gateway, you can change the service discovery mode to Pod and read the Endpoints resources of Kubernetes.
The self-implementing routing function is divided into the following steps:
1. Use the client interface provided by spring cloud kubernetes core to access kubernetes resources to regularly pull the service directory cache locally.
2. Self-implement load balancing algorithm, or use the load balancing algorithm (IRule) provided by Ribbon's ribbon-loadbalancer package. Each route selects a node from the service list according to the algorithm to call.
3. Customize the global filter (GlobalFilter) that implements the reason function, and specify that the filter ranks before the non-load balancer filter registered by Gateway, and before it completes the replacement of "lb://serviceId" with "http://ip:port"" in url.
Thank you for reading, the above is the content of "how to customize routing function in Spring Cloud". After the study of this article, I believe you have a deeper understanding of how to customize routing function in Spring Cloud, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.