Search in sources :

Example 6 with Client

use of j.service.Client 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()]);
}
Also used : ServiceBase(j.service.server.ServiceBase) ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) RemoteException(java.rmi.RemoteException)

Example 7 with Client

use of j.service.Client 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;
    }
}
Also used : ServiceBase(j.service.server.ServiceBase) ConcurrentMap(j.util.ConcurrentMap) List(java.util.List) ConcurrentList(j.util.ConcurrentList) Client(j.service.Client) HttpClient(org.apache.http.client.HttpClient) RemoteException(java.rmi.RemoteException)

Example 8 with Client

use of j.service.Client in project JFramework by gugumall.

the class ServiceManager method load.

/**
 * 初始化
 */
public static void load() {
    try {
        loading = true;
        servicesOfClusters.clear();
        List currentServices = new LinkedList();
        // 文件是否存在
        File file = new File(Properties.getConfigPath() + "service.server.xml");
        if (!file.exists()) {
            throw new Exception("找不到配置文件:" + file.getAbsolutePath());
        }
        Document document = JUtilDom4j.parse(Properties.getConfigPath() + "service.server.xml", "UTF-8");
        Element root = document.getRootElement();
        List servs = root.elements("service");
        for (int i = 0; i < servs.size(); i++) {
            Element serv = (Element) servs.get(i);
            List nodes = serv.elements("node");
            for (int n = 0; n < nodes.size(); n++) {
                Element nodeE = (Element) nodes.get(n);
                ServiceConfig service = new ServiceConfig();
                String _toString = "";
                // 公共信息
                service.setCode(serv.elementText("code"));
                service.setName(serv.elementText("name"));
                service.setPrivacy(serv.elementText("privacy"));
                _toString += serv.elementText("code");
                _toString += " - " + serv.elementText("name");
                _toString += " - " + serv.elementText("privacy");
                List serviceProps = serv.elements("property");
                for (int j = 0; j < serviceProps.size(); j++) {
                    Element pEle = (Element) serviceProps.get(j);
                    service.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                    _toString += " - {" + pEle.attributeValue("key") + "=" + pEle.attributeValue("value") + "}";
                }
                List methods = serv.elements("method");
                for (int j = 0; j < methods.size(); j++) {
                    Element methodEle = (Element) methods.get(j);
                    Method method = new Method();
                    method.setName(methodEle.elementText("name"));
                    method.setPrivacy(methodEle.elementText("privacy"));
                    _toString += " - [" + methodEle.elementText("name") + ";" + methodEle.elementText("privacy") + "]";
                    service.addMethod(method);
                }
                List clients = serv.elements("client");
                for (int j = 0; j < clients.size(); j++) {
                    Element clientEle = (Element) clients.get(j);
                    Client client = new Client();
                    client.setUuid(clientEle.attributeValue("uuid"));
                    client.setName(clientEle.attributeValue("name"));
                    client.setKey(clientEle.attributeValue("key"));
                    _toString += " - <" + clientEle.attributeValue("uuid") + ";" + clientEle.attributeValue("name") + ";" + clientEle.attributeValue("key") + ">";
                    service.addClient(client);
                }
                // 公共信息 END
                // 节点信息
                service.setServerUuid(nodeE.attributeValue("server-uuid"));
                _toString += " - " + nodeE.attributeValue("server-uuid");
                service.setUuid(nodeE.elementText("uuid"));
                _toString += " - " + nodeE.elementText("uuid");
                Element rmiEle = nodeE.element("rmi");
                if (rmiEle != null && "true".equalsIgnoreCase(rmiEle.attributeValue("available"))) {
                    Rmi rmi = new Rmi(Properties.getProperties("rmi"));
                    List props = rmiEle.elements("property");
                    for (int j = 0; j < props.size(); j++) {
                        Element pEle = (Element) props.get(j);
                        rmi.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                        _toString += " - <" + pEle.attributeValue("key") + "=" + pEle.attributeValue("value") + ">";
                    }
                    service.setRmi(rmi);
                }
                Element httpEle = nodeE.element("http");
                if (httpEle != null && "true".equalsIgnoreCase(httpEle.attributeValue("available"))) {
                    Http http = new Http();
                    List props = httpEle.elements("property");
                    for (int j = 0; j < props.size(); j++) {
                        Element pEle = (Element) props.get(j);
                        http.addConfig(pEle.attributeValue("key"), pEle.attributeValue("value"));
                        _toString += " - <" + pEle.attributeValue("key") + "=" + pEle.attributeValue("value") + ">";
                    }
                    service.setHttp(http);
                }
                service.setToString(_toString);
                // 节点信息 END
                currentServices.add(service);
                // 分组保存
                ConcurrentList ls = (ConcurrentList) servicesOfClusters.get(service.getCode());
                if (ls == null) {
                    ls = new ConcurrentList();
                    servicesOfClusters.put(service.getCode(), ls);
                }
                ls.add(service);
            }
        }
        root = null;
        document = null;
        // 停止已启动服务
        List olds = services.listValues();
        for (int i = 0; i < olds.size(); i++) {
            ServiceConfig service = (ServiceConfig) olds.get(i);
            ServiceContainer containter = (ServiceContainer) serviceContainers.get(service.getUuid());
            if (containter != null && containter.getStarted()) {
                ServiceConfig _new = null;
                for (int j = 0; j < currentServices.size(); j++) {
                    ServiceConfig s = (ServiceConfig) currentServices.get(j);
                    if (s.getUuid().equals(service.getUuid())) {
                        _new = s;
                        break;
                    }
                }
                if (_new != null && _new.equals(service)) {
                    log.log("重启服务(停止阶段)- " + service.getCode() + " - " + service.getName() + " 没有变动,不需要重启", -1);
                } else {
                    log.log("重启服务(停止阶段)- " + service.getCode() + " - " + service.getName() + " 有变动,需要重启,正尝试停止", -1);
                    containter.shutdown();
                }
            }
        }
        services.clear();
        serviceContainers.clear();
        // 处理最新服务
        for (int i = 0; i < currentServices.size(); i++) {
            ServiceConfig serviceConfig = (ServiceConfig) currentServices.get(i);
            // 保存服务,可通过uuid找到
            services.put(serviceConfig.getUuid(), serviceConfig);
            if (serviceConfig.getServerUuid().equals(Manager.getServerNodeUuid())) {
                // 本地服务才启动
                ServiceConfig _old = null;
                for (int j = 0; j < olds.size(); j++) {
                    ServiceConfig s = (ServiceConfig) olds.get(j);
                    if (s.getUuid().equals(serviceConfig.getUuid())) {
                        _old = s;
                        break;
                    }
                }
                if (_old != null && _old.equals(serviceConfig)) {
                    log.log("重启服务(启动阶段) - " + serviceConfig.getCode() + " - " + serviceConfig.getName() + " 没有变动,不需要重启", -1);
                } else {
                    if (_old != null) {
                        log.log("重启服务(启动阶段)- " + serviceConfig.getCode() + " - " + serviceConfig.getName() + " 有变动,需要重启,正尝试启动", -1);
                    } else {
                        log.log("启动服务 - " + serviceConfig.getCode() + " - " + serviceConfig.getName() + ",正尝试启动", -1);
                    }
                    // 启动服务
                    ServiceContainer container = new ServiceContainer(serviceConfig);
                    serviceContainers.put(serviceConfig.getUuid(), container);
                    serviceContainers.put(serviceConfig.getCode(), container);
                    container.startup();
                }
            }
        }
        currentServices.clear();
        currentServices = null;
        JUtilList.clear_AllNull(olds);
        // 处理最新服务 END
        // 配置文件最近修改时间
        File configFile = new File(Properties.getConfigPath() + "service.server.xml");
        configLastModified = configFile.lastModified();
        configFile = null;
        loading = false;
    } catch (Exception e) {
        loading = false;
        log.log(e, Logger.LEVEL_FATAL);
    }
}
Also used : Element(org.dom4j.Element) Http(j.service.Http) Document(org.dom4j.Document) LinkedList(java.util.LinkedList) Rmi(j.service.Rmi) ConcurrentList(j.util.ConcurrentList) List(java.util.List) ConcurrentList(j.util.ConcurrentList) JUtilList(j.util.JUtilList) LinkedList(java.util.LinkedList) Client(j.service.Client) File(java.io.File)

Aggregations

Client (j.service.Client)8 ConcurrentList (j.util.ConcurrentList)8 HttpClient (org.apache.http.client.HttpClient)7 List (java.util.List)6 ConcurrentMap (j.util.ConcurrentMap)5 RemoteException (java.rmi.RemoteException)4 Http (j.service.Http)2 Rmi (j.service.Rmi)2 ServiceBase (j.service.server.ServiceBase)2 JUtilList (j.util.JUtilList)2 File (java.io.File)2 LinkedList (java.util.LinkedList)2 Document (org.dom4j.Document)2 Element (org.dom4j.Element)2 JHttp (j.http.JHttp)1 NvwaObject (j.nvwa.NvwaObject)1