use of j.service.server.ServiceBase in project JFramework by gugumall.
the class JRouterImpl method getAllServiceNodeAvailable.
/*
* (non-Javadoc)
* @see j.service.router.JRouter#getAllServiceNode(java.lang.String, java.lang.String, java.lang.String)
*/
public ServiceBase[] getAllServiceNodeAvailable(String clientUuid, String clusterCode, String md54Routing) throws RemoteException {
waitWhileMonitoring();
if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
// is public, nothing to do
} else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
Client client = routerConfig.getClient(clientUuid);
if (client == null) {
log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_DEBUG);
throw new RemoteException(Constants.AUTH_FAILED);
}
String md5 = "";
md5 += clientUuid;
md5 += client.getKey();
md5 = JUtilMD5.MD5EncodeToHex(md5);
if (!md5.equalsIgnoreCase(md54Routing)) {
throw new RemoteException(Constants.AUTH_FAILED);
}
} else {
// 未实现的隐私策略
throw new RemoteException(Constants.AUTH_FAILED);
}
ConcurrentMap servantsOfCluster = (ConcurrentMap) servantsOfClusters.get(clusterCode);
if (servantsOfCluster == null) {
throw new RemoteException("the service(rmi) " + clusterCode + " is not found.");
}
List nodes = servantsOfCluster.listValues();
if (nodes.size() == 0) {
throw new RemoteException("no valid node for the service(rmi) " + clusterCode + ".");
}
return (ServiceBase[]) nodes.toArray(new ServiceBase[nodes.size()]);
}
use of j.service.server.ServiceBase in project JFramework by gugumall.
the class JRouterImpl method service.
/*
* (non-Javadoc)
* @see j.service.router.JRouter#service(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
*/
public ServiceBase service(String clientUuid, String clusterCode, String md54Routing) throws RemoteException {
waitWhileMonitoring();
if (Constants.PRIVICY_PUBLIC.equalsIgnoreCase(routerConfig.getPrivacy())) {
// is public, nothing to do
} else if (Constants.PRIVICY_MD5.equalsIgnoreCase(routerConfig.getPrivacy())) {
Client client = routerConfig.getClient(clientUuid);
if (client == null) {
log.log("client " + clientUuid + " is not exists.", Logger.LEVEL_DEBUG);
throw new RemoteException(Constants.AUTH_FAILED);
}
String md5 = "";
md5 += clientUuid;
md5 += client.getKey();
md5 = JUtilMD5.MD5EncodeToHex(md5);
if (!md5.equalsIgnoreCase(md54Routing)) {
throw new RemoteException(Constants.AUTH_FAILED);
}
} else {
// 未实现的隐私策略
throw new RemoteException(Constants.AUTH_FAILED);
}
String codeOfUuid = null;
for (int i = 0; i < nodeInfoList.size(); i++) {
// 按uuid获得
String[] node = ((String) nodeInfoList.get(i)).split(",");
if (node[0].equals(clusterCode)) {
codeOfUuid = node[4];
break;
}
}
ConcurrentMap servantsOfCluster = (ConcurrentMap) servantsOfClusters.get(codeOfUuid == null ? clusterCode : codeOfUuid);
if (servantsOfCluster == null) {
throw new RemoteException("the service(rmi) " + clusterCode + " is not found.");
}
List nodes = servantsOfCluster.listValues();
if (nodes.size() == 0) {
throw new RemoteException("no valid node for the service(rmi) " + clusterCode + ".");
}
if (servantsOfCluster.containsKey(clusterCode)) {
// 按uuid获得
nodes.clear();
nodes = null;
return (ServiceBase) servantsOfCluster.get(clusterCode);
} else {
ServiceBase servant = (ServiceBase) nodes.get(JUtilRandom.nextInt(nodes.size()));
nodes.clear();
nodes = null;
return servant;
}
}
use of j.service.server.ServiceBase in project JFramework by gugumall.
the class RouterAgent method serviceNodesRmi.
/**
* RouterManager通过均衡策略选择本路由节点代理,调用此方法从关联路由节点获得服务入口
* @param clientUuid
* @param code
* @param md54Routing
* @return
* @throws Exception
*/
protected ServiceBase[] serviceNodesRmi(String clientUuid, String code, String md54Routing) throws Exception {
String cacheKey = "rmi|" + clientUuid + "|" + code + "|nodes";
if (cache.containsKey(cacheKey)) {
return (ServiceBase[]) cache.get(cacheKey);
}
if (this.routerConfig.getRmi() == null) {
throw new Exception("router(rmi) is not supported.");
}
if (!routerRmiAvailable || servant == null) {
throw new Exception("router(rmi) " + routerConfig.getUuid() + " is unavailable.");
}
ServiceBase[] obj = servant.getAllServiceNodeAvailable(clientUuid, code, md54Routing);
cache.put(cacheKey, obj);
return obj;
}
use of j.service.server.ServiceBase in project JFramework by gugumall.
the class Client method httpGetService.
/**
* 获得服务入口
* @param code
* @return
* @throws RemoteException
*/
public static String httpGetService(JHttp jhttp, HttpClient client, String code, boolean local) throws Exception {
boolean callLocal = local ? true : false;
if (!local) {
if ("true".equalsIgnoreCase(Client.callLocalServiceIfExists))
callLocal = true;
else if ("random".equalsIgnoreCase(Client.callLocalServiceIfExists)) {
int x = JUtilRandom.nextInt(3);
if (x == 0)
callLocal = true;
}
}
if (callLocal) {
// 调用本地服务
ServiceContainer container = ServiceManager.getServiceContainer(code);
if (container != null) {
ServiceBase servant = container.getServantOfService(code);
if (servant != null) {
return servant.getServiceConfig().getHttp().getEntrance();
}
}
}
if (local)
return null;
String md54Routing = "";
md54Routing += Manager.getClientNodeUuid();
md54Routing += Manager.getClientKeyToRouter();
md54Routing = JUtilMD5.MD5EncodeToHex(md54Routing);
String entrance = RouterManager.serviceHttp(jhttp, client, Manager.getClientNodeUuid(), code, md54Routing);
if (Constants.AUTH_FAILED.equals(entrance) || Constants.SERVICE_NOT_FOUND.equals(entrance) || Constants.SERVICE_NOT_AVAIL.equals(entrance)) {
throw new Exception("无可用服务(http) - " + entrance + " - " + code);
}
if (entrance == null || !entrance.startsWith("http")) {
throw new Exception("无可用服务(http) - " + entrance + " - " + code);
}
return entrance;
}
use of j.service.server.ServiceBase in project JFramework by gugumall.
the class Client method rmiGetService.
/**
* 获得服务入口
* @param code
* @param local
* @return
* @throws Exception
*/
public static ServiceBase rmiGetService(String code, boolean local) throws Exception {
boolean callLocal = local ? true : false;
if (!local) {
if ("true".equalsIgnoreCase(Client.callLocalServiceIfExists))
callLocal = true;
else if ("random".equalsIgnoreCase(Client.callLocalServiceIfExists)) {
int x = JUtilRandom.nextInt(3);
if (x == 0)
callLocal = true;
}
}
if (callLocal) {
// 调用本地服务
ServiceContainer container = ServiceManager.getServiceContainer(code);
if (container != null) {
ServiceBase servant = container.getServantOfService(code);
if (servant != null) {
return servant;
}
}
}
if (local)
return null;
String md54Routing = "";
md54Routing += Manager.getClientNodeUuid();
md54Routing += Manager.getClientKeyToRouter();
md54Routing = JUtilMD5.MD5EncodeToHex(md54Routing);
return (ServiceBase) RouterManager.serviceRmi(Manager.getClientNodeUuid(), code, md54Routing);
}
Aggregations