use of com.tencent.polaris.api.pojo.Instance 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());
}
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class SyncFlow method commonSyncGetOneInstance.
/**
* 获取单个实例
*
* @param request 请求对象
* @return 实例应答
* @throws PolarisException 异常
*/
public InstancesResponse commonSyncGetOneInstance(CommonInstancesRequest request) throws PolarisException {
syncGetServiceInstances(request);
ServiceInstances dstInstances = request.getDstInstances();
if (CollectionUtils.isEmpty(dstInstances.getInstances())) {
return new InstancesResponse(dstInstances);
}
ServiceInstances routerInstances = BaseFlow.processServiceRouters(request.getRouteInfo(), request.getDstInstances(), extensions.getConfigRouterChainGroup());
LoadBalancer loadBalancer = extensions.getLoadBalancer();
Instance instance = BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), routerInstances);
return new InstancesResponse(dstInstances, instance);
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class Utils method checkAddInstances.
public static List<Instance> checkAddInstances(ServiceInstancesByProto oldVal, ServiceInstancesByProto newVal) {
Set<Instance> oldIns = new HashSet<>(oldVal.getInstances());
Set<Instance> newIns = new HashSet<>(newVal.getInstances());
List<Instance> ret = new LinkedList<>();
for (Instance instance : newIns) {
if (!oldIns.contains(instance)) {
ret.add(instance);
}
}
return ret;
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class NearbyRouter method router.
@Override
public RouteResult router(RouteInfo routeInfo, ServiceInstances serviceInstances) throws PolarisException {
// 先获取最低可用就近级别
LocationLevel minAvailableLevel = config.getMatchLevel();
if (null == minAvailableLevel) {
minAvailableLevel = defaultMinLevel;
}
LocationLevel minLevel = minAvailableLevel;
if (null != routeInfo.getNextRouterInfo()) {
if (null != routeInfo.getNextRouterInfo().getLocationLevel()) {
minLevel = routeInfo.getNextRouterInfo().getLocationLevel();
}
if (null != routeInfo.getNextRouterInfo().getMinAvailableLevel()) {
minAvailableLevel = routeInfo.getNextRouterInfo().getMinAvailableLevel();
}
}
LocationLevel maxLevel = config.getMaxMatchLevel();
if (null == maxLevel) {
maxLevel = LocationLevel.all;
}
Map<LocationLevel, String> clientLocationInfo = locationInfo.get();
if (minLevel.ordinal() >= maxLevel.ordinal()) {
List<Instance> instances = selectInstances(serviceInstances, minAvailableLevel, clientLocationInfo);
if (CollectionUtils.isEmpty(instances)) {
throw new PolarisException(ErrorCode.LOCATION_MISMATCH, String.format("can not find any instance by level %s", minLevel.name()));
}
// 已经循环了一圈
return new RouteResult(selectInstances(serviceInstances, minAvailableLevel, clientLocationInfo), RouteResult.State.Next);
}
CheckResult checkResult = new CheckResult();
for (int i = minLevel.ordinal(); i <= maxLevel.ordinal(); i++) {
LocationLevel curLevel = LocationLevel.values()[i];
checkResult = hasHealthyInstances(serviceInstances, routeInfo.getStatusDimensions(), curLevel, clientLocationInfo);
checkResult.curLevel = curLevel;
if (!CollectionUtils.isEmpty(checkResult.instances)) {
break;
} else {
minAvailableLevel = curLevel;
}
}
if (CollectionUtils.isEmpty(checkResult.instances)) {
throw new PolarisException(ErrorCode.LOCATION_MISMATCH, String.format("can not find any instance by level %s", checkResult.curLevel.name()));
}
if (!config.isEnableDegradeByUnhealthyPercent() || checkResult.curLevel == LocationLevel.all) {
return new RouteResult(checkResult.instances, RouteResult.State.Next);
}
int healthyInstanceCount = checkResult.healthyInstanceCount;
double actualHealthyPercent = (double) healthyInstanceCount / (double) serviceInstances.getInstances().size();
if (actualHealthyPercent <= healthyPercentToDegrade) {
LOG.debug("[shouldDegrade] enableDegradeByUnhealthyPercent = {},unhealthyPercentToDegrade={}," + "healthyPercent={},isStrict={},matchLevel={}", config.isEnableDegradeByUnhealthyPercent(), config.getUnhealthyPercentToDegrade(), actualHealthyPercent, config.isStrictNearby(), checkResult.curLevel);
RouteResult result = new RouteResult(checkResult.instances, RouteResult.State.Retry);
result.getNextRouterInfo().setLocationLevel(nextLevel(checkResult.curLevel));
result.getNextRouterInfo().setMinAvailableLevel(minAvailableLevel);
return result;
}
return new RouteResult(checkResult.instances, RouteResult.State.Next);
}
use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.
the class NearbyRouter method selectInstances.
private List<Instance> selectInstances(ServiceInstances svcInstances, LocationLevel targetLevel, Map<LocationLevel, String> clientInfo) {
List<Instance> instances = new ArrayList<>();
String clientZone = "";
String clientRegion = "";
String clientCampus = "";
if (null != clientInfo) {
clientZone = clientInfo.get(LocationLevel.zone);
clientRegion = clientInfo.get(LocationLevel.region);
clientCampus = clientInfo.get(LocationLevel.campus);
}
for (Instance instance : svcInstances.getInstances()) {
switch(targetLevel) {
case zone:
if (clientZone.equals("") || clientZone.equals(instance.getZone())) {
instances.add(instance);
}
break;
case campus:
if (clientCampus.equals("") || clientCampus.equals(instance.getCampus())) {
instances.add(instance);
}
break;
case region:
if (clientRegion.equals("") || clientRegion.equals(instance.getRegion())) {
instances.add(instance);
}
break;
default:
instances.add(instance);
break;
}
}
return instances;
}
Aggregations