use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class ServiceRegistryClientImpl method findServiceInstances.
@Override
public MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
// must register self first, and then invoke findServiceInstances
if (consumerId == null) {
LOGGER.error("find microservice instance {}/{}/{} failed, not registered to serviceCenter.", appId, serviceName, versionRule);
return null;
}
MicroserviceInstances microserviceInstances = new MicroserviceInstances();
IpPort ipPort = ipPortManager.getAvailableAddress();
CountDownLatch countDownLatch = new CountDownLatch(1);
RequestParam requestParam = new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("version", versionRule).addHeader("X-ConsumerId", consumerId);
if (revision != null) {
requestParam.addQueryParam("rev", revision);
}
RestUtils.get(ipPort, Const.REGISTRY_API.MICROSERVICE_INSTANCES, requestParam, syncHandlerForInstances(countDownLatch, microserviceInstances));
try {
countDownLatch.await();
if (!microserviceInstances.isNeedRefresh()) {
return microserviceInstances;
}
if (microserviceInstances.getInstancesResponse() == null) {
// error
return null;
}
List<MicroserviceInstance> list = microserviceInstances.getInstancesResponse().getInstances();
if (list == null) {
microserviceInstances.getInstancesResponse().setInstances(new ArrayList<>());
}
return microserviceInstances;
} catch (Exception e) {
LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
}
return null;
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class LocalServiceRegistryClientImpl method updateInstanceProperties.
@Override
public boolean updateInstanceProperties(String microserviceId, String microserviceInstanceId, Map<String, String> instanceProperties) {
Map<String, MicroserviceInstance> instanceMap = microserviceInstanceMap.get(microserviceId);
if (instanceMap == null) {
throw new IllegalArgumentException("Invalid serviceId, serviceId=" + microserviceId);
}
MicroserviceInstance microserviceInstance = instanceMap.get(microserviceInstanceId);
if (microserviceInstance == null) {
throw new IllegalArgumentException(String.format("Invalid argument. microserviceId=%s, microserviceInstanceId=%s.", microserviceId, microserviceInstanceId));
}
if (instanceProperties != null) {
microserviceInstance.getProperties().putAll(instanceProperties);
}
return true;
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class LocalServiceRegistryClientImpl method initFromData.
private void initFromData(Map<String, Object> data) {
for (Entry<String, Object> entry : data.entrySet()) {
String name = entry.getKey();
@SuppressWarnings("unchecked") List<Map<String, Object>> serviceConfigs = (List<Map<String, Object>>) entry.getValue();
for (Map<String, Object> serviceConfig : serviceConfigs) {
@SuppressWarnings("unchecked") List<Map<String, Object>> instancesConfig = (List<Map<String, Object>>) serviceConfig.get("instances");
String appId = (String) serviceConfig.get("appid");
String version = (String) serviceConfig.get("version");
String serviceId = (String) serviceConfig.get("id");
Microservice microservice = new Microservice();
microservice.setAppId(appId == null ? DEFAULT_APPLICATION_ID : appId);
microservice.setServiceName(name);
microservice.setVersion(version);
microservice.setServiceId(serviceId == null ? UUID.randomUUID().toString() : serviceId);
microserviceIdMap.put(microservice.getServiceId(), microservice);
Map<String, MicroserviceInstance> instanceMap = new ConcurrentHashMap<>();
for (Map<String, Object> instanceConfig : instancesConfig) {
@SuppressWarnings("unchecked") List<String> endpoints = (List<String>) instanceConfig.get("endpoints");
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId(UUID.randomUUID().toString());
instance.setEndpoints(endpoints);
instance.setServiceId(microservice.getServiceId());
instanceMap.put(instance.getInstanceId(), instance);
}
microserviceInstanceMap.put(microservice.getServiceId(), instanceMap);
}
}
if (!data.isEmpty()) {
revision.incrementAndGet();
}
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class MicroserviceVersions method pullInstances.
public void pullInstances() {
if (pendingPullCount.decrementAndGet() != 0) {
return;
}
MicroserviceInstances microserviceInstances = RegistryUtils.findServiceInstances(appId, microserviceName, DefinitionConst.VERSION_RULE_ALL, revision);
if (microserviceInstances == null) {
return;
}
if (!microserviceInstances.isNeedRefresh()) {
return;
}
List<MicroserviceInstance> pulledInstances = microserviceInstances.getInstancesResponse().getInstances();
String rev = microserviceInstances.getRevision();
safeSetInstances(pulledInstances, rev);
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class InstanceCache method createTransportMap.
protected Map<String, List<CacheEndpoint>> createTransportMap() {
Map<String, List<CacheEndpoint>> transportMap = new HashMap<>();
for (MicroserviceInstance instance : instanceMap.values()) {
// 过滤到不可用实例
if (instance.getStatus() != MicroserviceInstanceStatus.UP) {
continue;
}
for (String endpoint : instance.getEndpoints()) {
try {
URI uri = URI.create(endpoint);
String transportName = uri.getScheme();
List<CacheEndpoint> cacheEndpointList = transportMap.computeIfAbsent(transportName, k -> new ArrayList<>());
cacheEndpointList.add(new CacheEndpoint(endpoint, instance));
} catch (Exception e) {
LOGGER.warn("unrecognized address find, ignore " + endpoint);
}
}
}
return transportMap;
}
Aggregations