dubbo中DubboHealthIndicator的作用是什么
这期内容当中小编将会给大家带来有关dubbo中DubboHealthIndicator的作用是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联公司主营宾县网站建设的网络公司,主营网站建设方案,成都APP应用开发,宾县h5小程序制作搭建,宾县网站营销推广欢迎宾县等地区企业咨询
DubboHealthIndicator
dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/actuator/src/main/java/org/apache/dubbo/spring/boot/actuate/health/DubboHealthIndicator.java
public class DubboHealthIndicator extends AbstractHealthIndicator { @Autowired private DubboHealthIndicatorProperties dubboHealthIndicatorProperties; @Autowired(required = false) private MapprotocolConfigs = Collections.emptyMap(); @Autowired(required = false) private Map providerConfigs = Collections.emptyMap(); @Override protected void doHealthCheck(Health.Builder builder) throws Exception { ExtensionLoader extensionLoader = getExtensionLoader(StatusChecker.class); Map statusCheckerNamesMap = resolveStatusCheckerNamesMap(); boolean hasError = false; boolean hasUnknown = false; // Up first builder.up(); for (Map.Entry entry : statusCheckerNamesMap.entrySet()) { String statusCheckerName = entry.getKey(); String source = entry.getValue(); StatusChecker checker = extensionLoader.getExtension(statusCheckerName); org.apache.dubbo.common.status.Status status = checker.check(); org.apache.dubbo.common.status.Status.Level level = status.getLevel(); if (!hasError && level.equals(org.apache.dubbo.common.status.Status.Level.ERROR)) { hasError = true; builder.down(); } if (!hasError && !hasUnknown && level.equals(org.apache.dubbo.common.status.Status.Level.UNKNOWN)) { hasUnknown = true; builder.unknown(); } Map detail = new LinkedHashMap<>(); detail.put("source", source); detail.put("status", status); builder.withDetail(statusCheckerName, detail); } } /** * Resolves the map of {@link StatusChecker}'s name and its' source. * * @return non-null {@link Map} */ protected Map resolveStatusCheckerNamesMap() { Map statusCheckerNamesMap = new LinkedHashMap<>(); statusCheckerNamesMap.putAll(resolveStatusCheckerNamesMapFromDubboHealthIndicatorProperties()); statusCheckerNamesMap.putAll(resolveStatusCheckerNamesMapFromProtocolConfigs()); statusCheckerNamesMap.putAll(resolveStatusCheckerNamesMapFromProviderConfig()); return statusCheckerNamesMap; } private Map resolveStatusCheckerNamesMapFromDubboHealthIndicatorProperties() { DubboHealthIndicatorProperties.Status status = dubboHealthIndicatorProperties.getStatus(); Map statusCheckerNamesMap = new LinkedHashMap<>(); for (String statusName : status.getDefaults()) { statusCheckerNamesMap.put(statusName, DubboHealthIndicatorProperties.PREFIX + ".status.defaults"); } for (String statusName : status.getExtras()) { statusCheckerNamesMap.put(statusName, DubboHealthIndicatorProperties.PREFIX + ".status.extras"); } return statusCheckerNamesMap; } private Map resolveStatusCheckerNamesMapFromProtocolConfigs() { Map statusCheckerNamesMap = new LinkedHashMap<>(); for (Map.Entry entry : protocolConfigs.entrySet()) { String beanName = entry.getKey(); ProtocolConfig protocolConfig = entry.getValue(); Set statusCheckerNames = getStatusCheckerNames(protocolConfig); for (String statusCheckerName : statusCheckerNames) { String source = buildSource(beanName, protocolConfig); statusCheckerNamesMap.put(statusCheckerName, source); } } return statusCheckerNamesMap; } private Map resolveStatusCheckerNamesMapFromProviderConfig() { Map statusCheckerNamesMap = new LinkedHashMap<>(); for (Map.Entry entry : providerConfigs.entrySet()) { String beanName = entry.getKey(); ProviderConfig providerConfig = entry.getValue(); Set statusCheckerNames = getStatusCheckerNames(providerConfig); for (String statusCheckerName : statusCheckerNames) { String source = buildSource(beanName, providerConfig); statusCheckerNamesMap.put(statusCheckerName, source); } } return statusCheckerNamesMap; } private Set getStatusCheckerNames(ProtocolConfig protocolConfig) { String status = protocolConfig.getStatus(); return StringUtils.commaDelimitedListToSet(status); } private Set getStatusCheckerNames(ProviderConfig providerConfig) { String status = providerConfig.getStatus(); return StringUtils.commaDelimitedListToSet(status); } private String buildSource(String beanName, Object bean) { return beanName + "@" + bean.getClass().getSimpleName() + ".getStatus()"; } }
DubboHealthIndicator继承了AbstractHealthIndicator,这里注入了dubboHealthIndicatorProperties、protocolConfigs、providerConfigs
doHealthCheck方法会先获取StatusChecker的ExtensionLoader,然后通过resolveStatusCheckerNamesMap方法获取statusCheckerNamesMap,之后遍历该map根据statusCheckerName获取checker,然后执行check获取status及level,构建detail
resolveStatusCheckerNamesMap方法分别通过resolveStatusCheckerNamesMapFromDubboHealthIndicatorProperties、resolveStatusCheckerNamesMapFromProtocolConfigs、resolveStatusCheckerNamesMapFromProviderConfig来组装statusCheckerNamesMap
DubboHealthIndicatorTest
dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/actuator/src/test/java/org/apache/dubbo/spring/boot/actuate/health/DubboHealthIndicatorTest.java
@RunWith(SpringRunner.class) @TestPropertySource(properties = { "dubbo.protocol.id = dubbo-protocol", "dubbo.protocol.name = dubbo", "dubbo.protocol.port = 12345", "dubbo.protocol.status = registry", "dubbo.provider.id = dubbo-provider", "dubbo.provider.status = server", "management.health.dubbo.status.defaults = memory", "management.health.dubbo.status.extras = load,threadpool" }) @SpringBootTest( classes = { DubboHealthIndicator.class, DubboHealthIndicatorTest.class } ) @EnableConfigurationProperties(DubboHealthIndicatorProperties.class) @EnableDubboConfig public class DubboHealthIndicatorTest { @Autowired private DubboHealthIndicator dubboHealthIndicator; @Test public void testResolveStatusCheckerNamesMap() { MapstatusCheckerNamesMap = dubboHealthIndicator.resolveStatusCheckerNamesMap(); Assert.assertEquals(5, statusCheckerNamesMap.size()); Assert.assertEquals("dubbo-protocol@ProtocolConfig.getStatus()", statusCheckerNamesMap.get("registry")); Assert.assertEquals("dubbo-provider@ProviderConfig.getStatus()", statusCheckerNamesMap.get("server")); Assert.assertEquals("management.health.dubbo.status.defaults", statusCheckerNamesMap.get("memory")); Assert.assertEquals("management.health.dubbo.status.extras", statusCheckerNamesMap.get("load")); Assert.assertEquals("management.health.dubbo.status.extras", statusCheckerNamesMap.get("threadpool")); } @Test public void testHealth() { Health health = dubboHealthIndicator.health(); Assert.assertEquals(Status.UNKNOWN, health.getStatus()); } }
DubboHealthIndicatorTest验证了resolveStatusCheckerNamesMap及dubboHealthIndicator.health()方法
DubboHealthIndicatorProperties
dubbo-spring-boot-project-2.7.3/dubbo-spring-boot-compatible/actuator/src/main/java/org/apache/dubbo/spring/boot/actuate/health/DubboHealthIndicatorProperties.java
@ConfigurationProperties(prefix = PREFIX, ignoreUnknownFields = false) public class DubboHealthIndicatorProperties { /** * The prefix of {@link DubboHealthIndicatorProperties} */ public static final String PREFIX = "management.health.dubbo"; private Status status = new Status(); public Status getStatus() { return status; } public void setStatus(Status status) { this.status = status; } /** * The nested class for {@link StatusChecker}'s names ** registry= org.apache.dubbo.registry.status.RegistryStatusChecker * spring= org.apache.dubbo.config.spring.status.SpringStatusChecker * datasource= org.apache.dubbo.config.spring.status.DataSourceStatusChecker * memory= org.apache.dubbo.common.status.support.MemoryStatusChecker * load= org.apache.dubbo.common.status.support.LoadStatusChecker * server= org.apache.dubbo.rpc.protocol.dubbo.status.ServerStatusChecker * threadpool= org.apache.dubbo.rpc.protocol.dubbo.status.ThreadPoolStatusChecker ** * @see StatusChecker */ public static class Status { /** * The defaults names of {@link StatusChecker} ** The defaults : "memory", "load" */ private Set
defaults = new LinkedHashSet<>(Arrays.asList("memory", "load")); /** * The extra names of {@link StatusChecker} */ private Set extras = new LinkedHashSet<>(); public Set getDefaults() { return defaults; } public void setDefaults(Set defaults) { this.defaults = defaults; } public Set getExtras() { return extras; } public void setExtras(Set extras) { this.extras = extras; } } }
DubboHealthIndicatorProperties定义了Status,其定义了defaults、extras字段
小结
DubboHealthIndicator继承了AbstractHealthIndicator,这里注入了dubboHealthIndicatorProperties、protocolConfigs、providerConfigs
doHealthCheck方法会先获取StatusChecker的ExtensionLoader,然后通过resolveStatusCheckerNamesMap方法获取statusCheckerNamesMap,之后遍历该map根据statusCheckerName获取checker,然后执行check获取status及level,构建detail
resolveStatusCheckerNamesMap方法分别通过resolveStatusCheckerNamesMapFromDubboHealthIndicatorProperties、resolveStatusCheckerNamesMapFromProtocolConfigs、resolveStatusCheckerNamesMapFromProviderConfig来组装statusCheckerNamesMap
上述就是小编为大家分享的dubbo中DubboHealthIndicator的作用是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网站名称:dubbo中DubboHealthIndicator的作用是什么
转载来于:http://scyanting.com/article/iijoso.html