Search in sources :

Example 1 with Node

use of com.tencent.polaris.client.pojo.Node in project polaris-java by polarismesh.

the class ChangeStateUtils method getInstance.

/**
 * 获取实例ID
 *
 * @param instanceGauge 实例统计数据
 * @param localRegistry 本地缓存插件
 * @return ID
 */
public static InstanceByProto getInstance(InstanceGauge instanceGauge, LocalRegistry localRegistry) {
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(instanceGauge.getNamespace(), instanceGauge.getService()), EventType.INSTANCE);
    ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
    ServiceInstances instances = localRegistry.getInstances(resourceFilter);
    if (!instances.isInitialized()) {
        return null;
    }
    ServiceInstancesByProto serviceInstancesByProto = (ServiceInstancesByProto) instances;
    Instance instance = instanceGauge.getInstance();
    if (instance instanceof InstanceByProto) {
        return (InstanceByProto) instance;
    }
    InstanceByProto instanceByProto;
    String instanceId = instanceGauge.getInstanceId();
    if (StringUtils.isNotBlank(instanceId)) {
        instanceByProto = serviceInstancesByProto.getInstance(instanceId);
    } else {
        Node node = new Node(instanceGauge.getHost(), instanceGauge.getPort());
        instanceByProto = serviceInstancesByProto.getInstanceByNode(node);
    }
    if (null != instanceByProto) {
        instanceGauge.setInstance(instanceByProto);
    }
    return instanceByProto;
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) Instance(com.tencent.polaris.api.pojo.Instance) Node(com.tencent.polaris.client.pojo.Node) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Example 2 with Node

use of com.tencent.polaris.client.pojo.Node in project polaris-java by polarismesh.

the class ConsumerTest method before.

@Before
public void before() {
    try {
        namingServer = NamingServer.startNamingServer(10081);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    for (ValidParam validParam : validParams.values()) {
        InstanceParameter instanceParameter = new InstanceParameter();
        instanceParameter.setHealthy(true);
        instanceParameter.setIsolated(false);
        instanceParameter.setWeight(100);
        ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, validParam.getServiceName());
        List<Node> nodes = namingServer.getNamingService().batchAddInstances(serviceKey, 10000, validParam.getCountAll(), instanceParameter);
        if (validParam.getCountAll() > validParam.getCountHealth()) {
            int abnormalCount = validParam.getCountAll() - validParam.getCountHealth();
            int unhealthyCount = abnormalCount / 2;
            int isolatedCount = abnormalCount - unhealthyCount;
            for (int i = 0; i < unhealthyCount; i++) {
                namingServer.getNamingService().setInstanceHealthyStatus(serviceKey, nodes.get(i), false, null, null);
            }
            for (int i = 0; i < isolatedCount; i++) {
                namingServer.getNamingService().setInstanceHealthyStatus(serviceKey, nodes.get(nodes.size() - 1 - i), null, true, null);
            }
        }
        if (validParam.getCountAll() > validParam.getCountHasWeight()) {
            int weightZeroCount = validParam.getCountAll() - validParam.getCountHasWeight();
            for (int i = 0; i < weightZeroCount; i++) {
                namingServer.getNamingService().setInstanceHealthyStatus(serviceKey, nodes.get(i), null, null, 0);
            }
        }
    }
}
Also used : InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) Node(com.tencent.polaris.client.pojo.Node) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) IOException(java.io.IOException) Before(org.junit.Before)

Example 3 with Node

use of com.tencent.polaris.client.pojo.Node in project polaris-java by polarismesh.

the class MetadataRouterTest method before.

