当前位置:网站首页>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
边栏推荐
- How to build an efficient information warehouse
- Verilog HDL continuous assignment statement, process assignment statement, process continuous assignment statement
- The most valuable thing
- [leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
- SQL injection for Web Security (1)
- 记录在模拟器中运行flutter时报的错
- Sqlalchemy - subquery in a where clause - Sqlalchemy - subquery in a where clause
- What does a really excellent CTO look like in my eyes
- Introduction to SSH Remote execution command
- leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
猜你喜欢

SQL injection for Web Security (1)

my. INI file not found

SQL custom collation

Flutter network and data storage framework construction-b1

235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】

Compose LazyColumn 顶部添加控件
![[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD](/img/51/209e35e0b94a51b3b406a184459475.png)
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
![[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult](/img/8d/0e515af6c17971ddf461e3f3b87c30.png)
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult

Summary of composition materials for 2020 high-frequency examination center of educational resources

Record the errors reported when running fluent in the simulator
随机推荐
Simulation scheduling problem of SystemVerilog (1)
math_泰勒公式
[disease identification] machine vision lung cancer detection system based on Matlab GUI [including Matlab source code 1922]
Analyse du Code du planificateur ego bspline Section Optimizer (1)
A green plug-in that allows you to stay focused, live and work hard
Ego planner code parsing Bspline_ Optimizer section (2)
Flutter网络和数据存储框架搭建 -b1
High concurrency Architecture - read write separation
VLAN experiment
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
shell 脚本中关于用户输入参数的处理
Dynamic planning -- expansion topics
Sustainable service business models
C enum contains value - C enum contains value
The installation path cannot be selected when installing MySQL 8.0.23
Record the errors reported when running fluent in the simulator
Analysis of dart JSON encoder and decoder
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
[leetcode] [SQL] notes