当前位置:网站首页>Sentinel source code analysis part II - sentinel dashboard console startup and configuration
Sentinel source code analysis part II - sentinel dashboard console startup and configuration
2022-07-03 19:11:00 【Age people】
List of articles
| working principle
- The console address needs to be configured when the application starts , Make the application interact with the console
- The application is responsible for registering with the console
- The console is responsible for configuring rule information
- Applications pull through listening and other methods rule To local , Wait for the flow to reach and limit the flow according to the configuration
|| push How mode works
Console configuration rules
Application listening rules
||| Source code analysis
A Console
- The console module is a lightweight springboot modular
- User pass FlowControllerV2 Configure rules and push zk Wait for the configuration center
Console configuration
- Modify by type DynamicRuleProvider and DynamicRulePublisher by zk,nacos etc.
@RestController
@RequestMapping(value = "/v2/flow")
public class FlowControllerV2 {
private final Logger logger = LoggerFactory.getLogger(FlowControllerV2.class);
@Autowired
private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;
@Autowired
@Qualifier("flowRuleDefaultProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
}
Console push
apiAddFlowRule
- Persistent in memory
- push zk
@PostMapping("/rule")
@AuthAction(value = AuthService.PrivilegeType.WRITE_RULE)
public Result<FlowRuleEntity> apiAddFlowRule(@RequestBody FlowRuleEntity entity) {
Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
if (checkResult != null) {
return checkResult;
}
entity.setId(null);
Date date = new Date();
entity.setGmtCreate(date);
entity.setGmtModified(date);
entity.setLimitApp(entity.getLimitApp().trim());
entity.setResource(entity.getResource().trim());
try {
Persistent in memory
entity = repository.save(entity);
push zk
publishRules(entity.getApp());
} catch (Throwable throwable) {
logger.error("Failed to add flow rule", throwable);
return Result.ofThrowable(-1, throwable);
}
return Result.ofSuccess(entity);
}
publishRules
- Get all the rules and push zk
private void publishRules(/*@NonNull*/ String app) throws Exception {
List<FlowRuleEntity> rules = repository.findAllByApp(app);
// FlowRuleApiPublisher
// FlowRuleApolloPublisher
// FlowRuleNacosPublisher
// FlowRuleZookeeperPublisher
rulePublisher.publish(app, rules);
}
B Client program
Client transformation
- Sentinel in the light of ZooKeeper The corresponding adaptation is made , The bottom layer can adopt ZooKeeper As a rule configuration data source . Just add the following dependencies when using :
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-zookeeper</artifactId>
<version>x.y.z</version>
</dependency>
- Then create ZookeeperDataSource And register it to the corresponding RuleManager You can go up.
// remoteAddress representative ZooKeeper Address of the server
// path Corresponding ZK Data path in
ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<>(remoteAddress, path, source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
}));
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
Client monitoring and updating
- adopt zklistener Listening rule change
- Full coverage update rules
public class ZookeeperDataSource<T> extends AbstractDataSource<String, T> {
...... Delete a lot of code
private void initZookeeperListener(final String serverAddr, final List<AuthInfo> authInfos) {
...... Delete a lot of code
this.listener = new NodeCacheListener() {
@Override
public void nodeChanged() {
zk change
try {
newValue That's the configuration rule Information Such as current limiting Fuse etc.
T newValue = loadConfig();
RecordLog.info(String.format("[ZookeeperDataSource] New property value received for (%s, %s): %s",
serverAddr, path, newValue));
Update when updating ruleManager Relevant rule information
Property Correspondence also exists with ruleManager in
getProperty().updateValue(newValue);
} catch (Exception ex) {
RecordLog.warn("[ZookeeperDataSource] loadConfig exception", ex);
}
}
};
}
}
Update implementation
getProperty().updateValue(newValue); Update implementation
- Property Sign up to FlowRuleManager
FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
- modify currentProperty Rule information is zk Of the monitor property
public class FlowRuleManager {
Current limiting rule information
private static SentinelProperty<List<FlowRule>> currentProperty = new DynamicSentinelProperty<List<FlowRule>>();
public static void register2Property(SentinelProperty<List<FlowRule>> property) {
AssertUtil.notNull(property, "property cannot be null");
synchronized (LISTENER) {
currentProperty.removeListener(LISTENER);
property.addListener(LISTENER);
modify currentProperty Rule information is zk Of the monitor property
currentProperty = property;
}
}
summary
- The generation environment generally adopts the registry to ensure the persistence of rules , Will not be lost due to downtime
- This article outlines push The realization of pattern ,sentinel also pull, Memory direct push mode
边栏推荐
- math_ Taylor formula
- 我们做了一个智能零售结算平台
- VLAN experiment
- Php based campus lost and found platform (automatic matching push)
- Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
- This Chinese numpy quick look-up table is too easy!
- Streaming media server (16) -- figure out the difference between live broadcast and on-demand
- I didn't cancel
- Foundation of ActiveMQ
- Le changement est un thème éternel
猜你喜欢
How to read the source code [debug and observe the source code]
EGO Planner代码解析bspline_optimizer部分(2)
SQL custom collation
EGO Planner代码解析bspline_optimizer部分(3)
The installation path cannot be selected when installing MySQL 8.0.23
Smart wax therapy machine based on STM32 and smart cloud
Pan for in-depth understanding of the attention mechanism in CV
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
[optics] vortex generation based on MATLAB [including Matlab source code 1927]
Record: MySQL changes the time zone
随机推荐
FBI warning: some people use AI to disguise themselves as others for remote interview
Pan for in-depth understanding of the attention mechanism in CV
硬盘监控和分析工具:Smartctl
01. Preparation for automated office (free guidance, only three steps)
Record: writing MySQL commands
HOW TO WRITE A DAILY LAB NOTE?
EGO Planner代码解析bspline_optimizer部分(2)
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
Record: install MySQL on ubuntu18.04
These problems should be paid attention to in the production of enterprise promotional videos
Simulation scheduling problem of SystemVerilog (1)
EGO Planner代码解析bspline_optimizer部分(3)
“google is not defined” when using Google Maps V3 in Firefox remotely
Common PostgreSQL commands
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
This Chinese numpy quick look-up table is too easy!
Record: pymysql is used in pycharm to connect to the database
User identity used by startup script and login script in group policy