@Before
public void before() {
    try {
        namingServer = NamingServer.startNamingServer(10081);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    /**
     * 该服务下有四个实例
     * 1. 127.0.0.1:80 不健康 Env-set:1-0
     * 2. 127.0.0.1:70 健康 Env-set:1-0
     * 3. 127.0.0.1:100 健康 Env-set:1-0
     * 4. 127.0.0.1:90 健康
     */
    ServiceKey serviceKey = new ServiceKey(NAMESPACE_PRODUCTION, METADATA_SERVICE);
    InstanceParameter parameter = new InstanceParameter();
    parameter.setWeight(100);
    parameter.setHealthy(false);
    parameter.setIsolated(false);
    Map<String, String> metadata = new HashMap<>();
    metadata.put("Env-set", "1-0");
    parameter.setMetadata(metadata);
    namingServer.getNamingService().addInstance(serviceKey, new Node("127.0.0.1", 80), parameter);
    parameter.setHealthy(true);
    namingServer.getNamingService().addInstance(serviceKey, new Node("127.0.0.1", 70), parameter);
    namingServer.getNamingService().addInstance(serviceKey, new Node("127.0.0.1", 100), parameter);
    parameter.setMetadata(null);
    namingServer.getNamingService().addInstance(serviceKey, new Node("127.0.0.1", 90), parameter);
    /**
     * 该服务下有两个实例
     * 1. 127.0.0.1:80 不健康 Env-set:1-0
     * 2. 127.0.0.1:81 不健康
     */
    ServiceKey serviceKey1 = new ServiceKey(NAMESPACE_PRODUCTION, METADATA_SERVICE_1);
    parameter.setMetadata(metadata);
    parameter.setHealthy(false);
    namingServer.getNamingService().addInstance(serviceKey1, new Node("127.0.0.1", 80), parameter);
    parameter.setMetadata(null);
    namingServer.getNamingService().addInstance(serviceKey1, new Node("127.0.0.1", 81), parameter);
}
Also used : HashMap(java.util.HashMap) InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) Node(com.tencent.polaris.client.pojo.Node) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) IOException(java.io.IOException) Before(org.junit.Before)

Example 4 with Node

use of com.tencent.polaris.client.pojo.Node in project polaris-java by polarismesh.

the class NamingServer method startNamingServer.

public static NamingServer startNamingServer(int port) throws IOException {
    NamingServer namingServer = new NamingServer(port);
    namingServer.start();
    Node node = new Node("127.0.0.1", port);
    InstanceParameter parameter = new InstanceParameter();
    parameter.setHealthy(true);
    parameter.setIsolated(false);
    parameter.setProtocol("grpc");
    parameter.setWeight(100);
    // 注册系统集群地址
    namingServer.getNamingService().addInstance(new ServiceKey("Polaris", "polaris.discover"), node, parameter);
    namingServer.getNamingService().addInstance(new ServiceKey("Polaris", "polaris.healthcheck"), node, parameter);
    return namingServer;
}
Also used : Node(com.tencent.polaris.client.pojo.Node) InstanceParameter(com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey)

Example 5 with Node

use of com.tencent.polaris.client.pojo.Node in project polaris-java by polarismesh.

the class NamingService method batchAddInstances.

/**
 * 批量增加服务实例
 *
 * @param svcKey 服务名
 * @param portStart 起始端口
 * @param instCount 实例数
 * @param parameter 实例参数
 * @return 批量服务实例的IP和端口
 */
public List<Node> batchAddInstances(ServiceKey svcKey, int portStart, int instCount, InstanceParameter parameter) {
    List<Node> nodes = new ArrayList<>();
    List<Instance> instances = new ArrayList<>();
    for (int i = 0; i < instCount; i++) {
        Node node = new Node("127.0.0.1", portStart + i);
        ServiceProto.Instance nextInstance = buildInstance(svcKey, node, parameter);
        instances.add(nextInstance);
        nodes.add(node);
    }
    List<Instance> existsInstances = services.get(svcKey);
    if (null == existsInstances) {
        services.put(svcKey, instances);
    } else {
        existsInstances.addAll(instances);
    }
    return nodes;
}
Also used : Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) Node(com.tencent.polaris.client.pojo.Node) ArrayList(java.util.ArrayList) Instance(com.tencent.polaris.client.pb.ServiceProto.Instance) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Aggregations

Node (com.tencent.polaris.client.pojo.Node)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)4 InstanceParameter (com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter)3 IOException (java.io.IOException)2 Before (org.junit.Before)2 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)1 Instance (com.tencent.polaris.api.pojo.Instance)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)1 Instance (com.tencent.polaris.client.pb.ServiceProto.Instance)1 InstanceByProto (com.tencent.polaris.client.pojo.InstanceByProto)1 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1