Ejemplo n.º 1
0
        public override void PostInitialize()
        {
            _grpcClientConfiguration = IocManager.Resolve <IGrpcClientConfiguration>();

            if (!_grpcClientConfiguration.IsDebugMode)
            {
                ScanAllAvailableGrpcServices(_grpcClientConfiguration);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 扫描所有可用的远端服务
        /// </summary>
        /// <param name="config">配置项</param>
        private void ScanAllAvailableGrpcServices(IGrpcClientConfiguration config)
        {
            var consulClient       = IocManager.Resolve <IConsulClientFactory>().Get(config.ConsulRegistryConfiguration);
            var grpcChannelFactory = IocManager.Resolve <IGrpcChannelFactory>();

            var policy = Policy.Timeout(5, (context, span, arg3) => throw new AbpInitializationException("无法连接到 Consul 集群."));

            policy.Execute(() =>
            {
                AsyncHelper.RunSync(async() =>
                {
                    var services = await consulClient.Catalog.Services();
                    foreach (var service in services.Response)
                    {
                        var serviceInfo    = await consulClient.Catalog.Service(service.Key);
                        var grpcServerInfo = serviceInfo.Response.SkipWhile(z => !z.ServiceTags.Contains("Grpc"));

                        //TODO: 此处可做负载均衡
                        foreach (var info in grpcServerInfo)
                        {
                            if (!_grpcClientConfiguration.GrpcServers.ContainsKey(info.ServiceName))
                            {
                                _grpcClientConfiguration.GrpcServers.Add(info.ServiceName,
                                                                         new List <Channel> {
                                    grpcChannelFactory.Get(info.ServiceAddress, info.ServicePort)
                                });
                            }
                            else
                            {
                                _grpcClientConfiguration.GrpcServers[info.ServiceName]
                                .Add(grpcChannelFactory.Get(info.ServiceAddress, info.ServicePort));
                            }
                        }
                    }
                });
            });
        }
Ejemplo n.º 3
0
        public GrpcServiceProvider(IGrpcServerConfiguration grpcServerConfiguration, IGrpcClientConfiguration grpcClientConfiguration)
        {
            _grpcServerConfiguration = grpcServerConfiguration;
            _grpcClientConfiguration = grpcClientConfiguration;
            //if (grpcServerConfiguration.GrpcAssemblies == null || grpcServerConfiguration.GrpcAssemblies.Count == 0)
            //{
            //    throw new ArgumentNullException("GrpcAssemblies of grpcServerConfiguration can not be null ,are you missing AddRpcServiceAssembly ?");
            //}
            //var types = new List<Type>();
            //foreach (var assembly in grpcServerConfiguration.GrpcAssemblies)
            //{
            //    var interfaceType = assembly.GetTypes().Where(o => o.IsInterface && o.CustomAttributes.Any(c => c.AttributeType == typeof(MagicOnionGrpcAttribute)));
            //    types.AddIfNotContains(interfaceType);
            //}
            //_needProxyTypes = types.ToArray();

            //var clientTypes = new List<Type>();
            //foreach (var assembly in grpcClientConfiguration.GrpcClientAssemblies)
            //{
            //    var interfaceType = assembly.GetTypes().Where(o => o.IsInterface && o.CustomAttributes.Any(c => c.AttributeType == typeof(MagicOnionGrpcAttribute)));
            //    clientTypes.AddIfNotContains(interfaceType);
            //}
            //_needProxyClientInterfaceTypes = clientTypes.ToArray();
        }
Ejemplo n.º 4
0
 /// <inheritdoc />
 public GrpcConnectionUtility(IGrpcClientConfiguration clientConfiguration)
 {
     _clientConfiguration = clientConfiguration;
 }
Ejemplo n.º 5
0
 public GRpcConnectionUtility(IGrpcClientConfiguration grpcClientConfiguration)
 {
     ClientCreate            = typeof(MagicOnionClient).GetMethod("Create", 1, new Type[] { typeof(Channel) });
     GrpcClientConfiguration = grpcClientConfiguration;
 }