专栏首页码匠的流水账聊聊spring cloud的ConsulAutoConfiguration
原创

聊聊spring cloud的ConsulAutoConfiguration

本文主要研究一下spring cloud的ConsulAutoConfiguration

ConsulAutoConfiguration

spring-cloud-consul-core-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/ConsulAutoConfiguration.java

@Configuration
@EnableConfigurationProperties
@ConditionalOnConsulEnabled
public class ConsulAutoConfiguration {
​
    @Bean
    @ConditionalOnMissingBean
    public ConsulProperties consulProperties() {
        return new ConsulProperties();
    }
​
    @Bean
    @ConditionalOnMissingBean
    public ConsulClient consulClient(ConsulProperties consulProperties) {
        final int agentPort = consulProperties.getPort();
        final String agentHost = !StringUtils.isEmpty(consulProperties.getScheme())
                ? consulProperties.getScheme() + "://" + consulProperties.getHost()
                : consulProperties.getHost();
​
        if (consulProperties.getTls() != null) {
            ConsulProperties.TLSConfig tls = consulProperties.getTls();
            TLSConfig tlsConfig = new TLSConfig(tls.getKeyStoreInstanceType(),
                    tls.getCertificatePath(), tls.getCertificatePassword(),
                    tls.getKeyStorePath(), tls.getKeyStorePassword());
            return new ConsulClient(agentHost, agentPort, tlsConfig);
        }
        return new ConsulClient(agentHost, agentPort);
    }
​
    @Configuration
    @ConditionalOnClass(Endpoint.class)
    protected static class ConsulHealthConfig {
​
        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnEnabledEndpoint
        public ConsulEndpoint consulEndpoint(ConsulClient consulClient) {
            return new ConsulEndpoint(consulClient);
        }
​
        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnEnabledHealthIndicator("consul")
        public ConsulHealthIndicator consulHealthIndicator(ConsulClient consulClient) {
            return new ConsulHealthIndicator(consulClient);
        }
​
    }
​
    @ConditionalOnClass({ Retryable.class, Aspect.class, AopAutoConfiguration.class })
    @Configuration
    @EnableRetry(proxyTargetClass = true)
    @Import(AopAutoConfiguration.class)
    @EnableConfigurationProperties(RetryProperties.class)
    protected static class RetryConfiguration {
​
        @Bean(name = "consulRetryInterceptor")
        @ConditionalOnMissingBean(name = "consulRetryInterceptor")
        public RetryOperationsInterceptor consulRetryInterceptor(
                RetryProperties properties) {
            return RetryInterceptorBuilder.stateless()
                    .backOffOptions(properties.getInitialInterval(),
                            properties.getMultiplier(), properties.getMaxInterval())
                    .maxAttempts(properties.getMaxAttempts()).build();
        }
​
    }
​
}
  • ConsulAutoConfiguration注册了ConsulClient、ConsulEndpoint、ConsulHealthIndicator

ConsulEndpoint

spring-cloud-consul-core-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/ConsulEndpoint.java

