真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

springcloud中ConsulServiceRegistry的用法

本篇內(nèi)容介紹了“spring cloud中ConsulServiceRegistry的用法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),岳麓企業(yè)網(wǎng)站建設(shè),岳麓品牌網(wǎng)站建設(shè),網(wǎng)站定制,岳麓網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,岳麓網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

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

ServiceRegistry

spring-cloud-commons-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/client/serviceregistry/ServiceRegistry.java

public interface ServiceRegistry {

	/**
	 * Registers the registration. A registration typically has information about an
	 * instance, such as its hostname and port.
	 * @param registration registration meta data
	 */
	void register(R registration);

	/**
	 * Deregisters the registration.
	 * @param registration registration meta data
	 */
	void deregister(R registration);

	/**
	 * Closes the ServiceRegistry. This is a lifecycle method.
	 */
	void close();

	/**
	 * Sets the status of the registration. The status values are determined by the
	 * individual implementations.
	 * @param registration The registration to update.
	 * @param status The status to set.
	 * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
	 */
	void setStatus(R registration, String status);

	/**
	 * Gets the status of a particular registration.
	 * @param registration The registration to query.
	 * @param  The type of the status.
	 * @return The status of the registration.
	 * @see org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint
	 */
	 T getStatus(R registration);

}
  • ServiceRegistry接口定義了register、deregister、close、setStatus、getStatus方法

ConsulServiceRegistry

spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/serviceregistry/ConsulServiceRegistry.java

public class ConsulServiceRegistry implements ServiceRegistry {

	private static Log log = LogFactory.getLog(ConsulServiceRegistry.class);

	private final ConsulClient client;

	private final ConsulDiscoveryProperties properties;

	private final TtlScheduler ttlScheduler;

	private final HeartbeatProperties heartbeatProperties;

	public ConsulServiceRegistry(ConsulClient client,
			ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler,
			HeartbeatProperties heartbeatProperties) {
		this.client = client;
		this.properties = properties;
		this.ttlScheduler = ttlScheduler;
		this.heartbeatProperties = heartbeatProperties;
	}

	@Override
	public void register(ConsulRegistration reg) {
		log.info("Registering service with consul: " + reg.getService());
		try {
			this.client.agentServiceRegister(reg.getService(),
					this.properties.getAclToken());
			NewService service = reg.getService();
			if (this.heartbeatProperties.isEnabled() && this.ttlScheduler != null
					&& service.getCheck() != null
					&& service.getCheck().getTtl() != null) {
				this.ttlScheduler.add(reg.getInstanceId());
			}
		}
		catch (ConsulException e) {
			if (this.properties.isFailFast()) {
				log.error("Error registering service with consul: " + reg.getService(),
						e);
				ReflectionUtils.rethrowRuntimeException(e);
			}
			log.warn("Failfast is false. Error registering service with consul: "
					+ reg.getService(), e);
		}
	}

	@Override
	public void deregister(ConsulRegistration reg) {
		if (this.ttlScheduler != null) {
			this.ttlScheduler.remove(reg.getInstanceId());
		}
		if (log.isInfoEnabled()) {
			log.info("Deregistering service with consul: " + reg.getInstanceId());
		}
		this.client.agentServiceDeregister(reg.getInstanceId(),
				this.properties.getAclToken());
	}

	@Override
	public void close() {

	}

	@Override
	public void setStatus(ConsulRegistration registration, String status) {
		if (status.equalsIgnoreCase(OUT_OF_SERVICE.getCode())) {
			this.client.agentServiceSetMaintenance(registration.getInstanceId(), true);
		}
		else if (status.equalsIgnoreCase(UP.getCode())) {
			this.client.agentServiceSetMaintenance(registration.getInstanceId(), false);
		}
		else {
			throw new IllegalArgumentException("Unknown status: " + status);
		}

	}

	@Override
	public Object getStatus(ConsulRegistration registration) {
		String serviceId = registration.getServiceId();
		Response> response = this.client.getHealthChecksForService(serviceId,
				QueryParams.DEFAULT);
		List checks = response.getValue();

		for (Check check : checks) {
			if (check.getServiceId().equals(registration.getInstanceId())) {
				if (check.getName().equalsIgnoreCase("Service Maintenance Mode")) {
					return OUT_OF_SERVICE.getCode();
				}
			}
		}

		return UP.getCode();
	}

}
  • ConsulServiceRegistry實現(xiàn)了ServiceRegistry方法,其構(gòu)造器接收ConsulClient、ConsulDiscoveryProperties、TtlScheduler、HeartbeatProperties參數(shù)

  • 其regiter方法使用ConsulClient的agentServiceRegister注冊服務(wù),如果開啟heartbeat并設(shè)置ttl的話,會注冊一個heartbeat的延時任務(wù);deregister方法使用ConsulClient的agentServiceDeregister注銷服務(wù),同時從ttlScheduler移除該instance的延時任務(wù)

  • setStatus方法對OUT_OF_SERVICE執(zhí)行client.agentServiceSetMaintenance(registration.getInstanceId(), true),對UP執(zhí)行client.agentServiceSetMaintenance(registration.getInstanceId(), false);getStatus方法通過client.getHealthChecksForService(serviceId,QueryParams.DEFAULT)獲取Check列表,一旦有Service Maintenance Mode的check立即返回OUT_OF_SERVICE

