Exemplo n.º 1
0
		public MeropsMonitor(AppHost appHost, RpcConfig config)
		{
			//var factory = new RpcClientFactory(null, null);
			_client = appHost.ClientFactory.GetInstance<IMonitorService>(config?.Monitor.Address);
			// ReSharper disable once UnusedVariable
			var writeTask = WriteLogsAsync();
		}
Exemplo n.º 2
0
        private static void InitializeMonitorConfig(IConfiguration config, RpcConfig instance)
        {
            var monitorNode = config.GetSection("monitor");

            if (monitorNode == null || !monitorNode.GetChildren().Any())
            {
                return;
            }

            try
            {
                var name    = monitorNode["name"];
                var type    = monitorNode["type"];
                var address = monitorNode["address"];

                if (string.IsNullOrEmpty(type))
                {
                    throw new RpcConfigException("type of RpcLite configuration Monitor node " + name + " can't be null or empty");
                }

                var monitor = new MonitorConfig
                {
                    Name    = name,
                    Type    = type,
                    Address = address,
                };
                instance.Monitor = monitor;
            }
            catch (Exception ex)
            {
                throw new ConfigException("Client Configuration Error", ex);
            }
        }
Exemplo n.º 3
0
        private void InitializeFilterConfig(IConfiguration config, RpcConfig instance)
        {
            var filterNode = config.GetSection("filter");

            if (filterNode?.GetChildren()?.Any() != true)
            {
                return;
            }

            instance.Filter = new FilterConfig
            {
                Filters = new List <FilterItemConfig>(),
            };

            var filtersNode = filterNode.GetSection("filters");
            var clients     = filtersNode.GetChildren();

            foreach (var item in clients)
            {
                var name = item["name"];
                var type = item["type"];

                if (string.IsNullOrEmpty(type))
                {
                    throw new RpcConfigException($"type of RpcLite configuration client node '{name}' can't be null or empty");
                }

                var serviceConfigItem = new FilterItemConfig
                {
                    Name = name,
                    Type = type,
                };
                instance.Filter.Filters.Add(serviceConfigItem);
            }
        }
