use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.
the class SimpleLoadBalanceHandler method handle.
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
if (invocation.getEndpoint() != null) {
invocation.next(asyncResp);
return;
}
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(invocation);
VersionedCache endpointsVersionedCache = discoveryTree.discovery(context, invocation.getAppId(), invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule());
if (endpointsVersionedCache.isEmpty()) {
asyncResp.consumerFail(ExceptionUtils.lbAddressNotFound(invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule(), endpointsVersionedCache.name()));
return;
}
List<Endpoint> endpoints = endpointsVersionedCache.data();
AtomicInteger index = indexMap.computeIfAbsent(endpointsVersionedCache.name(), name -> {
LOGGER.info("Create loadBalancer for {}.", name);
return new AtomicInteger();
});
LOGGER.debug("invocation {} use discoveryGroup {}.", invocation.getMicroserviceQualifiedName(), endpointsVersionedCache.name());
int idx = Math.abs(index.getAndIncrement());
idx = idx % endpoints.size();
Endpoint endpoint = endpoints.get(idx);
invocation.setEndpoint(endpoint);
invocation.next(asyncResp);
}
use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.
the class TestDateTimeSchema method testDateTimeSchemaMulticastRestTemplate.
private void testDateTimeSchemaMulticastRestTemplate() throws Exception {
DiscoveryContext context = new DiscoveryContext();
VersionedCache serversVersionedCache = discoveryTree.discovery(context, "springmvctest", "springmvc", "0+");
List<String> enpoints = serversVersionedCache.data();
RestTemplate restTemplate = RestTemplateBuilder.create();
for (String endpoint : enpoints) {
CseHttpEntity<?> entity = new CseHttpEntity<>(null);
InvocationContext invocationContext = new InvocationContext();
invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, endpoint);
entity.setContext(invocationContext);
Date date = new Date();
String dateValue = RestObjectMapperFactory.getRestObjectMapper().convertToString(date);
TestMgr.check(date.getTime(), restTemplate.exchange("cse://springmvc/dateTime/getDate?date={1}", HttpMethod.GET, entity, Date.class, dateValue).getBody().getTime());
entity = new CseHttpEntity<>(null);
invocationContext = new InvocationContext();
invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, parseEndpoint(endpoint));
entity.setContext(invocationContext);
date = new Date();
dateValue = RestObjectMapperFactory.getRestObjectMapper().convertToString(date);
TestMgr.check(date.getTime(), restTemplate.exchange("cse://springmvc/dateTime/getDate?date={1}", HttpMethod.GET, entity, Date.class, dateValue).getBody().getTime());
}
}
use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.
the class TestDateTimeSchema method testDateTimeSchemaMulticast.
private void testDateTimeSchemaMulticast() throws Exception {
DiscoveryContext context = new DiscoveryContext();
VersionedCache serversVersionedCache = discoveryTree.discovery(context, "springmvctest", "springmvc", "0+");
List<String> enpoints = serversVersionedCache.data();
for (String endpoint : enpoints) {
InvocationContext invocationContext = new InvocationContext();
invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, endpoint);
Date date = new Date();
TestMgr.check(date.getTime(), dateTimeSchemaWithContextInf.getDate(invocationContext, date).getTime());
invocationContext = new InvocationContext();
invocationContext.addLocalContext(LoadbalanceHandler.SERVICECOMB_SERVER_ENDPOINT, parseEndpoint(endpoint));
date = new Date();
TestMgr.check(date.getTime(), dateTimeSchemaWithContextInf.getDate(invocationContext, date).getTime());
}
}
use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.
the class IsolationDiscoveryFilterTest method before.
@Before
public void before() {
discoveryContext = new DiscoveryContext();
discoveryContext.setInputParameters(invocation);
discoveryTreeNode = new DiscoveryTreeNode();
Mockito.doAnswer(a -> a.getArguments()[0]).when(transport).parseAddress(Mockito.anyString());
data = new HashMap<>();
for (int i = 0; i < 3; ++i) {
MicroserviceInstance instance = new MicroserviceInstance();
instance.setInstanceId("i" + i);
String endpoint = "rest://127.0.0.1:" + i;
instance.setEndpoints(Collections.singletonList(endpoint));
data.put(instance.getInstanceId(), instance);
ServiceCombServer serviceCombServer = new ServiceCombServer(invocation.getMicroserviceName(), transport, new CacheEndpoint(endpoint, instance));
ServiceCombLoadBalancerStats.INSTANCE.getServiceCombServerStats(serviceCombServer);
}
discoveryTreeNode.data(data);
filter = new IsolationDiscoveryFilter();
TestServiceCombServerStats.releaseTryingChance();
}
use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project java-chassis by ServiceComb.
the class TestDiscoveryTree method filterRerun.
@Test
public void filterRerun() {
parent.name("1.0.0-2.0.0");
discoveryTree.addFilter(new DiscoveryFilterForTest("g1") {
@Override
public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
if (context.getContextParameter("step") == null) {
context.pushRerunFilter();
context.putContextParameter("step", 1);
return new DiscoveryTreeNode().name(groupName).data("first");
}
return new DiscoveryTreeNode().name(groupName).data("second");
}
});
discoveryTree.addFilter(new DiscoveryFilterForTest(null) {
@Override
public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
if ("first".equals(parent.data())) {
return new DiscoveryTreeNode();
}
return new DiscoveryTreeNode().data(parent.data());
}
});
result = discoveryTree.discovery(context, parent);
Assert.assertEquals("second", result.data());
}
Aggregations