Exemple #1
0
        /// <summary>
        /// HttpProxy 配置,主要配置那些微服务可以提供外部api服务
        /// </summary>
        /// <param name="proxyName">proxyName</param>
        /// <param name="updateAction">updateAction</param>
        /// <returns>HttpProxy</returns>
        public static Configuration GetHttpProxy(string proxyName, Action <Configuration> updateAction)
        {
            switch (configCenter.ConfigType)
            {
            case ConfigType.Local:
            case ConfigType.HttpFile:
                return(config);    // 直接返回本地配置

            case ConfigType.Zookeeper:
                Action <Configuration> callBack = (Configuration cfgValue) =>
                {
                    GetHttpProxy(proxyName, updateAction); // 调用一次方法,挂载回调
                    updateAction(cfgValue);                // 重连之后要执行回调,做变更
                };
                callBackList[proxyName] = callBack;        // 断线重连之后,要把当前方法封装起来,作为回调

                var router = new ConfigWatcher();
                router.OnChange += (string path) =>
                {
                    if (updateAction != null && !string.IsNullOrEmpty(path))
                    {
                        var proxyStr = ZookeeperManager.GetNodeData(path, router);
                        if (string.IsNullOrEmpty(proxyStr))
                        {
                            return;
                        }
                        var httpProxy = Newtonsoft.Json.JsonConvert.DeserializeObject <Configuration>(proxyStr);
                        config.HttpProxy = httpProxy.HttpProxy;
                        updateAction(config);
                    }
                };
                string p   = ZookeeperManager.getConfigPath(proxyName);
                var    cfg = ZookeeperManager.GetNodeData(p + "/" + SuperHttpProxy.HttpProxy, router);
                if (string.IsNullOrEmpty(cfg))
                {
                    logger.LogWarning("获取代理层配置为空,这个代理层将无法提供代理服务 ServerSetting.GetHttpProxy is Null");
                }
                else
                {
                    var proxy = Newtonsoft.Json.JsonConvert.DeserializeObject <Configuration>(cfg);
                    config.HttpProxy = proxy.HttpProxy;
                }
                return(config);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 注册zookeeper使用
        /// </summary>
        /// <param name="serverName"></param>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="enable"></param>
        /// <param name="timeout"></param>
        public static void RegisterRouter(string serverName, string ip, int port, bool enable, int timeout)
        {
            if (configCenter.ConfigType != ConfigType.Zookeeper)
            {
                return; // 本地配置不走zookeeper;
            }
            string path       = ZookeeperManager.SetRouter(serverName, ip, port, enable, timeout);
            var    cfgWatcher = new ConfigWatcher();

            cfgWatcher.OnChange += (string p) =>
            {
                var changeData = ZookeeperManager.GetNodeData(path, cfgWatcher);
                if (!string.IsNullOrEmpty(changeData))
                {
                    try
                    {
                        var clientItem = Newtonsoft.Json.JsonConvert.DeserializeObject <ClientItem>(changeData);
                        logger.LogWarning($"我的配置被修改为:{changeData}");
                        if (clientItem != null && !clientItem.Enable)
                        {
                            // 我被下线了,更新本地配置"Enable": false,下一次重启需要带上这个状态
                            logger.LogInformation($"我被管理员下线了,哼。。。。serverName={serverName},ip={ip},port = {port}");
                        }
                        else
                        {
                            logger.LogInformation($"我被管理员上线了,哈哈哈哈。。。。serverName={serverName},ip={ip},port = {port}");
                        }
                        // 在这里修改本地配置快照
                        if (clientItem == null || string.IsNullOrEmpty(clientItem.Ip) || clientItem.Port < 1)
                        {
                            return;
                        }
                        config.ServerConfig.RpcService.Ip      = clientItem.Ip;
                        config.ServerConfig.RpcService.Port    = clientItem.Port;
                        config.ServerConfig.RpcService.Pool    = clientItem.Pool;
                        config.ServerConfig.RpcService.Enable  = clientItem.Enable;
                        config.ServerConfig.RpcService.TimeOut = clientItem.TimeOut;
                        //copyConfig(); 暂时注掉,这里还没思考好
                    }
                    catch (Exception e)
                    {
                        logger.LogCritical(e, $"RegisterRouter.cfgWatcher.OnChange.Error,serverName={serverName},ip={ip},port = {port}");
                    }
                }
            };
            ZookeeperManager.GetNodeData(path, cfgWatcher); // 监控自己router节点的内容,有可能被置为下线;
        }
Exemple #3
0
        private static void initlizeData(string appName)
        {
            // 检查标准配置,第一次可能zk是空
            // 检查标准配置节点,帮助初始化
            var standConfig = getStandConfig();

            ZookeeperManager.CheckConfig(appName, standConfig);


            // 拉取当前AppName的配置,需要注册watcher
            var dataWatcher = new ConfigWatcher();

            dataWatcher.OnChange += (string path) =>
            {
                string configData = ZookeeperManager.GetNodeData(path, dataWatcher);
                if (string.IsNullOrEmpty(configData))
                {
                    return;
                }
                UpdateZookeeper(path, configData);
            };

            List <string>
            childrens = ZookeeperManager.GetConfigChildren(appName,
                                                           null); // 配置是整个获取节点,分别获取配置和分别增加watcher

            if (childrens != null && childrens.Count > 0)
            {
                string root = ZookeeperManager.getConfigPath(appName);
                if (string.IsNullOrEmpty(root))
                {
                    return;
                }
                foreach (var item in childrens)
                {
                    // 需要根据节点路径来判断是哪个节点变化了
                    string path       = root + "/" + item;
                    string configData = ZookeeperManager.GetNodeData(path, dataWatcher);
                    UpdateZookeeper(path, configData);
                }
            }
        }
Exemple #4
0
        private static RpcClients getRouters(string appName, Watcher serviceRouterWatcher)
        {
            List <string> nodeList = ZookeeperManager.GetRouterChildren(appName, serviceRouterWatcher); // 路由是整个获取节点整个跟节点监控,因为子节点是虚拟的

            if (nodeList == null || nodeList.Count < 1)
            {
                string msg = $"你代码里面调用了 {appName} ,但是从zookeeper中取不到这个服务的路由信息,GetAppClient.appName={appName}.GetChildrenNode==null";
                logger.LogWarning(msg);
                return(null);
            }

            RpcClients rpcClients = new RpcClients();

            rpcClients.Clients = new List <Client>();
            Client client = new Client()
            {
                RouterType = RouterType.Random, ServerName = appName
            };

            client.Items = new List <ClientItem>();
            rpcClients.Clients.Add(client);
            string p = ZookeeperManager.getRouterPath(appName);

            if (string.IsNullOrEmpty(p))
            {
                return(null);
            }
            foreach (var item in nodeList)
            {
                string nodeData = ZookeeperManager.GetNodeData(p + "/" + item, serviceRouterWatcher);
                if (string.IsNullOrEmpty(nodeData))
                {
                    continue;
                }
                ClientItem clientItem = Newtonsoft.Json.JsonConvert.DeserializeObject <ClientItem>(nodeData);
                client.Items.Add(clientItem);
            }
            config.RpcClients = rpcClients;
            return(rpcClients);
        }