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;
}
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);
}
}
}
}
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);
}
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;
}
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;
}
Aggregations