Search in sources :

Example 1 with DiscoveryContext

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);
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) Endpoint(org.apache.servicecomb.core.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(org.apache.servicecomb.core.Endpoint)

Example 2 with DiscoveryContext

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());
    }
}
Also used : CseHttpEntity(org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) RestTemplate(org.springframework.web.client.RestTemplate) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 3 with DiscoveryContext

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());
    }
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache) InvocationContext(org.apache.servicecomb.swagger.invocation.context.InvocationContext) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 4 with DiscoveryContext

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();
}
Also used : ServiceCombServer(org.apache.servicecomb.loadbalance.ServiceCombServer) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) Before(org.junit.Before)

Example 5 with DiscoveryContext

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());
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) Test(org.junit.Test)

Aggregations

DiscoveryContext (org.apache.servicecomb.registry.discovery.DiscoveryContext)10 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)5 DiscoveryTreeNode (org.apache.servicecomb.registry.discovery.DiscoveryTreeNode)5 MicroserviceInstance (org.apache.servicecomb.registry.api.registry.MicroserviceInstance)3 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 Invocation (org.apache.servicecomb.core.Invocation)2 InvocationContext (org.apache.servicecomb.swagger.invocation.context.InvocationContext)2 Test (org.junit.Test)2 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NotNull (javax.validation.constraints.NotNull)1 Expectations (mockit.Expectations)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Endpoint (org.apache.servicecomb.core.Endpoint)1 ServiceCombServer (org.apache.servicecomb.loadbalance.ServiceCombServer)1