Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of DubboHealthIndicator in dubbo

Shulou Source: shulou.com Published: 2022-06-02 10:27:27 09月24日 Update

In this issue, the editor will bring you about the role of DubboHealthIndicator in dubbo. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

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 Map protocolConfigs = 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 inherits AbstractHealthIndicator, where dubboHealthIndicatorProperties, protocolConfigs and providerConfigs are injected.

The doHealthCheck method first gets the ExtensionLoader of the StatusChecker, then obtains the statusCheckerNamesMap through the resolveStatusCheckerNamesMap method, then traverses the map to get the checker according to the statusCheckerName, then executes check to get the status and level, and builds the detail

The resolveStatusCheckerNamesMap method assembles statusCheckerNamesMap through resolveStatusCheckerNamesMapFromDubboHealthIndicatorProperties, resolveStatusCheckerNamesMapFromProtocolConfigs and resolveStatusCheckerNamesMapFromProviderConfig, respectively.

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) @ EnableDubboConfigpublic class DubboHealthIndicatorTest {@ Autowired private DubboHealthIndicator dubboHealthIndicator Test public void testResolveStatusCheckerNamesMap () {Map statusCheckerNamesMap = 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 validates the resolveStatusCheckerNamesMap and dubboHealthIndicator.health () methods

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.accoun.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 defines Status, which defines defaults and extras fields.

Summary

DubboHealthIndicator inherits AbstractHealthIndicator, where dubboHealthIndicatorProperties, protocolConfigs and providerConfigs are injected.

The doHealthCheck method first gets the ExtensionLoader of the StatusChecker, then obtains the statusCheckerNamesMap through the resolveStatusCheckerNamesMap method, then traverses the map to get the checker according to the statusCheckerName, then executes check to get the status and level, and builds the detail

The resolveStatusCheckerNamesMap method assembles statusCheckerNamesMap through resolveStatusCheckerNamesMapFromDubboHealthIndicatorProperties, resolveStatusCheckerNamesMapFromProtocolConfigs and resolveStatusCheckerNamesMapFromProviderConfig, respectively.

The above is what the role of DubboHealthIndicator in dubbo is shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Methods functions content analysis specialties small and medium-sized rich in content fields summary that is articles more knowledge articles industries perspectives information channels channels related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple macOS Redmi Shulou Technology