ConsulRegistration

spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/serviceregistry/ConsulRegistration.java

public class ConsulRegistration implements Registration {

	private final NewService service;

	private ConsulDiscoveryProperties properties;

	public ConsulRegistration(NewService service, ConsulDiscoveryProperties properties) {
		this.service = service;
		this.properties = properties;
	}

	public NewService getService() {
		return this.service;
	}

	protected ConsulDiscoveryProperties getProperties() {
		return this.properties;
	}

	public String getInstanceId() {
		return getService().getId();
	}

	public String getServiceId() {
		return getService().getName();
	}

	@Override
	public String getHost() {
		return getService().getAddress();
	}

	@Override
	public int getPort() {
		return getService().getPort();
	}

	@Override
	public boolean isSecure() {
		return this.properties.getScheme().equalsIgnoreCase("https");
	}

	@Override
	public URI getUri() {
		return DefaultServiceInstance.getUri(this);
	}

	@Override
	public Map getMetadata() {
		return ConsulServerUtils.getMetadata(getService().getTags());
	}

}
  • ConsulRegistration實現(xiàn)了Registration接口,其getMetadata方法返回的是ConsulServerUtils.getMetadata(getService().getTags())

ConsulServiceRegistryAutoConfiguration

spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/serviceregistry/ConsulServiceRegistryAutoConfiguration.java

@Configuration
@ConditionalOnConsulEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.enabled", matchIfMissing = true)
@AutoConfigureBefore(ServiceRegistryAutoConfiguration.class)
public class ConsulServiceRegistryAutoConfiguration {

	@Autowired(required = false)
	private TtlScheduler ttlScheduler;

	@Bean
	@ConditionalOnMissingBean
	public ConsulServiceRegistry consulServiceRegistry(ConsulClient consulClient,
			ConsulDiscoveryProperties properties,
			HeartbeatProperties heartbeatProperties) {
		return new ConsulServiceRegistry(consulClient, properties, this.ttlScheduler,
				heartbeatProperties);
	}

	@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty("spring.cloud.consul.discovery.heartbeat.enabled")
	public TtlScheduler ttlScheduler(ConsulClient consulClient,
			HeartbeatProperties heartbeatProperties) {
		return new TtlScheduler(heartbeatProperties, consulClient);
	}

	@Bean
	@ConditionalOnMissingBean
	public HeartbeatProperties heartbeatProperties() {
		return new HeartbeatProperties();
	}

	@Bean
	@ConditionalOnMissingBean
	public ConsulDiscoveryProperties consulDiscoveryProperties(InetUtils inetUtils) {
		return new ConsulDiscoveryProperties(inetUtils);
	}

}
  • ConsulServiceRegistryAutoConfiguration注冊了ConsulServiceRegistry、TtlScheduler、HeartbeatProperties、ConsulDiscoveryProperties

小結(jié)

  • ConsulServiceRegistry實現(xiàn)了ServiceRegistry方法,其構(gòu)造器接收ConsulClient、ConsulDiscoveryProperties、TtlScheduler、HeartbeatProperties參數(shù)

  • 其regiter方法使用ConsulClient的agentServiceRegister注冊服務(wù),如果開啟heartbeat并設(shè)置ttl的話,會注冊一個heartbeat的延時任務(wù);deregister方法使用ConsulClient的agentServiceDeregister注銷服務(wù),同時從ttlScheduler移除該instance的延時任務(wù)

  • setStatus方法對OUT_OF_SERVICE執(zhí)行client.agentServiceSetMaintenance(registration.getInstanceId(), true),對UP執(zhí)行client.agentServiceSetMaintenance(registration.getInstanceId(), false);getStatus方法通過client.getHealthChecksForService(serviceId,QueryParams.DEFAULT)獲取Check列表,一旦有Service Maintenance Mode的check立即返回OUT_OF_SERVICE

“spring cloud中ConsulServiceRegistry的用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!


文章標題:springcloud中ConsulServiceRegistry的用法
文章路徑:http://weahome.cn/article/gidhpp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部