Search in sources :

Example 6 with DefaultInstance

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

the class PolarisRoutingLoadBalancer method getReachableServers.

@Override
public List<Server> getReachableServers() {
    List<Server> allServers = super.getAllServers();
    if (CollectionUtils.isEmpty(allServers)) {
        return allServers;
    }
    ServiceInstances serviceInstances = null;
    if (allServers.get(0) instanceof PolarisServer) {
        serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
    } else {
        String serviceName;
        // notice the difference between different service registries
        if (StringUtils.isNotBlank(allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
            serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery();
        } else {
            serviceName = allServers.get(0).getMetaInfo().getAppName();
        }
        if (StringUtils.isBlank(serviceName)) {
            throw new IllegalStateException("PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
        }
        ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceName);
        List<Instance> instances = new ArrayList<>(8);
        for (Server server : allServers) {
            DefaultInstance instance = new DefaultInstance();
            instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE);
            instance.setService(serviceName);
            instance.setHealthy(server.isAlive());
            instance.setProtocol(server.getScheme());
            instance.setId(server.getId());
            instance.setHost(server.getHost());
            instance.setPort(server.getPort());
            instance.setZone(server.getZone());
            instance.setWeight(100);
            instances.add(instance);
        }
        serviceInstances = new DefaultServiceInstances(serviceKey, instances);
    }
    ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
    processRoutersRequest.setDstInstances(serviceInstances);
    String srcNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
    String srcService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
    Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
    String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
    processRoutersRequest.setMethod(method);
    if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setNamespace(srcNamespace);
        serviceInfo.setService(srcService);
        serviceInfo.setMetadata(transitiveCustomMetadata);
        processRoutersRequest.setSourceService(serviceInfo);
    }
    ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
    ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
    List<Server> filteredInstances = new ArrayList<>();
    for (Instance instance : filteredServiceInstances.getInstances()) {
        filteredInstances.add(new PolarisServer(serviceInstances, instance));
    }
    return filteredInstances;
}
Also used : Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer)

Example 7 with DefaultInstance

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

the class ConsulAPIConnector method syncGetServiceInstances.

@Override
public List<DefaultInstance> syncGetServiceInstances(ServiceUpdateTask serviceUpdateTask) {
    List<DefaultInstance> instanceList = new ArrayList<>();
    try {
        HealthServicesRequest request = HealthServicesRequest.newBuilder().setQueryParams(new QueryParams(ConsistencyMode.DEFAULT)).build();
        Response<List<HealthService>> response = this.consulClient.getHealthServices(serviceUpdateTask.getServiceEventKey().getService(), request);
        if (response.getValue() == null || response.getValue().isEmpty()) {
            return Collections.emptyList();
        }
        for (HealthService service : response.getValue()) {
            DefaultInstance instance = new DefaultInstance();
            instance.setId(service.getService().getId());
            instance.setService(service.getService().getService());
            instance.setHost(service.getService().getAddress());
            instance.setPort(service.getService().getPort());
            instanceList.add(instance);
        }
    } catch (ConsulException e) {
        throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get service instances of %s sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
    }
    return instanceList;
}
Also used : HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) ConsulException(com.ecwid.consul.ConsulException) HealthService(com.ecwid.consul.v1.health.model.HealthService) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) QueryParams(com.ecwid.consul.v1.QueryParams) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)7 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)4 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)4 ArrayList (java.util.ArrayList)4 Server (com.netflix.loadbalancer.Server)3 Instance (com.tencent.polaris.api.pojo.Instance)3 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)3 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)3 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ConsulException (com.ecwid.consul.ConsulException)1 QueryParams (com.ecwid.consul.v1.QueryParams)1 HealthServicesRequest (com.ecwid.consul.v1.health.HealthServicesRequest)1 HealthService (com.ecwid.consul.v1.health.model.HealthService)1 PolarisServer (com.tencent.cloud.polaris.pojo.PolarisServer)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 Services (com.tencent.polaris.api.pojo.Services)1 DiscoverResponse (com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse)1