use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.
the class XhashSender method asyncSend.
@Override
protected void asyncSend() {
String group = unitRequest.getContext().getGroup(), unit = unitRequest.getContext().getUnit();
try {
List<UnitInstance> unitInstances = UnitRouter.singleton.allInstances(Unit.fullName(group, unit));
List<String> clientIds = new ArrayList<>();
for (UnitInstance clientInfo : unitInstances) {
clientIds.add(clientInfo.getNodeId());
}
String[] xhashNames = unitInstances.get(0).getPayload().getInput().getXhashNames();
// sorting is for constant-hash requirement.
Collections.sort(clientIds);
String clientId = new Shard<>(clientIds).getShardInfo(xhashString(xhashNames));
if (clientId.equals(LocalNodeManager.LOCAL_NODE_ID)) {
new RoutedLocalAsyncSender(unitRequest, callback).send();
} else {
unitRequest.getContext().setDestinationNodeId(clientId);
LocalNodeManager.send(unitRequest, callback);
}
} catch (UnitOfflineException | UnitUndefinedException e) {
LOG.error("代码写错了吧? 进入xhashSender的前提就是unit在线!", e);
callback.callback(UnitResponse.exception(e));
}
}
use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.
the class UnitRegistrationBridge method unitInstance.
static UnitInstance unitInstance(UnitProxy unitProxy, NodeStatus nodeStatus) {
UnitInstance unitInstance = new UnitInstance();
unitInstance.setPayload(UnitProxy.create(unitProxy));
unitInstance.setRegistrationTimestamp(nodeStatus.getInitTime());
unitInstance.setPort(nodeStatus.getPort());
unitInstance.setName(Unit.fullName(unitProxy));
unitInstance.setEnabled(true);
unitInstance.setAddress(nodeStatus.getHost());
unitInstance.setUnitInstanceIdBean(new UnitInstanceIdBean(Unit.fullName(unitProxy), nodeStatus.getNodeId()));
return unitInstance;
}
use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.
the class UnitRegistrationBridge method execute.
@Override
public UnitResponse execute(UnitRequest msg) {
UnitProxy unitProxy = msg.get("unit", UnitProxy.class);
NodeStatus nodeStatus = msg.get("nodeStatus", NodeStatus.class);
UnitInstance unitInstance = unitInstance(unitProxy, nodeStatus);
try {
UnitDiscovery.singleton.register(unitInstance);
return UnitResponse.success();
} catch (Exception e) {
LOG.error(e);
return UnitResponse.failure(null, "registration failed.");
}
}
use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.
the class UnitRouter method firstInstance.
@Override
public UnitInstance firstInstance(String fullUnitName) throws UnitUndefinedException, UnitOfflineException {
UnitInstance instance = localInstance(fullUnitName);
if (instance != null)
return instance;
newestDefinition(fullUnitName);
/*Constant.SYSTEM_DAO_GROUP_NAME.concat("."))) {
List<UnitInstance> mappedInstances = allInstances(fullUnitName);
instance = mappedInstances.isEmpty() ? null : mappedInstances.get(0);
} else*/
if (UnitDiscovery.singleton != null)
instance = UnitDiscovery.singleton.firstInstance(fullUnitName);
else {
// service discovery(zk) is optional plugin, we shouldn't depend on it.
// Service discovery is disabled currently, use local single node mode.
}
if (instance == null)
throw new UnitOfflineException(fullUnitName);
return instance;
}
use of info.xiancloud.core.distribution.service_discovery.UnitInstance in project xian by happyyangyuan.
the class UnitRouter method localInstance.
public UnitInstance localInstance(String fullUnitName) {
Unit localUnit = LocalUnitsManager.getLocalUnit(fullUnitName);
if (localUnit != null) {
// 本地有,就直接用即可
UnitInstance unitInstance = new UnitInstance();
unitInstance.setPayload(UnitProxy.create(localUnit));
unitInstance.setRegistrationTimestamp(LocalNodeManager.singleton.getSimpleStatus().getInitTime());
unitInstance.setPort(Node.RPC_PORT);
unitInstance.setName(fullUnitName);
unitInstance.setEnabled(true);
unitInstance.setAddress(EnvUtil.getLocalIp());
unitInstance.setId(new UnitInstanceIdBean(fullUnitName, LocalNodeManager.LOCAL_NODE_ID).getUnitInstanceId());
return unitInstance;
}
return null;
}
Aggregations