@Endpoint(id = "consul")
public class ConsulEndpoint {
​
    private ConsulClient consul;
​
    public ConsulEndpoint(ConsulClient consul) {
        this.consul = consul;
    }
​
    @ReadOperation
    public ConsulData invoke() {
        ConsulData data = new ConsulData();
        // data.setKeyValues(kvClient.getKeyValueRecurse());
        Response<Map<String, Service>> agentServices = this.consul.getAgentServices();
        data.setAgentServices(agentServices.getValue());
​
        Response<Map<String, List<String>>> catalogServices = this.consul
                .getCatalogServices(QueryParams.DEFAULT);
​
        for (String serviceId : catalogServices.getValue().keySet()) {
            Response<List<CatalogService>> response = this.consul
                    .getCatalogService(serviceId, QueryParams.DEFAULT);
            data.getCatalogServices().put(serviceId, response.getValue());
        }
​
        Response<List<Node>> catalogNodes = this.consul
                .getCatalogNodes(QueryParams.DEFAULT);
        data.setCatalogNodes(catalogNodes.getValue());
​
        return data;
    }
​
    /**
     * Represents Consul data related to catalog entries and agent servies.
     */
    public static class ConsulData {
​
        Map<String, List<CatalogService>> catalogServices = new LinkedHashMap<>();
​
        Map<String, Service> agentServices;
​
        List<Node> catalogNodes;
​
        public ConsulData() {
        }
​
        public Map<String, List<CatalogService>> getCatalogServices() {
            return this.catalogServices;
        }
​
        public void setCatalogServices(
                Map<String, List<CatalogService>> catalogServices) {
            this.catalogServices = catalogServices;
        }
​
        public Map<String, Service> getAgentServices() {
            return this.agentServices;
        }
​
        public void setAgentServices(Map<String, Service> agentServices) {
            this.agentServices = agentServices;
        }
​
        public List<Node> getCatalogNodes() {
            return this.catalogNodes;
        }
​
        public void setCatalogNodes(List<Node> catalogNodes) {
            this.catalogNodes = catalogNodes;
        }
​
        @Override
        public String toString() {
            return new ToStringCreator(this)
                    .append("catalogServices", this.catalogServices)
                    .append("agentServices", this.agentServices)
                    .append("catalogNodes", this.catalogNodes).toString();
        }
​
    }
​
}
  • ConsulEndpoint有个ReadOperation,返回的是ConsulData,包含了agentServices、agentServices、catalogNodes

ConsulHealthIndicator

spring-cloud-consul-core-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/ConsulHealthIndicator.java

public class ConsulHealthIndicator extends AbstractHealthIndicator {
​
    private ConsulClient consul;
​
    public ConsulHealthIndicator(ConsulClient consul) {
        this.consul = consul;
    }
​
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        final Response<String> leaderStatus = this.consul.getStatusLeader();
        final Response<Map<String, List<String>>> services = this.consul
                .getCatalogServices(QueryParams.DEFAULT);
        builder.up().withDetail("leader", leaderStatus.getValue()).withDetail("services",
                services.getValue());
    }
​
}
  • ConsulHealthIndicator继承了AbstractHealthIndicator,其doHealthCheck会调用ConsulClient.getStatusLeader获取leaderStatus,以及ConsulClient.getCatalogServices获取catalogServices信息

小结

  • ConsulAutoConfiguration注册了ConsulClient、ConsulEndpoint、ConsulHealthIndicator
  • ConsulEndpoint有个ReadOperation,返回的是ConsulData,包含了agentServices、agentServices、catalogNodes
  • ConsulHealthIndicator继承了AbstractHealthIndicator,其doHealthCheck会调用ConsulClient.getStatusLeader获取leaderStatus,以及ConsulClient.getCatalogServices获取catalogServices信息

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊spring cloud consul的TtlScheduler

    spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/clo...

    codecraft
  • 聊聊spring cloud的ConsulServer

    spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/clo...

    codecraft
  • 聊聊elasticsearch的NodesFaultDetection

    本文主要研究一下elasticsearch的NodesFaultDetection

    codecraft
  • 聊聊spring cloud consul的TtlScheduler

    spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/clo...

    codecraft
  • 《从0到1学习Flink》—— 如何自定义 Data Source ?

    在 《从0到1学习Flink》—— Data Source 介绍 文章中,我给大家介绍了 Flink Data Source 以及简短的介绍了一下自定义 Dat...

    zhisheng
  • 设计模式 开闭原则

    软件实体应该对扩展开放,对修改关闭,即实体应当通过扩展实现变化,而不是修改代码实现变化

    mySoul
  • java内部类

    用户3030674
  • Java自动化测试(登陆接口测试 14)

    zx钟
  • 前后端数据交互-json 你还不会传集合吗?

    json(JavaScript Object Notation) 是一种数据格式,就是将key和value按规范封装成一串字符串,便于传输等。

    唔仄lo咚锵
  • YII2框架使用控制台命令的方法分析

    有些时候我们需要通过crontab在后台跑一些定时脚本,这时候就需要用到控制台命令了。

    砸漏

扫码关注云+社区

领取腾讯云代金券