use of info.xiancloud.core.distribution.service_discovery.GroupInstance in project xian by happyyangyuan.
the class GroupRouter method loadBalancedInstance.
public GroupInstance loadBalancedInstance(String groupName) throws GroupOfflineException, GroupUndefinedException {
GroupInstance instance = localInstance(groupName);
if (instance != null)
return instance;
newestDefinition(groupName);
if (GroupDiscovery.singleton != null)
instance = GroupDiscovery.singleton.lb(groupName);
if (instance != null)
return instance;
throw new GroupOfflineException(groupName);
}
use of info.xiancloud.core.distribution.service_discovery.GroupInstance in project xian by happyyangyuan.
the class GroupRouter method allInstances.
public List<GroupInstance> allInstances(String groupName) throws GroupOfflineException, GroupUndefinedException {
newestDefinition(groupName);
List<GroupInstance> instances;
if (GroupDiscovery.singleton != null)
instances = GroupDiscovery.singleton.all(groupName);
else {
instances = new ArrayList<>();
instances.add(localInstance(groupName));
}
if (instances.isEmpty())
throw new GroupOfflineException(groupName);
/*instances.sort(Comparator.comparing(GroupInstance::getNodeId));
we do not sort any more, sort by your self if you need consistent hash.
*/
return instances;
}
use of info.xiancloud.core.distribution.service_discovery.GroupInstance in project xian by happyyangyuan.
the class GroupRouter method localInstance.
@Override
public GroupInstance localInstance(String groupName) {
return LocalUnitsManager.unitMap(unitMap -> {
if (unitMap.containsKey(groupName)) {
GroupInstance serviceInstance = new GroupInstance();
serviceInstance.setRegistrationTimestamp(LocalNodeManager.singleton.getFullStatus().getInitTime());
serviceInstance.setPort(Node.RPC_PORT);
serviceInstance.setName(groupName);
serviceInstance.setEnabled(true);
serviceInstance.setAddress(EnvUtil.getLocalIp());
serviceInstance.setId(new GroupInstanceIdBean(groupName, LocalNodeManager.LOCAL_NODE_ID).getGroupInstanceId());
serviceInstance.setPayload(GroupProxy.create(LocalUnitsManager.getGroupByName(groupName)));
return serviceInstance;
} else {
return null;
}
});
}
use of info.xiancloud.core.distribution.service_discovery.GroupInstance in project xian by happyyangyuan.
the class FactorCollector method collect.
public static JSONObject collect() {
JSONObject factorOriented = new JSONObject();
String diyMonitorServiceName = new DiyMonitorGroup().getName();
Set<String> diyMonitorUnitNames = new HashSet<>();
try {
for (GroupInstance serviceInstance : GroupRouter.singleton.allInstances(diyMonitorServiceName)) {
diyMonitorUnitNames.addAll(serviceInstance.getPayload().getUnitNames());
}
} catch (GroupOfflineException | GroupUndefinedException e) {
throw new RuntimeException(e);
}
for (String diyMonitorUnitName : diyMonitorUnitNames) {
UnitResponse o = SyncXian.call(diyMonitorServiceName, diyMonitorUnitName, new HashMap());
if (o.succeeded()) {
try {
LOG.debug("data可以是单纯的数字,也可以是json/jsonArray");
factorOriented.put(diyMonitorUnitName, o.getData());
} catch (Throwable e) {
LOG.error("收集指标 '" + diyMonitorUnitName + "' 时出现异常,返回-1作为指标值", e);
factorOriented.put(diyMonitorUnitName, ERROR_VALUE_NEGATIVE_1);
}
} else {
LOG.error("收集指标 '" + diyMonitorUnitName + "' 失败 ! 返回-1作为指标值 . 失败内容为 :" + o);
factorOriented.put(diyMonitorUnitName, ERROR_VALUE_NEGATIVE_1);
}
}
return factorOriented;
}
use of info.xiancloud.core.distribution.service_discovery.GroupInstance in project xian by happyyangyuan.
the class GroupRouter method firstInstance.
@Override
public GroupInstance firstInstance(String groupName) throws GroupOfflineException, GroupUndefinedException {
GroupInstance instance = localInstance(groupName);
if (instance != null)
return instance;
newestDefinition(groupName);
if (GroupDiscovery.singleton != null)
instance = GroupDiscovery.singleton.firstInstance(groupName);
if (instance != null)
return instance;
throw new GroupOfflineException(groupName);
}
Aggregations