Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of dynamic updating of Spring Cloud Zuul routing rules

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of dynamic update of Spring Cloud Zuul routing rules, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

Background

As the gateway of micro-service, Spring Cloud Zuul requests to be routed to internal service via zuul. Due to the need to add / modify / delete routing rules for services, the dynamic change function of zuul routing rules provides

You can update in real time without restarting the zuul gateway. The following methods are available:

1. Dynamic refresh based on refresh + config-server events

(1) after spring boot integrates the refresh function provided by spring actuator, a zuul routing rule is added to the git configuration repository of congfig-server.

(2) refresh the refresh endpoint http://127.0.0.1:8080/refresh in post (take the local machine as an example)

(3) visit zuul again and find that there are new rules in the routing rules.

Starting from triggering the refresh operation, the whole process of updating route in ZuulPropeties is as follows:

The routing rules of Zuul gateway load the core classes DiscoveryClientRouteLocator and SimpleRouteLocator. For more information, please see Spring Cloud Zuul source code. There is no analysis here.

The loading mechanism of routing rules is mainly through SimpleRouteLocator to load routing rules in ZuulPropetties. The figure above illustrates the whole process from refresh to attribute injection ZuulProperties.

Disadvantages:

Because the latest K _ ZuulPropeties V data of all zuul in the configuration center loaded after refresh is first loaded, and then injected into ZuulPropeties (Bean) according to the loaded attribute K _ ZuulPropeties V, while ZuulPropeties is stored in the Bean container before refresh

Therefore, add or modify Zuul routing rules. After refresh, the attribute values in ZuulPropeties will be added or overwritten, while the routing rules in delete operation ZuulPropeties still exist, so deletion is invalid.

Advantages:

Easy to use, versioning routing rules based on the configuration repository, simply exposing refresh endpoints to the outside.

Second, dynamic refresh based on RefreshScope

Expand ZuulPropeties and use RefreshScope annotations. After modifying the routing rules in the configuration center, the routing rules will be changed when refresh is triggered.

Code:

Bean @ ConfigurationProperties ("zuul") @ RefreshScope @ Primary public ZuulProperties zuulProperties () {return new ZuulProperties ();}

Disadvantages:

Need to customize the extension ZuulPropetties, add RefreshScope comments

Advantages:

Because RefreshScope uses cglib to generate ZuulPropetties proxies, which is different from the way in which Bean objects are implemented, deletion of routing rules can also take effect.

Third, dynamic refresh based on db storage

The routing rules of the Zuul gateway load the core classes DiscoveryClientRouteLocator and SimpleRouteLocator, but

The locateRoutes () method in the overload of SimpleRouteLocator is extended to implement custom loading routing rules from db. The trigger condition is still refresh.

Disadvantages:

The extension is complex and requires customized development and rewriting the loading logic of routing rules.

Advantages:

You can flexibly control the changes of routing rules and add more custom functions combined with business scenarios.

Thank you for reading this article carefully. I hope the article "sample Analysis of dynamic updates of Spring Cloud Zuul routing rules" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report