Search in sources :

Example 1 with GroupInstance

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);
}
Also used : GroupOfflineException(info.xiancloud.core.distribution.exception.GroupOfflineException) GroupInstance(info.xiancloud.core.distribution.service_discovery.GroupInstance)

Example 2 with GroupInstance

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;
}
Also used : GroupOfflineException(info.xiancloud.core.distribution.exception.GroupOfflineException) GroupInstance(info.xiancloud.core.distribution.service_discovery.GroupInstance)

Example 3 with GroupInstance

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;
        }
    });
}
Also used : GroupInstanceIdBean(info.xiancloud.core.distribution.service_discovery.GroupInstanceIdBean) GroupInstance(info.xiancloud.core.distribution.service_discovery.GroupInstance)

Example 4 with GroupInstance

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;
}
Also used : HashMap(java.util.HashMap) GroupOfflineException(info.xiancloud.core.distribution.exception.GroupOfflineException) GroupUndefinedException(info.xiancloud.core.distribution.exception.GroupUndefinedException) DiyMonitorGroup(info.xiancloud.core.support.falcon.DiyMonitorGroup) JSONObject(com.alibaba.fastjson.JSONObject) UnitResponse(info.xiancloud.core.message.UnitResponse) GroupInstance(info.xiancloud.core.distribution.service_discovery.GroupInstance) HashSet(java.util.HashSet)

Example 5 with GroupInstance

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);
}
Also used : GroupOfflineException(info.xiancloud.core.distribution.exception.GroupOfflineException) GroupInstance(info.xiancloud.core.distribution.service_discovery.GroupInstance)

Aggregations

GroupInstance (info.xiancloud.core.distribution.service_discovery.GroupInstance)7 GroupOfflineException (info.xiancloud.core.distribution.exception.GroupOfflineException)4 GroupInstanceIdBean (info.xiancloud.core.distribution.service_discovery.GroupInstanceIdBean)2 JSONObject (com.alibaba.fastjson.JSONObject)1 GroupProxy (info.xiancloud.core.distribution.GroupProxy)1 NodeStatus (info.xiancloud.core.distribution.NodeStatus)1 GroupUndefinedException (info.xiancloud.core.distribution.exception.GroupUndefinedException)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 DiyMonitorGroup (info.xiancloud.core.support.falcon.DiyMonitorGroup)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1