189 8069 5689

springcloud中ConsulAutoConfiguration的用法

本篇内容主要讲解“spring cloud中ConsulAutoConfiguration的用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring cloud中ConsulAutoConfiguration的用法”吧!

创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计、成都网站设计、青岛网络推广、小程序定制开发、青岛网络营销、青岛企业策划、青岛品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供青岛建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com

本文主要研究一下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> agentServices = this.consul.getAgentServices();
		data.setAgentServices(agentServices.getValue());

		Response>> catalogServices = this.consul
				.getCatalogServices(QueryParams.DEFAULT);

		for (String serviceId : catalogServices.getValue().keySet()) {
			Response> response = this.consul
					.getCatalogService(serviceId, QueryParams.DEFAULT);
			data.getCatalogServices().put(serviceId, response.getValue());
		}

		Response> 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> catalogServices = new LinkedHashMap<>();

		Map agentServices;

		List catalogNodes;

		public ConsulData() {
		}

		public Map> getCatalogServices() {
			return this.catalogServices;
		}

		public void setCatalogServices(
				Map> catalogServices) {
			this.catalogServices = catalogServices;
		}

		public Map getAgentServices() {
			return this.agentServices;
		}

		public void setAgentServices(Map agentServices) {
			this.agentServices = agentServices;
		}

		public List getCatalogNodes() {
			return this.catalogNodes;
		}

		public void setCatalogNodes(List 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 leaderStatus = this.consul.getStatusLeader();
		final Response>> 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信息

到此,相信大家对“spring cloud中ConsulAutoConfiguration的用法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前标题:springcloud中ConsulAutoConfiguration的用法
链接分享:http://cdxtjz.cn/article/jsigej.html

其他资讯