use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestServiceCombServerList method testServiceCombServerList.
@Test
public void testServiceCombServerList(@Injectable IClientConfig iClientConfig, @Mocked RegistryUtils registryUtils, @Mocked DiscoveryTree discoveryTree, @Injectable DiscoveryTreeNode versionedCache) {
Map<String, MicroserviceInstance> servers = new HashMap<>();
List<String> endpoints = new ArrayList<>();
endpoints.add("rest://localhost:3333");
endpoints.add("rest://localhost:4444");
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setServiceId("service1");
instance1.setInstanceId("service1-instance1");
instance1.setEndpoints(endpoints);
servers.put("service1-instance1", instance1);
new Expectations() {
{
iClientConfig.getClientName();
result = "serviceId1";
RegistryUtils.getAppId();
result = "app";
discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, anyString);
result = versionedCache;
versionedCache.data();
result = servers;
}
};
ServiceCombServerList list = new ServiceCombServerList();
list.initWithNiwsConfig(iClientConfig);
List<Server> serverList = list.getInitialListOfServers();
Assert.assertEquals(2, serverList.size());
Assert.assertEquals(4444, serverList.get(1).getPort());
Assert.assertEquals(serverList.size(), list.getUpdatedListOfServers().size());
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class CseDiscoveryClient method getInstances.
@Override
public List<ServiceInstance> getInstances(final String serviceId) {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);
DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId, key -> {
return new DiscoveryTree();
});
VersionedCache serversVersionedCache = discoveryTree.discovery(context, RegistryUtils.getAppId(), serviceId, DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<ServiceInstance> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return instances;
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class ServiceCombServerList method getInitialListOfServers.
@Override
public List<Server> getInitialListOfServers() {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);
VersionedCache serversVersionedCache = discoveryTree.discovery(context, RegistryUtils.getAppId(), serviceId, DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<Server> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new Server(uri.getHostOrIp(), uri.getPort()));
}
}
return instances;
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class ZoneAwareServerListFilterExt method getFilteredListOfServers.
@Override
public List<Server> getFilteredListOfServers(List<Server> list) {
List<Server> result = new ArrayList<>();
MicroserviceInstance myself = RegistryUtils.getMicroserviceInstance();
boolean find = false;
for (Server server : list) {
CseServer cseServer = (CseServer) server;
if (regionAndAZMatch(myself, cseServer.getInstance())) {
result.add(cseServer);
find = true;
}
}
if (!find) {
for (Server server : list) {
CseServer cseServer = (CseServer) server;
if (regionMatch(myself, cseServer.getInstance())) {
result.add(cseServer);
find = true;
}
}
}
if (!find) {
result = list;
}
return result;
}
use of org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance in project incubator-servicecomb-java-chassis by apache.
the class TestZoneAwareServerListFilterExt method testZoneAwareServerListFilterExt.
@Test
public void testZoneAwareServerListFilterExt(@Mocked RegistryUtils registryUtils) {
MicroserviceInstance myself = new MicroserviceInstance();
DataCenterInfo info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
myself.setDataCenterInfo(info);
MicroserviceInstance allmatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone");
allmatchInstance.setDataCenterInfo(info);
MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone2");
regionMatchInstance.setDataCenterInfo(info);
MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region2");
info.setAvailableZone("test-zone2");
noneMatchInstance.setDataCenterInfo(info);
new Expectations() {
{
RegistryUtils.getMicroserviceInstance();
result = myself;
}
};
ZoneAwareServerListFilterExt filter = new ZoneAwareServerListFilterExt();
List<Server> servers = new ArrayList<>();
CseServer noneMatchServer = new MockUp<CseServer>() {
@Mock
public String toString() {
return "noneMatchServer";
}
@Mock
public String getHost() {
return "noneMatchServer";
}
@Mock
public MicroserviceInstance getInstance() {
return noneMatchInstance;
}
}.getMockInstance();
CseServer regionMatchregionMatchServer = new MockUp<CseServer>() {
@Mock
public String toString() {
return "regionMatchregionMatchServer";
}
@Mock
public String getHost() {
return "regionMatchregionMatchServer";
}
@Mock
public MicroserviceInstance getInstance() {
return regionMatchInstance;
}
}.getMockInstance();
CseServer allmatchServer = new MockUp<CseServer>() {
@Mock
public String toString() {
return "allmatchServer";
}
@Mock
public String getHost() {
return "allmatchServer";
}
@Mock
public MicroserviceInstance getInstance() {
return allmatchInstance;
}
}.getMockInstance();
servers.add(noneMatchServer);
List<Server> result = filter.getFilteredListOfServers(servers);
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(result.get(0), noneMatchServer);
servers.add(regionMatchregionMatchServer);
result = filter.getFilteredListOfServers(servers);
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(result.get(0), regionMatchregionMatchServer);
servers.add(allmatchServer);
result = filter.getFilteredListOfServers(servers);
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(result.get(0), allmatchServer);
}
Aggregations