Search in sources :

Example 16 with DiscoveryContext

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

the class PriorityInstancePropertyDiscoveryFilter method init.

@Override
protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
    propertyKey = DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.loadbalance.filter.priorityInstanceProperty.key", "environment").get();
    // group all instance by property
    Map<String, MicroserviceInstance> instances = parent.data();
    Map<String, Map<String, MicroserviceInstance>> groupByProperty = new HashMap<>();
    for (MicroserviceInstance microserviceInstance : instances.values()) {
        String propertyValue = new PriorityInstanceProperty(propertyKey, microserviceInstance).getPropertyValue();
        groupByProperty.computeIfAbsent(propertyValue, key -> new HashMap<>()).put(microserviceInstance.getInstanceId(), microserviceInstance);
    }
    Map<String, DiscoveryTreeNode> children = new HashMap<>();
    for (Map.Entry<String, Map<String, MicroserviceInstance>> entry : groupByProperty.entrySet()) {
        children.put(entry.getKey(), new DiscoveryTreeNode().subName(parent, entry.getKey()).data(entry.getValue()));
    }
    children.put(ALL_INSTANCE, new DiscoveryTreeNode().subName(parent, ALL_INSTANCE).data(instances));
    parent.children(children);
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) NotNull(javax.validation.constraints.NotNull) StringUtils(org.apache.commons.lang3.StringUtils) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) Objects(java.util.Objects) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Map(java.util.Map) AbstractDiscoveryFilter(org.apache.servicecomb.registry.discovery.AbstractDiscoveryFilter) Optional(java.util.Optional) RegistrationManager(org.apache.servicecomb.registry.RegistrationManager) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with DiscoveryContext

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

the class PriorityInstancePropertyDiscoveryFilterTest method executeTest.

private void executeTest(String selfProperty, Set<String> expectedMatchedKeys) {
    Invocation invocation = new Invocation();
    DiscoveryContext discoveryContext = new DiscoveryContext();
    discoveryContext.setInputParameters(invocation);
    self.getProperties().put(PROPERTY_KEY, selfProperty);
    DiscoveryTreeNode parent = new DiscoveryTreeNode();
    parent.name("parent");
    parent.data(instances);
    DiscoveryTreeNode node = filter.discovery(discoveryContext, parent);
    Map<String, MicroserviceInstance> filterInstance = node.data();
    assertThat(filterInstance.keySet()).containsAnyElementsOf(expectedMatchedKeys);
}
Also used : DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Invocation(org.apache.servicecomb.core.Invocation) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance)

Example 18 with DiscoveryContext

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

the class PriorityInstancePropertyDiscoveryFilter method init.

@Override
protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
    propertyKey = DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.loadbalance.filter.priorityInstanceProperty.key", "environment").get();
    // group all instance by property
    Map<String, MicroserviceInstance> instances = parent.data();
    Map<String, Map<String, MicroserviceInstance>> groupByProperty = new HashMap<>();
    for (MicroserviceInstance microserviceInstance : instances.values()) {
        String propertyValue = new PriorityInstanceProperty(propertyKey, microserviceInstance).getPropertyValue();
        groupByProperty.computeIfAbsent(propertyValue, key -> new HashMap<>()).put(microserviceInstance.getInstanceId(), microserviceInstance);
    }
    Map<String, DiscoveryTreeNode> children = new HashMap<>();
    for (Map.Entry<String, Map<String, MicroserviceInstance>> entry : groupByProperty.entrySet()) {
        children.put(entry.getKey(), new DiscoveryTreeNode().subName(parent, entry.getKey()).data(entry.getValue()));
    }
    children.put(ALL_INSTANCE, new DiscoveryTreeNode().subName(parent, ALL_INSTANCE).data(instances));
    parent.children(children);
}
Also used : MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) NotNull(javax.validation.constraints.NotNull) StringUtils(org.apache.commons.lang3.StringUtils) DynamicPropertyFactory(com.netflix.config.DynamicPropertyFactory) Invocation(org.apache.servicecomb.core.Invocation) Objects(java.util.Objects) DiscoveryContext(org.apache.servicecomb.registry.discovery.DiscoveryContext) Map(java.util.Map) AbstractDiscoveryFilter(org.apache.servicecomb.registry.discovery.AbstractDiscoveryFilter) Optional(java.util.Optional) RegistrationManager(org.apache.servicecomb.registry.RegistrationManager) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with DiscoveryContext

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

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 20 with DiscoveryContext

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

the class CommonHttpEdgeDispatcher method getOrCreateLoadBalancer.

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

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