spring cloud的ConsulServer怎么用

spring,spring cloud"/>序本文主要研究一下spring cloud的ConsulServerConsulServerspring-cloud-consul-discovery-2.1.2.RELEAS

本篇文章给大家分享的是有关春云的ConsulServer怎么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

本文主要研究一下春云的ConsulServer

ConsulServer

spring-cloud-consult-discovery-2。1 .2 .发布源。罐子!/org/spring framework/cloud/consult/discovery/consul server。爪哇

公共类consultlserverextendserver { 0

privatefinitimetainfometa信息;

privatefilehealthserviceservice;

privatefinalMapString,Stringmetadata

公共使用者服务器(financehealthservicehealth service){ 0

super(findHost(healthService),healthService.getService().getPort());

this.service=healthService

这个。metadata=consulserverrutils。getmetadata(这个。服务);

这个。元信息=新元信息(){ 0

@覆盖

public stringgetappname(){ 0

returnconsumerserver。这个。服务。getservice().getServiCe();

}

@覆盖

public stringgetservergroup(){ 0

returngetMetadata().get(' group ');

}

@覆盖

public stringgetserviceidfordiscol(){ 0

返回空

}

@覆盖

public stringgetinstanceid(){ 0

returnconsumerserver。这个。服务。getservice().getId();

}

};

setAlive(isPassingChecks());

}

@覆盖

publicategameinfogetmeta info(){ 0

returnthis.metaInfo

}

公共卫生服务gethealth service(){ 0

归还这个。服务;

}

publicMapString,StringGetMetadata(){ 0

返回this.metadata

}

public booleanispassingchecks(){ 0

(检查:这个。服务。getchecks()){ 0

if(check.getStatus()!=检查检查状态。通过){ 0

returnfalse

}

}

返回真

}

}ConsulServer继承了com。网飞。负载平衡器。服务器;其构造器会调用isPassingChecks方法来setAlive,它通过健康服务来获取检查的状态

ConsulServerList

spring-cloud-consult-discovery-2。1 .2 .发布源。罐子!/org/spring framework/cloud/consult/discovery/consulserverlist。爪哇

publicclassCon

