Search in sources :

Example 6 with DiscoveryContext

use of org.apache.servicecomb.registry.discovery.DiscoveryContext in project incubator-servicecomb-java-chassis by apache.

the class LoadbalanceHandler method getOrCreateLoadBalancer.

protected LoadBalancer getOrCreateLoadBalancer(Invocation invocation) {
    DiscoveryContext context = new DiscoveryContext();
    context.setInputParameters(invocation);
    VersionedCache serversVersionedCache = discoveryTree.discovery(context, invocation.getAppId(), invocation.getMicroserviceName(), invocation.getMicroserviceVersionRule());
    invocation.addLocalContext(CONTEXT_KEY_SERVER_LIST, serversVersionedCache.data());
    return loadBalancerMap.computeIfAbsent(serversVersionedCache.name(), name -> createLoadBalancer(invocation.getMicroserviceName()));
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) VersionedCache(org.apache.servicecomb.foundation.common.cache.VersionedCache)

Example 7 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 8 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 9 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 10 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)

Aggregations

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