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

What is the design concept and transformation practice of Sentinel dynamic data source architecture?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about the design concept and transformation practice of Sentinel dynamic data source architecture, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Before introducing the cluster current limit, we need to master the configuration mode of the dynamic data source, put forward the overall architecture idea according to the official code provided by Sentinel, and finally give the practical guidance.

Warm Tip: this paper is mainly divided into three parts: the design concept of dynamic data source architecture, looking for transformation ideas from official examples, and the transformation scheme based on SpringBoot to analyze the transformation scheme of Sentienl dynamic data source in detail. Step by step, it not only solves the problem itself, but also reflects the ideas and methods of the author to study a problem.

1. The concept of architecture design

There are mainly the following roles in Sentinel: management background, current-limiting circuit breaker rule data source, application.

1) manage the backend

The management backend is mainly used to visually configure current limit rules and circuit breaker rules. The screenshots of the operation interface are as follows:

2) data source of current-limiting fuse rule

The data container used to store current-limiting circuit breaker rules corresponds to the concept of dynamic data source in Sentinel, which has two meanings:

Data container

A data container refers to a database that stores the configuration of rules such as circuit breakers and current limits, such as relational databases, Zookeeper, and so on. In the actual production process, you need to select a database that supports persistence, otherwise the configuration rules will be lost as soon as the program is restarted, which is obviously unacceptable.

Dynamic

The word "dynamic" mainly emphasizes that the changes of configuration rules can take effect dynamically and timely, and the applications that introduce Sentinel current-limiting SDK dynamically perceive that the configuration rules change and take effect immediately without restarting. Sentinel currently supports apollo, consul, etcd, nacos, redis, spring-clould-config, zookeeper and so on.

3) Application Program

In order to protect the application through the current-limiting and circuit-breaking functions provided by Sentinel, we need to refer to the SDK related to Sentinel and judge whether it conforms to the current current-limiting rules according to the collected call information.

The relationship between background management system, dynamic data source, and application is shown in the figure:

2. Find the idea of transformation from the official example.

It can be clearly learned from the official documents that sentinel-dashboard, the official backend management system, only supports storing current-limiting, circuit breaker and other current-limiting configuration rules in memory. Once the background management system is restarted, all configured circuit breaker rules will be lost, so it is necessary to carry out certain improvements to sentinel-dashboard in production practice, introducing dynamic data sources, such as Zookeeper, and persisting current-limiting configuration.

With the above architectural design concept provides a direction for our transformation, then how to specifically transform it? First of all, let's take a look at the official Demo program. The official sample code is shown in the following figure:

Next we will use zookeeper dynamic data source to introduce how to build Sentinel dynamic data source based on zookeeper.

2.1 regular storage such as current limiting and fusing

First, take a look at ZookeeperConfigSender. The main function of this class is to write the configuration to zookeeper. The key code screenshot is as follows:

The testing purpose of this class is very simple. First, the current limiting rules are persisted into Zookeeper and play the same role as sentinel-dashboard. Therefore, this class brings great inspiration for us to transform the background management system, that is, we can store sentinel current limiting rules through zookeeper. From the demo example, we can see the directory structure of current limiting rules in zookeeper, the path is / {groupId} / {dataid}, and the value value of this node stores the json string. Store all current restriction rules.

Practical guidance, usually based on zookeeper development, is mainly to plan the directory structure. With regard to Sentinel, I give a preliminary directory planning.

Create a root node in zookeeper, such as / sentienl, to represent the root directory related to current restriction.

GroupId is usually a separate application name, such as the appId of the application, such as provider-demo in the example.

DataId is usually a configuration type, such as current limiting rules, circuit breaker rules, hotspot rules and so on. For example, current limiting rules use / flowRule, circuit breakers use / degradeRule, and their value values are stored in json. All current limiting rules under this application are represented by a json object, and the storage format is similar to [{}, {}].

2.2 client dynamic awareness configuration

After realizing the configuration storage of the storage rules, the client is required to be able to dynamically perceive the changes of the rules, so that the configuration rules take effect in real time.

Let's first take a look at the official example, whose core code is shown in the figure:

Although the concept of groupId and dataId is introduced here to facilitate switching with nacos, even without switching, this kind of directory planning is very necessary based on zookeeper programming. The above sample code has two key points:

Create a ZookeeperDataSource, and each ZookeeperDataSource is responsible for listening on a node.

The register2Property method of FlowRuleManager needs to be called to register the data associated with the data source into FlowRuleManager, which makes it convenient for the Sentinel kernel to work according to the rules such as current-limiting circuit breakers stored in the data source.

When the client starts, it will call the FlowRuleManager-related methods to load the configuration related to current limit, so if the configuration rules are changed, how does the client dynamically perceive it? The key lies in the implementation of ZookeeperDataSource, and the key points are as follows:

That is, when building a ZookeeperDataSource, it will listen on the / groupId/dataId node, that is, the node that stores the current limit configuration. Once the data changes, it will notify the client, and then call loadConfig to update the current limit configuration of the Sentienl client, so that the configuration takes effect in real time. 3. Implementation scheme of dynamic data source

From the official example, it is not difficult to find that there are two main steps to introduce Zookeeper data sources: storing the data in Zookeeper and listening to ZK on the client side to take effect in real time.

Sentinel officially provides the default background management system implementation: sentinel-dashboard, but its disadvantage is very obvious: based on memory storage, can not be used in the actual production process. You may store the configuration information in memory from the background management system, so how does the connected client get the configuration information from the memory of sentinel-dashboard? this is because sentinel-dashboard provides simple machine discovery and built-in communication protocols between sentinel clients and between sentinel clients and sentinel-dashboard, specifically implemented by the sentinel-transport module, and currently provides an implementation method based on http and netty Therefore, the configuration information in sentinel-dashboard memory can be pushed to the client, so that the client can limit the current and circuit breaker according to the configuration.

Next, to answer the key part of this article, how to introduce dynamic data sources such as zookeeper based on sentinel-dashboard?

3.1Store configuration rules in Zookeeper

First of all, we can follow the controller provided by sentinel-dashboard to find its background entry, and the goal of the transformation is very clear, that is, to persist the data into zookeeper, for example, to add the background processing entry of traffic control rules to:

You just need to start the transformation here and persist its configuration to the database and zookeeper. The key to storing data in zookeeper is to design how projects are organized and organized in zookeeper. I give the following design scheme: in this way, relevant managers can directly configure current-limiting rules in sentinel-dashboard, that is, applications are stored as dimensions, and each application is configured according to dimensions, such as current limiting, circuit breaker, hot spot, cluster, and so on. The value of each classification node stores all configurations, using the JSON format of [{}, {}]. 3.2 Sentinel client rule loading encapsulation

At present, most of the projects are based on SpringBoot, so this paper gives the idea of client loading based on SpringBoot.

Using the event mechanism of SpringBoot, after the initialization of the Spring container, the configuration in zookeeper starts to be loaded. The implementation idea is to read all the child nodes under / sentinel in zookeeper, and then traverse their child nodes (appid), then read the configurations such as flow (current limit) and degrade (fuse), and call the relevant API of Sentinel to complete the loading. The pseudo code is as follows:

The main key points are as follows:

Based on the Spring ApplicationReadyEvent event, the loading of current limiting rules is realized.

Create ZookeeperDataSource create dynamic data source.

And call the relevant API provided by Sentinel to complete the loading of current limiting rules.

After reading the above, do you have any further understanding of the design concept and transformation practice of Sentinel dynamic data source architecture? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report