sulServerList extends AbstractServerList<ConsulServer> {
private final ConsulClient client;
private final ConsulDiscoveryProperties properties;
private String serviceId;
public ConsulServerList(ConsulClient client, ConsulDiscoveryProperties properties) {
this.client = client;
this.properties = properties;
}
protected ConsulClient getClient() {
return this.client;
}
protected ConsulDiscoveryProperties getProperties() {
return this.properties;
}
protected String getServiceId() {
return this.serviceId;
}
@Override
public void initWithNiwsConfig(IClientConfig clientConfig) {
this.serviceId = clientConfig.getClientName();
}
@Override
public List<ConsulServer> getInitialListOfServers() {
return getServers();
}
@Override
public List<ConsulServer> getUpdatedListOfServers() {
return getServers();
}
private List<ConsulServer> getServers() {
if (this.client == null) {
return Collections.emptyList();
}
String tag = getTag(); // null is ok
Response<List<HealthService>> response = this.client.getHealthServices(
this.serviceId, tag, this.properties.isQueryPassing(),
createQueryParamsForClientRequest(), this.properties.getAclToken());
if (response.getValue() == null || response.getValue().isEmpty()) {
return Collections.emptyList();
}
return transformResponse(response.getValue());
}
/**
 * Transforms the response from Consul in to a list of usable {@link ConsulServer}s.
 * @param healthServices the initial list of servers from Consul. Guaranteed to be
 * non-empty list
 * @return ConsulServer instances
 * @see ConsulServer#ConsulServer(HealthService)
 */
protected List<ConsulServer> transformResponse(List<HealthService> healthServices) {
List<ConsulServer> servers = new ArrayList<>();
for (HealthService service : healthServices) {
ConsulServer server = new ConsulServer(service);
if (server.getMetadata()
.containsKey(this.properties.getDefaultZoneMetadataName())) {
server.setZone(server.getMetadata()
.get(this.properties.getDefaultZoneMetadataName()));
}
servers.add(server);
}
return servers;
}
/**
 * This method will create the {@link QueryParams} to use when retrieving the services
 * from Consul. By default {@link QueryParams#DEFAULT} is used. In case a datacenter
 * is specified for the current serviceId {@link QueryParams#datacenter} is set.
 * @return an instance of {@link QueryParams}
 */
protected QueryParams createQueryParamsForClientRequest() {
String datacenter = getDatacenter();
if (datacenter != null) {
return new QueryParams(datacenter);
}
return QueryParams.DEFAULT;
}
protected String getTag() {
return this.properties.getQueryTagForService(this.serviceId);
}
protected String getDatacenter() {
return this.properties.getDatacenters().get(this.serviceId);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ConsulServerList{");
sb.append("serviceId='").append(this.serviceId).append('\'');
sb.append(", tag=").append(getTag());
sb.append('}');
return sb.toString();
}
}

  • ConsulServerList继承了com.netflix.loadbalancer.AbstractServerList;其getInitialListOfServers及getUpdatedListOfServers方法都会调用getServers方法;它通过ConsulClient.getHealthServices来获取指定serviceId的HealthService列表,然后通过transformResponse方法包装为ConsulServer列表

ConsulRibbonClientConfiguration

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

@Configuration
public class ConsulRibbonClientConfiguration {
	protected static final String VALUE_NOT_SET = "__not__set__";
	protected static final String DEFAULT_NAMESPACE = "ribbon";
	@Autowired
	private ConsulClient client;
	@Value("${ribbon.client.name}")
	private String serviceId = "client";
	public ConsulRibbonClientConfiguration() {
	}
	public ConsulRibbonClientConfiguration(String serviceId) {
		this.serviceId = serviceId;
	}
	@Bean
	@ConditionalOnMissingBean
	public ServerList<?> ribbonServerList(IClientConfig config,
			ConsulDiscoveryProperties properties) {
		ConsulServerList serverList = new ConsulServerList(this.client, properties);
		serverList.initWithNiwsConfig(config);
		return serverList;
	}
	@Bean
	@ConditionalOnMissingBean
	public ServerListFilter<Server> ribbonServerListFilter() {
		return new HealthServiceServerListFilter();
	}
	@Bean
	@ConditionalOnMissingBean
	public IPing ribbonPing() {
		return new ConsulPing();
	}
	@Bean
	@ConditionalOnMissingBean
	public ConsulServerIntrospector serverIntrospector() {
		return new ConsulServerIntrospector();
	}
	@PostConstruct
	public void preprocess() {
		setProp(this.serviceId, DeploymentContextBasedVipAddresses.key(), this.serviceId);
		setProp(this.serviceId, EnableZoneAffinity.key(), "true");
	}
	protected void setProp(String serviceId, String suffix, String value) {
		// how to set the namespace properly?
		String key = getKey(serviceId, suffix);
		DynamicStringProperty property = getProperty(key);
		if (property.get().equals(VALUE_NOT_SET)) {
			ConfigurationManager.getConfigInstance().setProperty(key, value);
		}
	}
	protected DynamicStringProperty getProperty(String key) {
		return DynamicPropertyFactory.getInstance().getStringProperty(key, VALUE_NOT_SET);
	}
	protected String getKey(String serviceId, String suffix) {
		return serviceId + "." + DEFAULT_NAMESPACE + "." + suffix;
	}
}
  • ConsulRibbonClientConfiguration注入了ribbonServerList,其创建的是ConsulServerList

小结

  • ConsulServer继承了com.netflix.loadbalancer.Server;其构造器会调用isPassingChecks方法来setAlive,它通过HealthService来获取checks的状态

  • ConsulServerList继承了com.netflix.loadbalancer.AbstractServerList;其getInitialListOfServers及getUpdatedListOfServers方法都会调用getServers方法;它通过ConsulClient.getHealthServices来获取指定serviceId的HealthService列表,然后通过transformResponse方法包装为ConsulServer列表

  • ConsulRibbonClientConfiguration注入了ribbonServerList,其创建的是ConsulServerList

doc

  • ConsulServer

以上就是spring cloud的ConsulServer怎么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/36007.html

(0)