Search in sources :

Example 6 with ServiceInfo

use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java-agent by polarismesh.

the class PolarisServiceRouter method getOneInstance.

/**
 * 利用ConsumerAPI进行路由、负载均衡
 *
 * @param service
 * @return
 */
public static ServiceInstance getOneInstance(String service) {
    LogUtils.logInvoke(PolarisServiceRouter.class, "getOneInstance");
    PolarisAgentProperties polarisAgentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
    String namespace = polarisAgentProperties.getNamespace();
    GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
    getOneInstanceRequest.setService(service);
    getOneInstanceRequest.setNamespace(namespace);
    String srcNamespace = polarisAgentProperties.getNamespace();
    String srcService = polarisAgentProperties.getService();
    if (StringUtils.isNotBlank(srcNamespace) || StringUtils.isNotBlank(srcService)) {
        ServiceInfo sourceService = new ServiceInfo();
        sourceService.setNamespace(srcNamespace);
        sourceService.setService(srcService);
        getOneInstanceRequest.setServiceInfo(sourceService);
    }
    Map<String, String> metadata = InvokeContextHolder.get().getMetadata();
    if (metadata != null) {
        getOneInstanceRequest.setMetadata(metadata);
    }
    InstancesResponse response = PolarisAPIFactory.getConsumerApi().getOneInstance(getOneInstanceRequest);
    List<Instance> instances = response.toServiceInstances().getInstances();
    if (CollectionUtils.isNotEmpty(instances)) {
        LOGGER.info("success to route and loadBalance by Polaris with instance:{}", instances.get(0));
    }
    return new PolarisServiceInstance(instances.get(0));
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Instance(com.tencent.polaris.api.pojo.Instance) PolarisServiceInstance(cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) PolarisServiceInstance(cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)

Example 7 with ServiceInfo

use of com.tencent.polaris.api.pojo.ServiceInfo in project polaris-java-agent by polarismesh.

the class PolarisServiceRouter method getRoutedServiceInstance.

/**
 * 路由服务实例(不具有熔断功能)
 *
 * @param dstInstances
 * @return
 */
public static ServiceInstances getRoutedServiceInstance(ServiceInstances dstInstances) {
    LogUtils.logInvoke(PolarisServiceRouter.class, "getRoutedServiceInstance");
    PolarisAgentProperties agentProperties = PolarisAgentPropertiesFactory.getPolarisAgentProperties();
    // 执行服务路由
    ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
    // 主调方信息
    ServiceInfo srcSourceInfo = new ServiceInfo();
    String srcService = agentProperties.getService();
    String srcNamespace = agentProperties.getNamespace();
    if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
        srcSourceInfo.setNamespace(srcNamespace);
        srcSourceInfo.setService(srcService);
        processRoutersRequest.setSourceService(srcSourceInfo);
    }
    ProcessRoutersRequest.RouterNamesGroup routerNamesGroup = new ProcessRoutersRequest.RouterNamesGroup();
    List<String> coreRouters = new ArrayList<>();
    coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_RULE);
    coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_METADATA);
    coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY);
    // 设置走规则路由
    routerNamesGroup.setCoreRouters(coreRouters);
    processRoutersRequest.setDstInstances(dstInstances);
    processRoutersRequest.setSourceService(srcSourceInfo);
    processRoutersRequest.setRouters(routerNamesGroup);
    ProcessRoutersResponse processRoutersResponse = PolarisAPIFactory.getRouterApi().processRouters(processRoutersRequest);
    LOGGER.info("success to route by Polaris with instance size:{}", processRoutersResponse.getServiceInstances().getInstances().size());
    return processRoutersResponse.getServiceInstances();
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) PolarisAgentProperties(cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList)

Example 8 with ServiceInfo

use of com.tencent.polaris.api.pojo.ServiceInfo in project spring-cloud-tencent by Tencent.

the class PolarisDiscoveryHandler method getFilteredInstances.

/**
 * 获取服务路由后的实例列表
 *
 * @param service 服务名
 * @return 服务实例列表
 */
public InstancesResponse getFilteredInstances(String service) {
    String namespace = polarisProperties.getNamespace();
    GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
    getInstancesRequest.setNamespace(namespace);
    getInstancesRequest.setService(service);
    String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
    getInstancesRequest.setMethod(method);
    String localNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
    String localService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
    Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
    if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService) || null != allTransitiveCustomMetadata) {
        ServiceInfo sourceService = new ServiceInfo();
        sourceService.setNamespace(localNamespace);
        sourceService.setService(localService);
        sourceService.setMetadata(allTransitiveCustomMetadata);
        getInstancesRequest.setServiceInfo(sourceService);
    }
    return polarisConsumer.getInstances(getInstancesRequest);
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest)

Example 9 with ServiceInfo

use of com.tencent.polaris.api.pojo.ServiceInfo in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRule method buildProcessRoutersRequest.

