From d924184fec77736f5af7b7dc6381ed7745dfe318 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Thu, 25 Oct 2018 10:47:03 +0800 Subject: [PATCH] sentinel add logs to query datasource process. --- .../SentinelDataSourcePostProcessor.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java index 40f313700..73ece0ec4 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java @@ -195,30 +195,44 @@ public class SentinelDataSourcePostProcessor @EventListener(classes = ApplicationReadyEvent.class) public void appStartedListener(ApplicationReadyEvent event) throws Exception { + logger.info("[Sentinel Starter] Start to find ReadableDataSource"); Map dataSourceMap = event.getApplicationContext().getBeansOfType( ReadableDataSource.class); if (dataSourceMap.size() == 1) { + logger.info("[Sentinel Starter] There exists only one ReadableDataSource named {}, start to load rules", + dataSourceMap.keySet().iterator().next()); ReadableDataSource dataSource = dataSourceMap.values().iterator().next(); Object ruleConfig = dataSource.loadConfig(); SentinelProperty sentinelProperty = dataSource.getProperty(); - if (checkRuleType(ruleConfig, FlowRule.class)) { + Integer rulesNum; + if ((rulesNum = checkRuleType(ruleConfig, FlowRule.class)) > 0) { FlowRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} flow rules", rulesNum); } - if (checkRuleType(ruleConfig, DegradeRule.class)) { + if ((rulesNum = checkRuleType(ruleConfig, DegradeRule.class)) > 0) { DegradeRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} degrade rules", rulesNum); } - if (checkRuleType(ruleConfig, SystemRule.class)) { + if ((rulesNum = checkRuleType(ruleConfig, SystemRule.class)) > 0) { SystemRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} system rules", rulesNum); } - if (checkRuleType(ruleConfig, AuthorityRule.class)) { + if ((rulesNum = checkRuleType(ruleConfig, AuthorityRule.class)) > 0) { AuthorityRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} authority rules", rulesNum); } + } else if (dataSourceMap.size() > 1) { + logger.warn( + "[Sentinel Starter] There exists more than one ReadableDataSource, can not choose which one to load"); + } else { + logger.warn( + "[Sentinel Starter] No ReadableDataSource exists"); } } - private boolean checkRuleType(Object ruleConfig, Class type) { + private Integer checkRuleType(Object ruleConfig, Class type) { if (ruleConfig.getClass() == type) { - return true; + return 1; } else if (ruleConfig instanceof List) { List ruleList = (List)ruleConfig; List checkList = new ArrayList(); @@ -228,10 +242,10 @@ public class SentinelDataSourcePostProcessor } } if (ruleList.size() == checkList.size()) { - return true; + return ruleList.size(); } } - return false; + return -1; } class SentinelDataSourceField {