use of com.tencent.polaris.api.rpc.GetOneInstanceRequest in project polaris-java by polarismesh.
the class MetadataRouterTest method testFailoverAllScene.
@Test
public void testFailoverAllScene() {
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
// 但是不健康,降级策略采用默认返回所有健康实例,其中80端口实例为不健康,故80端口的实例不会返回
for (int i = 0; i < 10; i++) {
GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
getInstances2.setNamespace("Production");
getInstances2.setService(METADATA_SERVICE);
Map<String, String> map = new HashMap<>();
map.put("Env-set", "1-1");
getInstances2.setMetadata(map);
getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERALL);
InstancesResponse ins = null;
try {
ins = consumer.getOneInstance(getInstances2);
} catch (PolarisException e) {
e.printStackTrace();
}
Assert.assertNotEquals(80, ins.getInstances()[0].getPort());
}
}
}
use of com.tencent.polaris.api.rpc.GetOneInstanceRequest in project polaris-java by polarismesh.
the class MetadataRouterTest method testFailoverNotKeyScene.
@Test
public void testFailoverNotKeyScene() {
// 传入Env-set:1-1 ,应该返回第4个实例,因为前三个都包含Env-set这个key
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
for (int i = 0; i < 10; i++) {
GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
getInstances2.setNamespace("Production");
getInstances2.setService(METADATA_SERVICE);
Map<String, String> map = new HashMap<>();
map.put("Env-set", "1-1");
getInstances2.setMetadata(map);
// TODO: 通过配置文件来设置该配置
getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
InstancesResponse ins = null;
try {
ins = consumer.getOneInstance(getInstances2);
} catch (PolarisException e) {
Assert.fail(e.getMessage());
}
Assert.assertEquals(90, ins.getInstances()[0].getPort());
Assert.assertEquals("127.0.0.1", ins.getInstances()[0].getHost());
}
}
}
use of com.tencent.polaris.api.rpc.GetOneInstanceRequest 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));
}
use of com.tencent.polaris.api.rpc.GetOneInstanceRequest in project polaris-java by polarismesh.
the class Consumer method invokeByNameResolution.
private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
System.out.println("namespace " + namespace + ", service " + service);
// 1. we need to do naming resolution to get a load balanced host and port
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setNamespace(namespace);
getOneInstanceRequest.setService(service);
InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
Instance[] instances = oneInstance.getInstances();
System.out.println("instances count is " + instances.length);
Instance targetInstance = instances[0];
System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
// 2. invoke the server by the resolved address
String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
long startMillis = System.currentTimeMillis();
HttpResult httpResult = httpGet(urlStr);
long delay = System.currentTimeMillis() - startMillis;
System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
// 3. report the invoke result to polaris-java, to eliminate the fail address
RetStatus status = RetStatus.RetSuccess;
if (httpResult.code != 200) {
status = RetStatus.RetFail;
}
ServiceCallResult result = new ServiceCallResult();
result.setNamespace(namespace);
result.setService(service);
result.setHost(targetInstance.getHost());
result.setPort(targetInstance.getPort());
result.setRetCode(httpResult.code);
result.setDelay(delay);
result.setRetStatus(status);
consumerAPI.updateServiceCallResult(result);
System.out.println("success to call updateServiceCallResult");
return httpResult.message;
}
use of com.tencent.polaris.api.rpc.GetOneInstanceRequest in project polaris-java by polarismesh.
the class GetOneInstanceExample method main.
public static void main(String[] args) throws Exception {
InitResult initResult = ExampleUtils.initConsumerConfiguration(args, false);
String namespace = initResult.getNamespace();
String service = initResult.getService();
try (ConsumerAPI consumerAPI = ExampleUtils.createConsumerAPI(initResult.getConfig())) {
System.out.println("namespace " + namespace + ", service " + service);
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setNamespace(namespace);
getOneInstanceRequest.setService(service);
InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
Instance[] instances = oneInstance.getInstances();
System.out.println("instances count is " + instances.length);
Instance targetInstance = instances[0];
System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
}
}
Aggregations