com.github.yinfujing.dubbo.spring.boot.actuate.ZookeeperHealthIndicator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-boot-starter-dubbo Show documentation
Show all versions of spring-boot-starter-dubbo Show documentation
provide a simple spring-boot-stater for dubbo
The newest version!
package com.github.yinfujing.dubbo.spring.boot.actuate;
import com.alibaba.dubbo.config.RegistryConfig;
import com.github.yinfujing.dubbo.spring.boot.autoconfigure.register.RegistryConfigRegister;
import lombok.Getter;
import lombok.Setter;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkTimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Setter
@ConditionalOnClass({ZkClient.class})
public class ZookeeperHealthIndicator extends AbstractHealthIndicator {
@Autowired
private RegistryConfigRegister registryConfigRegister;
private final String protocol="zookeeper";
private int connectionTimeout =1000;
@Getter
private List zookeeperUrls = new ArrayList<>();
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
initZkUrls();
checkZkConnection(builder);
builder.up();
}
private void checkZkConnection(Health.Builder builder) {
for (String zookeeperUrl : zookeeperUrls) {
try {
new ZkClient(zookeeperUrl,connectionTimeout);
builder.up().withDetail(zookeeperUrl,true);
} catch (ZkTimeoutException e) {
builder.down().withDetail(zookeeperUrl,e.getMessage());
}
}
}
private void initZkUrls() {
List registryConfigs = registryConfigRegister.getConfigs();
for (RegistryConfig registryConfig : registryConfigs) {
if (registryConfig.getProtocol().equals(protocol)) {
zookeeperUrls.add(registryConfig.getAddress());
}
}
}
}