Exemplo n.º 4
0
        private static void InitializeChannelConfig(IConfiguration config, RpcConfig instance)
        {
            var channelNode = config.GetSection("channel");

            if (channelNode?.GetChildren()?.Any() != true)
            {
                return;
            }

            var providersNode = channelNode.GetSection("providers");
            var providers     = providersNode.GetChildren();

            instance.Client.Channel = new ChannelConfig {
                Providers = new List <ChannelProviderConfig>()
            };
            foreach (var item in providers)
            {
                var name = item["name"];
                var type = item["type"];

                if (string.IsNullOrEmpty(type))
                {
                    throw new RpcConfigException($"type of RpcLite configuration client node '{name}' can't be null or empty");
                }

                var serviceConfigItem = new ChannelProviderConfig
                {
                    Name = name,
                    Type = type,
                };
                instance.Client.Channel.Providers.Add(serviceConfigItem);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 
        /// </summary>
        public AppHost(RpcConfig config)
        {
            if (config == null)
                throw new ArgumentNullException(nameof(config));

            Config = config;

            AppId = config.AppId;
            Registry = RegistryHelper.GetRegistry(this, config);
            ServiceHost = new ServiceHost(this, config);
            FormatterManager = new FormatterManager(config);
            ClientFactory = new RpcClientFactory(this, config);

            if (!string.IsNullOrWhiteSpace(config.Monitor?.Type))
            {
                var monitorFactory = ReflectHelper.CreateInstanceByIdentifier<IMonitorFactory>(config.Monitor.Type);
                Monitor = monitorFactory.CreateMonitor(this, config);
            }

            if (config.Filter?.Filters.Count > 0)
            {
                foreach (var item in config.Filter.Filters)
                {
                    var factory = ReflectHelper.CreateInstanceByIdentifier<IFilterFactory>(item.Type);
                    var filters = factory.CreateFilters();
                    if (filters == null) continue;
                    foreach (var filter in filters)
                    {
                        AddFilter(filter);
                    }
                }
            }
        }
Exemplo n.º 6
0
        public MeropsRegistry(AppHost appHost, RpcConfig config)
            : base(config)
        {
            Config = config;
            _appHost = appHost;

            _registryClient = new Lazy<IRegistryService>(() =>
            {
                var address = Config?.Registry?.Address;

                if (string.IsNullOrWhiteSpace(address))
                {
                    LogHelper.Error("Registry Client Config Error: not exist or path is empty");
                    return null;
                }

                var client = _appHost == null
                    ? ClientFactory.GetInstance<IRegistryService>(address)
                    : _appHost.ClientFactory.GetInstance<IRegistryService>(address);
                return client;
            });

            InitilizeAddresses();
            // ReSharper disable once VirtualMemberCallInConstructor
            StartUpdateRegistry();
        }
Exemplo n.º 7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            if (Instance != null)
                return Instance;

            Instance = RpcConfigHelper.GetConfig(new XmlConfigurationSection(section));

            return Instance;
        }
Exemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public object Create(object parent, object configContext, XmlNode section)
        {
            if (Instance != null)
            {
                return(Instance);
            }

            Instance = RpcConfigHelper.GetConfig(new XmlConfigurationSection(section));

            return(Instance);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="config"></param>
        public DefaultServiceMapper(RpcServiceFactory factory, RpcConfig config)
        {
            if (factory == null)
                throw new ArgumentNullException(nameof(factory));

            if (config == null)
                throw new ArgumentNullException(nameof(config));

            _config = config;
            _factory = factory;
        }
Exemplo n.º 10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="routers"></param>
        /// <param name="rpcConfig"></param>
        public static void Initialize(IRouteBuilder routers, RpcConfig rpcConfig)
        {
            RpcManager.Initialize(rpcConfig);

            if (rpcConfig?.Service?.Paths != null)
            {
                foreach (var path in rpcConfig.Service.Paths)
                {
                    routers.MapRoute(path, context => RpcManager.ProcessAsync(new AspNetCoreServerContext(context)));
                }
            }
        }
Exemplo n.º 11
0
        // ReSharper disable once FunctionComplexityOverflow
        private static void InitializeClientConfig(IConfiguration config, RpcConfig instance)
        {
            var clientsNode = config.GetSection("clients");

            if (clientsNode != null)
            {
                var environment = clientsNode["environment"];
                //instance.ClientEnvironment = !string.IsNullOrWhiteSpace(environment) ? environment : instance.Environment;
                if (instance.Client == null)
                {
                    instance.Client = new ClientConfig();
                }

                var clients = clientsNode.GetChildren();
                foreach (var item in clients)
                {
                    var name    = item["name"];                  // GetAttribute("name", item);
                    var address = item["address"];               //GetAttribute("path", item);
                    var type    = item["type"];                  //GetAttribute("type", item);
                    var env     = item["environment"];           //GetAttribute("type", item);
                    //var nameSpace = item["namespace"]; // GetAttribute("namespace", item);

                    if (string.IsNullOrEmpty(name))
                    {
                        throw new RpcConfigException("name of RpcLite configuration client node can't be null or empty");
                    }
                    //if (string.IsNullOrEmpty(path))
                    //	throw new ConfigurationErrorsException("path of RpcLite configuration node can't be null or empty");
                    if (string.IsNullOrEmpty(type))
                    {
                        throw new RpcConfigException(string.Format("type of RpcLite configuration client node '{0}' can't be null or empty", name));
                    }
                    //if (string.IsNullOrEmpty(nameSpace))
                    //	throw new RpcConfigException(string.Format("namespace of RpcLite configuration client node '{0}' can't be null or empty", name));

                    var serviceConfigItem = new ClientConfigItem
                    {
                        Name = name,
                        Type = type,
                        //TypeName = typeName,
                        //AssemblyName = assemblyName,
                        Address = address,
                        //Namespace = nameSpace,
                        Group = string.IsNullOrWhiteSpace(env) ? environment : env,
                    };
                    instance.Client.Clients.Add(serviceConfigItem);
                }
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// get RpcLiteConfig from IConfiguration
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public RpcConfig GetConfig(IConfiguration config)
        {
            var instance = new RpcConfig
            {
                AppId = config["appId"],
                Group = config["environment"],
                Version = RpcConfigHelper.GetVersion(config),
            };

            //InitializeResolverConfig(config, instance);
            InitializeServiceConfig(config, instance);
            InitializeClientConfig(config, instance);

            return instance;
        }
Exemplo n.º 13
0
        /// <summary>
        /// get RpcLiteConfig from IConfiguration
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public RpcConfig GetConfig(IConfiguration config)
        {
            var instance = new RpcConfig
            {
                AppId   = config["appId"],
                Group   = config["environment"],
                Version = RpcConfigHelper.GetVersion(config),
            };

            //InitializeResolverConfig(config, instance);
            InitializeServiceConfig(config, instance);
            InitializeClientConfig(config, instance);

            return(instance);
        }
Exemplo n.º 14
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="rpcConfig"></param>
        public static void Initialize(IApplicationBuilder app, RpcConfig rpcConfig)
        {
            RpcManager.Initialize(rpcConfig);

            if (rpcConfig?.Service?.Paths == null) return;
            if (app == null) return;

            var routers = new RouteBuilder(app);

            foreach (var path in rpcConfig.Service.Paths)
            {
                routers.MapRoute(path + "{*RpcLiteServicePath}", context => RpcManager.ProcessAsync(new AspNetCoreServerContext(context)));
            }
            var routes1 = routers.Build();
            app.UseRouter(routes1);
        }
Exemplo n.º 15
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="host"></param>
        /// <param name="config"></param>
        public RpcServiceFactory(AppHost host, RpcConfig config)
        {
            _appHost = host;
            _config = config;
            Services = new ReadOnlyListWraper<RpcService>(_services);

            if (config?.Service?.Mapper != null)
            {
                var factory = ReflectHelper.CreateInstanceByIdentifier<IServiceMapperFactory>(config.Service.Mapper.Type);
                _serviceMapper = factory.CreateServiceMapper(this, config);
            }
            else
            {
                _serviceMapper = new DefaultServiceMapper(this, config);
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        public static void Initialize(RpcConfig config)
        {
            if (AppHost != null)
            {
                return;
                //throw new InvalidOperationException("default service host already initialized");
            }

            lock (InitilizeLock)
            {
                if (AppHost == null)
                {
                    AppHost = new AppHost(config);
                    AppHost.Initialize();
                }
            }
        }
Exemplo n.º 17
0
        //public static void Initialize()
        //{
        //}

        static RpcLiteConfigSection()
        {
            try
            {
                var sec = ConfigurationManager.GetSection("RpcLite");
                Instance = sec as RpcConfig;
                //RpcLiteConfig.SetInstance(Instance);
            }
            catch (RpcConfigException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ConfigException("initialize config error", ex);
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// get RpcLiteConfig from IConfiguration
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public RpcConfig GetConfig(IConfiguration config)
        {
            var instance = new RpcConfig
            {
                AppId = config["appId"],
                Group = config["environment"],
            };

            InitializeServiceConfig(config, instance);
            InitializeClientConfig(config, instance);
            InitializeRegistryConfig(config, instance);
            InitializeMonitorConfig(config, instance);
            InitializeFormatterConfig(config, instance);
            InitializeFilterConfig(config, instance);

            return instance;
        }
Exemplo n.º 19
0
        /// <summary>
        /// get RpcLiteConfig from IConfiguration
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public RpcConfig GetConfig(IConfiguration config)
        {
            var instance = new RpcConfig
            {
                AppId = config["appId"],
                Group = config["environment"],
            };

            InitializeServiceConfig(config, instance);
            InitializeClientConfig(config, instance);
            InitializeRegistryConfig(config, instance);
            InitializeMonitorConfig(config, instance);
            InitializeFormatterConfig(config, instance);
            InitializeFilterConfig(config, instance);

            return(instance);
        }
Exemplo n.º 20
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        public void Initialize(RpcConfig config)
        {
            var channelProviders = new List<IChannelProvider> { new DefaultChannelProvider() };

            if (config?.Client?.Channel?.Providers != null)
            {
                foreach (var provider in config.Client.Channel.Providers)
                {
                    channelProviders.Add(ReflectHelper.CreateInstanceByIdentifier<IChannelProvider>(provider.Type));
                }
            }

            foreach (var provider in channelProviders)
            {
                foreach (var protocolName in provider.Protocols)
                {
                    _chanelProviderDictionary[protocolName.ToLower()] = provider;
                }
            }
        }
Exemplo n.º 21
0
        // ReSharper disable once FunctionComplexityOverflow
        private static void InitializeClientConfig(IConfiguration config, RpcConfig instance)
        {
            var clientsNode = config.GetSection("clients");
            if (clientsNode != null)
            {
                var environment = clientsNode["environment"];
                //instance.ClientEnvironment = !string.IsNullOrWhiteSpace(environment) ? environment : instance.Environment;
                if (instance.Client == null) instance.Client = new ClientConfig();

                var clients = clientsNode.GetChildren();
                foreach (var item in clients)
                {
                    var name = item["name"]; // GetAttribute("name", item);
                    var address = item["address"]; //GetAttribute("path", item);
                    var type = item["type"]; //GetAttribute("type", item);
                    var env = item["environment"]; //GetAttribute("type", item);
                    //var nameSpace = item["namespace"]; // GetAttribute("namespace", item);

                    if (string.IsNullOrEmpty(name))
                        throw new RpcConfigException("name of RpcLite configuration client node can't be null or empty");
                    //if (string.IsNullOrEmpty(path))
                    //	throw new ConfigurationErrorsException("path of RpcLite configuration node can't be null or empty");
                    if (string.IsNullOrEmpty(type))
                        throw new RpcConfigException(string.Format("type of RpcLite configuration client node '{0}' can't be null or empty", name));
                    //if (string.IsNullOrEmpty(nameSpace))
                    //	throw new RpcConfigException(string.Format("namespace of RpcLite configuration client node '{0}' can't be null or empty", name));

                    var serviceConfigItem = new ClientConfigItem
                    {
                        Name = name,
                        Type = type,
                        //TypeName = typeName,
                        //AssemblyName = assemblyName,
                        Address = address,
                        //Namespace = nameSpace,
                        Group = string.IsNullOrWhiteSpace(env) ? environment : env,
                    };
                    instance.Client.Clients.Add(serviceConfigItem);
                }
            }
        }
Exemplo n.º 22
0
        /// <summary>
        /// 
        /// </summary>
        public ServiceHost(AppHost appHost, RpcConfig config)
        {
            _config = config;
            _appHost = appHost;

            _serviceFactory = new RpcServiceFactory(_appHost, config);

            _initializeRegistry = new Lazy<object>(() =>
            {
                var services = _config?.Service?.Services;
                if (services == null || _appHost.Registry?.CanRegister != true)
                    return null;

                foreach (var service in services)
                {
                    _appHost.Registry.RegisterAsync(service.ToServiceInfo());
                }

                return null;
            });
        }
Exemplo n.º 23
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        public FormatterManager(RpcConfig config)
        {
            if (config?.Formatter?.RemoveDefault != true)
            {
                //#if NETCORE
                //				AddFormatter(new JsonFormatter());
                //#else
                //AddFormatter(new NetJsonFormater());
                AddFormatter(new JsonFormatter());
                AddFormatter(new XmlFormatter());
                //#endif
            }

            if (config?.Formatter?.Formatters != null)
            {
                foreach (var item in config.Formatter?.Formatters)
                {
                    var formatter = ReflectHelper.CreateInstanceByIdentifier<IFormatter>(item.Type);
                    AddFormatter(formatter);
                }
            }
        }
Exemplo n.º 24
0
        private static void InitializeFormatterConfig(IConfiguration config, RpcConfig instance)
        {
            var formatterNode = config.GetSection("formatter");

            if (formatterNode?.GetChildren()?.Any() == true)
            {
                var removeDefaultNode = formatterNode["removeDefault"];
                instance.Formatter = new FormatterConfig
                {
                    RemoveDefault = removeDefaultNode?.ToLower() == "true",
                    Formatters    = new List <FormatterItemConfig>(),
                };

                var formattersNode = formatterNode.GetSection("formatters");
                var clients        = formattersNode.GetChildren();
                foreach (var item in clients)
                {
                    var name = item["name"];
                    var type = item["type"];

                    //if (string.IsNullOrEmpty(name))
                    //	throw new RpcConfigException("name of RpcLite configuration client node can't be null or empty");
                    if (string.IsNullOrEmpty(type))
                    {
                        throw new RpcConfigException(string.Format("type of RpcLite configuration client node '{0}' can't be null or empty", name));
                    }

                    var serviceConfigItem = new FormatterItemConfig
                    {
                        Name = name,
                        Type = type,
                    };
                    instance.Formatter.Formatters.Add(serviceConfigItem);
                }
            }
        }
Exemplo n.º 25
0
        private static void InitializeChannelConfig(IConfiguration config, RpcConfig instance)
        {
            var channelNode = config.GetSection("channel");
            if (channelNode?.GetChildren()?.Any() != true) return;

            var providersNode = channelNode.GetSection("providers");
            var providers = providersNode.GetChildren();
            instance.Client.Channel = new ChannelConfig { Providers = new List<ChannelProviderConfig>() };
            foreach (var item in providers)
            {
                var name = item["name"];
                var type = item["type"];

                if (string.IsNullOrEmpty(type))
                    throw new RpcConfigException($"type of RpcLite configuration client node '{name}' can't be null or empty");

                var serviceConfigItem = new ChannelProviderConfig
                {
                    Name = name,
                    Type = type,
                };
                instance.Client.Channel.Providers.Add(serviceConfigItem);
            }
        }
Exemplo n.º 26
0
        private static void InitializeRegistryConfig(IConfiguration config, RpcConfig instance)
        {
            var registryNode = config.GetSection("registry");
            if (registryNode == null || !registryNode.GetChildren().Any()) return;

            try
            {
                var name = registryNode["name"];
                var type = registryNode["type"];
                var address = registryNode["address"];

                if (string.IsNullOrEmpty(type))
                    throw new RpcConfigException("type of RpcLite configuration Registry node " + name + " can't be null or empty");

                var registry = new RegistryConfig
                {
                    Name = name,
                    Type = type,
                    Address = address,
                };
                instance.Registry = registry;
            }
            catch (Exception ex)
            {
                throw new ConfigException("Client Configuration Error", ex);
            }
        }
Exemplo n.º 27
0
        // ReSharper disable once FunctionComplexityOverflow
        private static void InitializeClientConfig(IConfiguration config, RpcConfig instance)
        {
            var clientNode = config.GetSection("client");

            if (clientNode?.GetChildren()?.Any() != true)
            {
                return;
            }

            instance.Client = new ClientConfig();

            var invokerNode = clientNode.GetSection("invoker");

            if (invokerNode?.GetChildren()?.Any() == true)
            {
                var name = invokerNode["name"];
                var type = invokerNode["type"];

                instance.Client.Invoker = new InvokerConfig
                {
                    Name = name,
                    Type = type,
                };
            }

            var clientsNode = clientNode.GetSection("clients");

            if (clientsNode?.GetChildren()?.Any() == true)
            {
                var group = clientsNode["group"];
                //instance.ClientEnvironment = !string.IsNullOrWhiteSpace(environment) ? environment : instance.Environment;

                var clients = clientsNode.GetChildren();
                foreach (var item in clients)
                {
                    var name    = item["name"];
                    var address = item["address"];
                    var type    = item["type"];
                    var env     = item["group"];
                    //var nameSpace = item["namespace"];

                    if (string.IsNullOrEmpty(name))
                    {
                        throw new RpcConfigException("name of RpcLite configuration client node can't be null or empty");
                    }
                    //if (string.IsNullOrEmpty(path))
                    //	throw new ConfigurationErrorsException("path of RpcLite configuration node can't be null or empty");
                    if (string.IsNullOrEmpty(type))
                    {
                        throw new RpcConfigException(string.Format("type of RpcLite configuration client node '{0}' can't be null or empty", name));
                    }
                    //if (string.IsNullOrEmpty(nameSpace))
                    //	throw new RpcConfigException(string.Format("namespace of RpcLite configuration client node '{0}' can't be null or empty", name));


                    var dic = new Dictionary <string, string>();
                    foreach (var configItem in item.GetChildren())
                    {
                        dic[configItem.Key] = configItem.Value;
                    }

                    var serviceConfigItem = new ClientConfigItem
                    {
                        Name = name,
                        Type = type,
                        //TypeName = typeName,
                        //AssemblyName = assemblyName,
                        Address = address,
                        //Namespace = nameSpace,
                        Group = string.IsNullOrWhiteSpace(env) ? group : env,
                        Items = dic
                    };
                    instance.Client.Clients.Add(serviceConfigItem);
                }
            }

            InitializeChannelConfig(clientNode, instance);
        }
Exemplo n.º 28
0
        private static void InitializeServiceConfig(IConfiguration config, RpcConfig instance)
        {
            try
            {
                var serviceNode = config.GetSection("service");
                if (serviceNode == null) return;

                if (instance.Service == null)
                    instance.Service = new ServiceConfig();

                var servicesNode = serviceNode.GetSection("services");
                if (servicesNode != null)
                {
                    var serviceItems = new List<ServiceConfigItem>();
                    var serviceItemNodes = servicesNode.GetChildren();
                    foreach (var item in serviceItemNodes)
                    {
                        var name = item["name"];
                        var path = item["path"];
                        var type = item["type"];
                        var address = item["address"];
                        var env = item["environment"]; //GetAttribute("type", item);

                        if (string.IsNullOrEmpty(name))
                            throw new RpcConfigException("name of RpcLite service configuration node can't be null or empty");
                        if (string.IsNullOrEmpty(path))
                            throw new RpcConfigException("path of RpcLite service configuration node " + name + " can't be null or empty");
                        if (string.IsNullOrEmpty(type))
                            throw new RpcConfigException("type of RpcLite service configuration node " + name + " can't be null or empty");

                        //string typeName;
                        //string assemblyName;

                        //var splitorIndex = type.IndexOf(",", StringComparison.Ordinal);
                        //if (splitorIndex > -1)
                        //{
                        //	typeName = type.Substring(0, splitorIndex);
                        //	assemblyName = type.Substring(splitorIndex + 1);
                        //}
                        //else
                        //{
                        //	typeName = type;
                        //	assemblyName = null;
                        //}

                        //if (string.IsNullOrWhiteSpace(assemblyName))
                        //	throw new RpcConfigException("assembly can't be null or empty, in RpcLite service configuration node " + name);

                        var serviceConfigItem = new ServiceConfigItem
                        {
                            Name = name,
                            Type = type,
                            //TypeName = typeName,
                            //AssemblyName = assemblyName,
                            Path = path,
                            Address = address,
                            Group = env,
                        };
                        serviceItems.Add(serviceConfigItem);
                    }
                    instance.Service.Services.AddRange(serviceItems);
                }

                var mapperNode = serviceNode.GetSection("mapper");
                if (mapperNode != null)
                {
                    instance.Service.Mapper = InitializeServiceMapperConfig(mapperNode);
                }

                var pathsNode = serviceNode.GetSection("paths");
                if (pathsNode != null)
                {
                    var paths = new List<string>();
                    var pathNodes = pathsNode.GetChildren();
                    foreach (var path in pathNodes)
                    {
                        if (!string.IsNullOrWhiteSpace(path.Value))
                            paths.Add(path.Value);
                        else if (!string.IsNullOrWhiteSpace(path["value"]))
                            paths.Add(path["value"]);
                    }
                    instance.Service.Paths = paths.ToArray();
                }

            }
            catch (Exception ex)
            {
                throw new ConfigException("Service Configuration Error", ex);
            }
        }
Exemplo n.º 29
0
 public IMonitor CreateMonitor(AppHost appHost, RpcConfig config)
 {
     return new MeropsMonitor(appHost, config);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public IServiceMapper CreateServiceMapper(RpcServiceFactory factory, RpcConfig config)
 {
     return new DefaultServiceMapper(factory, config);
 }
Exemplo n.º 31
0
        // ReSharper disable once FunctionComplexityOverflow
        private static void InitializeClientConfig(IConfiguration config, RpcConfig instance)
        {
            var clientNode = config.GetSection("client");
            if (clientNode?.GetChildren()?.Any() != true)
                return;

            instance.Client = new ClientConfig();

            var invokerNode = clientNode.GetSection("invoker");
            if (invokerNode?.GetChildren()?.Any() == true)
            {
                var name = invokerNode["name"];
                var type = invokerNode["type"];

                instance.Client.Invoker = new InvokerConfig
                {
                    Name = name,
                    Type = type,
                };
            }

            var clientsNode = clientNode.GetSection("clients");
            if (clientsNode?.GetChildren()?.Any() == true)
            {
                var group = clientsNode["group"];
                //instance.ClientEnvironment = !string.IsNullOrWhiteSpace(environment) ? environment : instance.Environment;

                var clients = clientsNode.GetChildren();
                foreach (var item in clients)
                {
                    var name = item["name"];
                    var address = item["address"];
                    var type = item["type"];
                    var env = item["group"];
                    //var nameSpace = item["namespace"];

                    if (string.IsNullOrEmpty(name))
                        throw new RpcConfigException("name of RpcLite configuration client node can't be null or empty");
                    //if (string.IsNullOrEmpty(path))
                    //	throw new ConfigurationErrorsException("path of RpcLite configuration node can't be null or empty");
                    if (string.IsNullOrEmpty(type))
                        throw new RpcConfigException(string.Format("type of RpcLite configuration client node '{0}' can't be null or empty", name));
                    //if (string.IsNullOrEmpty(nameSpace))
                    //	throw new RpcConfigException(string.Format("namespace of RpcLite configuration client node '{0}' can't be null or empty", name));

                    var dic = new Dictionary<string, string>();
                    foreach (var configItem in item.GetChildren())
                    {
                        dic[configItem.Key] = configItem.Value;
                    }

                    var serviceConfigItem = new ClientConfigItem
                    {
                        Name = name,
                        Type = type,
                        //TypeName = typeName,
                        //AssemblyName = assemblyName,
                        Address = address,
                        //Namespace = nameSpace,
                        Group = string.IsNullOrWhiteSpace(env) ? group : env,
                        Items = dic
                    };
                    instance.Client.Clients.Add(serviceConfigItem);
                }
            }

            InitializeChannelConfig(clientNode, instance);
        }
Exemplo n.º 32
0
 public IRegistry CreateRegistry(AppHost appHost, RpcConfig config)
 {
     return new MeropsRegistry(appHost, config);
 }
Exemplo n.º 33
0
        //private static void InitializeResolverConfig(IConfiguration config, RpcConfig instance)
        //{
        //    try
        //    {
        //        var resolverNode = config.GetSection("addressResolver");
        //        if (resolverNode != null && resolverNode.GetChildren().Any())
        //        {
        //            //foreach (XmlNode item in resolverNode.ChildNodes)
        //            {
        //                //if (resolverNode.Name != "add" || resolverNode.Attributes == null)
        //                //	continue;
        //                var name = resolverNode["name"]; //GetAttribute("name", resolverNode);
        //                var type = resolverNode["type"]; //GetAttribute("type", resolverNode);
        //                if (string.IsNullOrEmpty(name))
        //                    throw new RpcConfigException("name of RpcLite configuration addressResolver node can't be null or empty");
        //                if (string.IsNullOrEmpty(type))
        //                    throw new RpcConfigException("type of RpcLite configuration addressResolver node " + name + " can't be null or empty");
        //                string typeName;
        //                string assemblyName;
        //                var splitorIndex = type.IndexOf(",", StringComparison.Ordinal);
        //                if (splitorIndex > -1)
        //                {
        //                    typeName = type.Substring(0, splitorIndex);
        //                    assemblyName = type.Substring(splitorIndex + 1);
        //                }
        //                else
        //                {
        //                    typeName = type;
        //                    assemblyName = null;
        //                }
        //                var resolver = new ResolverConfigItem
        //                {
        //                    Name = name,
        //                    Type = type,
        //                    TypeName = typeName,
        //                    AssemblyName = assemblyName,
        //                };
        //                instance.Resover = resolver;
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new ConfigException("Client Configuration Error", ex);
        //    }
        //}
        private static void InitializeServiceConfig(IConfiguration config, RpcConfig instance)
        {
            try
            {
                var servicesNode = config.GetSection("services");
                if (servicesNode != null)
                {
                    instance.Service = instance.Service ?? new ServiceConfig();
                    var serviceItemNodes = servicesNode.GetChildren();
                    foreach (var item in serviceItemNodes)
                    {
                        var name = item["name"];
                        var path = item["path"];
                        var type = item["type"];
                        var address = item["address"];
                        var env = item["environment"]; //GetAttribute("type", item);

                        if (string.IsNullOrEmpty(name))
                            throw new RpcConfigException("name of RpcLite service configuration node can't be null or empty");
                        if (string.IsNullOrEmpty(path))
                            throw new RpcConfigException("path of RpcLite service configuration node " + name + " can't be null or empty");
                        if (string.IsNullOrEmpty(type))
                            throw new RpcConfigException("type of RpcLite service configuration node " + name + " can't be null or empty");

                        //string typeName;
                        //string assemblyName;

                        //var splitorIndex = type.IndexOf(",", StringComparison.Ordinal);
                        //if (splitorIndex > -1)
                        //{
                        //	typeName = type.Substring(0, splitorIndex);
                        //	assemblyName = type.Substring(splitorIndex + 1);
                        //}
                        //else
                        //{
                        //	typeName = type;
                        //	assemblyName = null;
                        //}

                        //if (string.IsNullOrWhiteSpace(assemblyName))
                        //	throw new RpcConfigException("assembly can't be null or empty, in RpcLite service configuration node " + name);

                        var serviceConfigItem = new ServiceConfigItem
                        {
                            Name = name,
                            Type = type,
                            //TypeName = typeName,
                            //AssemblyName = assemblyName,
                            Path = path,
                            Address = address,
                            Group = env,
                        };
                        instance.Service.Services.Add(serviceConfigItem);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ConfigException("Service Configuration Error", ex);
            }
        }
Exemplo n.º 34
0
        public static IMonitor GetMonitor(RpcConfig config)
        {
            var registryItem = config.Monitor;
            if (registryItem == null) return null;

            try
            {
                var type = ReflectHelper.GetTypeByIdentifier(registryItem.Type);
                if (type != null)
                {
                    var constructors = type
            #if NETCORE
                        .GetTypeInfo()
                        .DeclaredConstructors
                        .ToArray();
            #else
                    .GetConstructors();
            #endif
                    var constructorWithConfig = constructors
                        .Where(it =>
                        {
                            if (it.IsPrivate)
                                return false;

                            var paras = it.GetParameters();
                            return paras.Length == 1 && paras[0].ParameterType == typeof(RpcConfig);
                        })
                        .FirstOrDefault();

                    if (constructorWithConfig != null)
                    {
                        var monitor = Activator.CreateInstance(type, config) as IMonitor;
                        return monitor;
                    }

                    var constructorWithoutParamerters = constructors
                        .Where(it =>
                        {
                            if (it.IsPrivate)
                                return false;

                            var paras = it.GetParameters();
                            return paras.Length == 0;
                        })
                        .FirstOrDefault();

                    if (constructorWithoutParamerters != null)
                    {
                        var monitor = Activator.CreateInstance(type) as IMonitor;
                        return monitor;
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("InitializeResolver error", ex);
                throw;
            }

            return null;
        }
Exemplo n.º 35
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="config"></param>
 public DefaultRegistry(RpcConfig config)
     : base(config)
 {
     InitilizeAddresses();
 }
Exemplo n.º 36
0
 /// <summary>
 /// initialize with RpcConfig
 /// </summary>
 /// <param name="config"></param>
 public static void Initialize(RpcConfig config)
 {
     RpcManager.Initialize(config);
 }
Exemplo n.º 37
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="appHost"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public IMonitor CreateMonitor(AppHost appHost, RpcConfig config)
 {
     return new ConsoleMonitor();
 }
Exemplo n.º 38
0
        private static void InitializeServiceConfig(IConfiguration config, RpcConfig instance)
        {
            try
            {
                var serviceNode = config.GetSection("service");
                if (serviceNode == null)
                {
                    return;
                }

                if (instance.Service == null)
                {
                    instance.Service = new ServiceConfig();
                }

                var servicesNode = serviceNode.GetSection("services");
                if (servicesNode != null)
                {
                    var serviceItems     = new List <ServiceConfigItem>();
                    var serviceItemNodes = servicesNode.GetChildren();
                    foreach (var item in serviceItemNodes)
                    {
                        var name    = item["name"];
                        var path    = item["path"];
                        var type    = item["type"];
                        var address = item["address"];
                        var env     = item["environment"];                     //GetAttribute("type", item);

                        if (string.IsNullOrEmpty(name))
                        {
                            throw new RpcConfigException("name of RpcLite service configuration node can't be null or empty");
                        }
                        if (string.IsNullOrEmpty(path))
                        {
                            throw new RpcConfigException("path of RpcLite service configuration node " + name + " can't be null or empty");
                        }
                        if (string.IsNullOrEmpty(type))
                        {
                            throw new RpcConfigException("type of RpcLite service configuration node " + name + " can't be null or empty");
                        }

                        //string typeName;
                        //string assemblyName;

                        //var splitorIndex = type.IndexOf(",", StringComparison.Ordinal);
                        //if (splitorIndex > -1)
                        //{
                        //	typeName = type.Substring(0, splitorIndex);
                        //	assemblyName = type.Substring(splitorIndex + 1);
                        //}
                        //else
                        //{
                        //	typeName = type;
                        //	assemblyName = null;
                        //}

                        //if (string.IsNullOrWhiteSpace(assemblyName))
                        //	throw new RpcConfigException("assembly can't be null or empty, in RpcLite service configuration node " + name);

                        var serviceConfigItem = new ServiceConfigItem
                        {
                            Name = name,
                            Type = type,
                            //TypeName = typeName,
                            //AssemblyName = assemblyName,
                            Path    = path,
                            Address = address,
                            Group   = env,
                        };
                        serviceItems.Add(serviceConfigItem);
                    }
                    instance.Service.Services.AddRange(serviceItems);
                }

                var mapperNode = serviceNode.GetSection("mapper");
                if (mapperNode != null)
                {
                    instance.Service.Mapper = InitializeServiceMapperConfig(mapperNode);
                }

                var pathsNode = serviceNode.GetSection("paths");
                if (pathsNode != null)
                {
                    var paths     = new List <string>();
                    var pathNodes = pathsNode.GetChildren();
                    foreach (var path in pathNodes)
                    {
                        if (!string.IsNullOrWhiteSpace(path.Value))
                        {
                            paths.Add(path.Value);
                        }
                        else if (!string.IsNullOrWhiteSpace(path["value"]))
                        {
                            paths.Add(path["value"]);
                        }
                    }
                    instance.Service.Paths = paths.ToArray();
                }
            }
            catch (Exception ex)
            {
                throw new ConfigException("Service Configuration Error", ex);
            }
        }
Exemplo n.º 39
0
        private static void InitializeFormatterConfig(IConfiguration config, RpcConfig instance)
        {
            var formatterNode = config.GetSection("formatter");

            if (formatterNode?.GetChildren()?.Any() == true)
            {
                var removeDefaultNode = formatterNode["removeDefault"];
                instance.Formatter = new FormatterConfig
                {
                    RemoveDefault = removeDefaultNode?.ToLower() == "true",
                    Formatters = new List<FormatterItemConfig>(),
                };

                var formattersNode = formatterNode.GetSection("formatters");
                var clients = formattersNode.GetChildren();
                foreach (var item in clients)
                {
                    var name = item["name"];
                    var type = item["type"];

                    //if (string.IsNullOrEmpty(name))
                    //	throw new RpcConfigException("name of RpcLite configuration client node can't be null or empty");
                    if (string.IsNullOrEmpty(type))
                        throw new RpcConfigException(string.Format("type of RpcLite configuration client node '{0}' can't be null or empty", name));

                    var serviceConfigItem = new FormatterItemConfig
                    {
                        Name = name,
                        Type = type,
                    };
                    instance.Formatter.Formatters.Add(serviceConfigItem);
                }
            }
        }
Exemplo n.º 40
0
        private void InitializeFilterConfig(IConfiguration config, RpcConfig instance)
        {
            var filterNode = config.GetSection("filter");

            if (filterNode?.GetChildren()?.Any() != true) return;

            instance.Filter = new FilterConfig
            {
                Filters = new List<FilterItemConfig>(),
            };

            var filtersNode = filterNode.GetSection("filters");
            var clients = filtersNode.GetChildren();
            foreach (var item in clients)
            {
                var name = item["name"];
                var type = item["type"];

                if (string.IsNullOrEmpty(type))
                    throw new RpcConfigException($"type of RpcLite configuration client node '{name}' can't be null or empty");

                var serviceConfigItem = new FilterItemConfig
                {
                    Name = name,
                    Type = type,
                };
                instance.Filter.Filters.Add(serviceConfigItem);
            }
        }
Exemplo n.º 41
0
 /// <summary>
 /// initialize with RpcConfig
 /// </summary>
 /// <param name="config"></param>
 public static void Initialize(RpcConfig config)
 {
     RpcManager.Initialize(config);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="routes"></param>
 /// <param name="rpcConfig"></param>
 /// <returns></returns>
 public static IRouteBuilder UseRpcLite(this IRouteBuilder routes, RpcConfig rpcConfig)
 {
     AspNetCoreInitializer.Initialize(routes, rpcConfig);
     return routes;
 }