use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.
the class RegistrationManager method registerMicroserviceMappingByEndpoints.
/**
* @see #registerMicroserviceMapping(String, String, List, Class)
* @param endpoints the endpoints of 3rd party service. Each of endpoints will be treated as a separated instance.
* Format of the endpoints is the same as the endpoints that ServiceComb microservices register in service-center,
* like {@code rest://127.0.0.1:8080}
*/
public void registerMicroserviceMappingByEndpoints(String microserviceName, String version, List<String> endpoints, Class<?> schemaIntfCls) {
ArrayList<MicroserviceInstance> microserviceInstances = new ArrayList<>();
for (String endpoint : endpoints) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setEndpoints(Collections.singletonList(endpoint));
microserviceInstances.add(instance);
}
registerMicroserviceMapping(microserviceName, version, microserviceInstances, schemaIntfCls);
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.
the class AbstractEndpointDiscoveryFilter method createDiscoveryTreeNode.
@SuppressWarnings("unchecked")
protected DiscoveryTreeNode createDiscoveryTreeNode(String expectTransportName, DiscoveryContext context, DiscoveryTreeNode parent) {
List<Object> endpoints = new ArrayList<>();
for (MicroserviceInstance instance : ((Map<String, MicroserviceInstance>) parent.data()).values()) {
for (String endpoint : instance.getEndpoints()) {
try {
URI uri = URI.create(endpoint);
String transportName = uri.getScheme();
if (!isTransportNameMatch(transportName, expectTransportName)) {
continue;
}
Object objEndpoint = createEndpoint(context, transportName, endpoint, instance);
if (objEndpoint == null) {
continue;
}
endpoints.add(objEndpoint);
} catch (Exception e) {
LOGGER.warn("unrecognized address find, ignore {}.", endpoint);
}
}
}
return new DiscoveryTreeNode().subName(parent, expectTransportName).data(endpoints);
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.
the class InstanceCache method createTransportMap.
protected Map<String, List<CacheEndpoint>> createTransportMap() {
Map<String, List<CacheEndpoint>> transportMap = new HashMap<>();
for (MicroserviceInstance instance : instanceMap.values()) {
// This is only used for service center, not change it now
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;
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.
the class TestAbstractTransportDiscoveryFilter method createDiscoveryTree_ignoreInvalid.
@Test
public void createDiscoveryTree_ignoreInvalid() {
MicroserviceInstance instance1 = createInstance("a", "b");
MicroserviceInstance instance2 = createInstance("");
Map<String, MicroserviceInstance> instances = createMicroserviceInstances("name", instance1, instance2);
parent.data(instances);
result = filter.createDiscoveryTreeNode("", context, parent);
Assert.assertEquals("parent/", result.name());
Assert.assertThat(result.collectionData(), Matchers.contains(instance1.getEndpoints().toArray()));
}
use of org.apache.servicecomb.registry.api.registry.MicroserviceInstance in project java-chassis by ServiceComb.
the class TestLoadBalanceHandler2 method testZoneAwareAndIsolationFilterWorks.
@Test
public void testZoneAwareAndIsolationFilterWorks() throws Exception {
ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
when(microserviceMeta.getAppId()).thenReturn("testApp");
when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
when(referenceConfig.getTransport()).thenReturn("rest");
Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
TransportManager transportManager = Mockito.mock(TransportManager.class);
Transport transport = Mockito.mock(Transport.class);
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
// set up data
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");
List<String> allMatchEndpoint = new ArrayList<>();
allMatchEndpoint.add("rest://localhost:9090");
allmatchInstance.setEndpoints(allMatchEndpoint);
allmatchInstance.setDataCenterInfo(info);
allmatchInstance.setInstanceId("allmatchInstance");
MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region");
info.setAvailableZone("test-zone2");
List<String> regionMatchEndpoint = new ArrayList<>();
regionMatchEndpoint.add("rest://localhost:9091");
regionMatchInstance.setEndpoints(regionMatchEndpoint);
regionMatchInstance.setDataCenterInfo(info);
regionMatchInstance.setInstanceId("regionMatchInstance");
MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
info = new DataCenterInfo();
info.setName("test");
info.setRegion("test-Region2");
info.setAvailableZone("test-zone2");
List<String> noMatchEndpoint = new ArrayList<>();
noMatchEndpoint.add("rest://localhost:9092");
noneMatchInstance.setEndpoints(noMatchEndpoint);
noneMatchInstance.setDataCenterInfo(info);
noneMatchInstance.setInstanceId("noneMatchInstance");
Map<String, MicroserviceInstance> data = new HashMap<>();
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
scbEngine.setTransportManager(transportManager);
LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
mockUpInstanceCacheManager(instanceCacheManager);
when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
when(transportManager.findTransport("rest")).thenReturn(transport);
LoadbalanceHandler handler = null;
LoadBalancer loadBalancer = null;
ServiceCombServer server = null;
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals(null, server);
data.put("noneMatchInstance", noneMatchInstance);
parent.cacheVersion(1);
handler = new LoadbalanceHandler();
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9092", server.getEndpoint().getEndpoint());
data.put("regionMatchInstance", regionMatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
data.put("allmatchInstance", allmatchInstance);
parent.cacheVersion(parent.cacheVersion() + 1);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
// if errorThresholdPercentage is 0,that means errorThresholdPercentage is not active.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "0");
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
// if errorThresholdPercentage greater than 0, it will activate.
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage", "20");
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", "10");
ServiceCombServer server2 = server;
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
mockDelayMillis(20);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9090", server.getEndpoint().getEndpoint());
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
loadBalancer = handler.getOrCreateLoadBalancer(invocation);
server = loadBalancer.chooseServer(invocation);
Assert.assertEquals("rest://localhost:9091", server.getEndpoint().getEndpoint());
}
Aggregations