I. Engineering structure

  • Spring-cloud-consul-binder: Consul message bus
  • Spring-cloud-consul-config: consul configuration center
  • Spring-cloud-consul-core: consul core module. All modules depend on core
  • Spring-cloud-consul-dependencies: Consul manages dependencies, which is a parent POM
  • Spring-cloud-consul-discovery: consul service registration and discovery
  • Spring-cloud-consul-sample: indicates an example code
  • Some of the starter

2. Spring-cloud-consul-core module

2.1 Engineering Structure

2.2 spring. Factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.consul.ConsulAutoConfiguration
Copy the code

Third, ConsulAutoConfiguration

3.1 Enabling Rules

@ the Configuration (proxyBeanMethods = false) @ EnableConfigurationProperties / / / / ConsulAutoConfiguration operative conditions 1, spring. Cloud. Consul. Enabled matchIfMissing = true / / 2, ConsulClient. Class is @ ConditionalOnConsulEnabled public class ConsulAutoConfiguration {Copy the code

3.2 Which beans are injected

  • Consul configuration file, including host and port of Consul
	@Bean
	@ConditionalOnMissingBean
	public ConsulProperties consulProperties() {
		return new ConsulProperties();
	}
Copy the code
  • ConsulClient a client that communicates with Consul server
@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); }Copy the code
  • Actuator health check related
@Configuration(proxyBeanMethods = false) @ConditionalOnClass(Endpoint.class) protected static class ConsulHealthConfig { @ Bean @ ConditionalOnMissingBean @ ConditionalOnAvailableEndpoint / * * * / physical/consul * / public exposure ConsulEndpoint consulEndpoint(ConsulClient consulClient) { return new ConsulEndpoint(consulClient); } @ Bean @ ConditionalOnMissingBean @ ConditionalOnEnabledHealthIndicator (" consul ") / * * * exposure/physical/health/consul * / public ConsulHealthIndicator consulHealthIndicator(ConsulClient consulClient) { return new ConsulHealthIndicator(consulClient); }}Copy the code
  • SpringRetry interceptor
    • Because the spring-retry dependency is optional, you need to manually import spring-retry
    • Usage: 1. Consul is used for consul service registration. 2
    • Default configuration: Retry every 1s. If a failure occurs, retry every 1.1s. A maximum of six times is allowed
@ConditionalOnClass({ Retryable.class, Aspect.class, AopAutoConfiguration.class })
   @Configuration(proxyBeanMethods = false)
   @EnableRetry(proxyTargetClass = true)
   @Import(AopAutoConfiguration.class)
   @EnableConfigurationProperties(RetryProperties.class)
   @ConditionalOnProperty(value = "spring.cloud.consul.retry.enabled",
   		matchIfMissing = true)
   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();
   	}

   }
Copy the code

Four,

  • SpringCloudConsul has support for message buses, service registration and discovery, and configuration centers
  • All modules rely on the Spring-Cloud-Consul-core module
  • The spring-cloud-consul-core module provides common beans such as ConsulClient and RetryInterceptor