ProcessRoutersRequest buildProcessRoutersRequest(ServiceInstances serviceInstances, Object key) {
    ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
    processRoutersRequest.setDstInstances(serviceInstances);
    // metadata router
    if (polarisMetadataRouterProperties.isEnabled()) {
        Map<String, String> transitiveLabels = getRouterLabels(key, PolarisRouterContext.TRANSITIVE_LABELS);
        processRoutersRequest.putRouterMetadata(MetadataRouter.ROUTER_TYPE_METADATA, transitiveLabels);
    }
    // nearby router
    if (polarisNearByRouterProperties.isEnabled()) {
        Map<String, String> nearbyRouterMetadata = new HashMap<>();
        nearbyRouterMetadata.put(NearbyRouter.ROUTER_ENABLED, "true");
        processRoutersRequest.putRouterMetadata(NearbyRouter.ROUTER_TYPE_NEAR_BY, nearbyRouterMetadata);
    }
    // rule based router
    // set dynamic switch for rule based router
    boolean ruleBasedRouterEnabled = polarisRuleBasedRouterProperties.isEnabled();
    Map<String, String> ruleRouterMetadata = new HashMap<>();
    ruleRouterMetadata.put(RuleBasedRouter.ROUTER_ENABLED, String.valueOf(ruleBasedRouterEnabled));
    processRoutersRequest.putRouterMetadata(RuleBasedRouter.ROUTER_TYPE_RULE_BASED, ruleRouterMetadata);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(MetadataContext.LOCAL_NAMESPACE);
    serviceInfo.setService(MetadataContext.LOCAL_SERVICE);
    if (ruleBasedRouterEnabled) {
        Map<String, String> ruleRouterLabels = getRouterLabels(key, PolarisRouterContext.RULE_ROUTER_LABELS);
        // The label information that the rule based routing depends on
        // is placed in the metadata of the source service for transmission.
        // Later, can consider putting it in routerMetadata like other routers.
        serviceInfo.setMetadata(ruleRouterLabels);
    }
    processRoutersRequest.setSourceService(serviceInfo);
    return processRoutersRequest;
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) HashMap(java.util.HashMap) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)

Example 10 with ServiceInfo

use of com.tencent.polaris.api.pojo.ServiceInfo in project spring-cloud-tencent by Tencent.

the class PolarisServiceStatusChangeListenerTest method testOnResourceUpdated.

@Test
public void testOnResourceUpdated() {
    PolarisServiceStatusChangeListener polarisServiceStatusChangeListener = new PolarisServiceStatusChangeListener();
    polarisServiceStatusChangeListener.setApplicationEventPublisher(publisher);
    // Service update event
    ServiceEventKey serviceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.SERVICE);
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setNamespace(NAMESPACE_TEST);
    serviceInfo.setService(SERVICE_PROVIDER);
    // Need update
    ServicesByProto oldServices = new ServicesByProto(Collections.emptyList());
    ServicesByProto newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // No need update
    oldServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    newServices = new ServicesByProto(Collections.singletonList(serviceInfo));
    polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldServices, newServices);
    verify(publisher, times(1)).publishEvent(any(ApplicationEvent.class));
    // Instance update event
    ServiceEventKey instanceUpdateEventKey = new ServiceEventKey(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER), ServiceEventKey.EventType.INSTANCE);
    DefaultInstance instance = new DefaultInstance();
    instance.setNamespace(NAMESPACE_TEST);
    instance.setService(SERVICE_PROVIDER);
    instance.setHost(HOST);
    instance.setPort(PORT);
    try {
        Field instances = ServiceInstancesByProto.class.getDeclaredField("instances");
        instances.setAccessible(true);
        // Need update
        ServiceInstancesByProto oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.emptyList());
        ServiceInstancesByProto newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
        // No need update
        oldInstances = new ServiceInstancesByProto();
        instances.set(oldInstances, Collections.singletonList(instance));
        newInstances = new ServiceInstancesByProto();
        instances.set(newInstances, Collections.singletonList(instance));
        polarisServiceStatusChangeListener.onResourceUpdated(serviceUpdateEventKey, oldInstances, newInstances);
        verify(publisher, times(2)).publishEvent(any(ApplicationEvent.class));
    } catch (NoSuchFieldException | IllegalAccessException e) {
        Assertions.fail("Exception encountered.", e);
    }
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Field(java.lang.reflect.Field) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ApplicationEvent(org.springframework.context.ApplicationEvent) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto) Test(org.junit.Test)

Aggregations

ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)16 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)5 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)5 ArrayList (java.util.ArrayList)5 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)4 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)4 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)4 HashMap (java.util.HashMap)4 PolarisAgentProperties (cn.polarismesh.agent.core.spring.cloud.context.PolarisAgentProperties)3 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)3 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)3 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)2 Instance (com.tencent.polaris.api.pojo.Instance)2 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 Services (com.tencent.polaris.api.pojo.Services)2 ServicesByProto (com.tencent.polaris.client.pojo.ServicesByProto)2 Test (org.junit.Test)2 PolarisServiceInstance (cn.polarismesh.agent.core.spring.cloud.discovery.PolarisServiceInstance)1 ConsulException (com.ecwid.consul.ConsulException)1