Example #1
0
        public UserService(HttpClient httpClient,
                           IOptions <ServiceDisvoveryOptions> options,
                           IDnsQuery dnsQuery,
                           ILogger <UserService> logger,
                           IConfiguration configuration,
                           IHttpContextAccessor httpContextAccessor
                           )
        {
            _httpClient          = httpClient;
            _httpContextAccessor = httpContextAccessor;
            _logger        = logger;
            _configuration = configuration;



            var address = dnsQuery.ResolveService("service.consul", options.Value.ServiceName);

            if (!string.IsNullOrEmpty(httpContextAccessor.HttpContext.Request?.Path.Value) &&
                httpContextAccessor.HttpContext.Request.Path.Value.ToLower().Contains("/connect/token"))
            {
                address = dnsQuery.ResolveService("service.consul", _configuration.GetValue <string>("PostUserapiServiceName"));
            }

            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
            var port        = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}/";
        }
        public ActionResult Get([FromServicesAttribute] IDnsQuery dnsquery, [FromServicesAttribute] IOptions <ServiceDisvoveryOptions> serviceDisvoveryOptions)
        {
            var address     = dnsquery.ResolveService("service.consul", serviceDisvoveryOptions.Value.ServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : addressList.First().Address.ToString();
            var port        = address.First().Port;

            return(Ok($"http://{host}:{port}"));
        }
Example #3
0
        public UserService(HttpClient httpClient, IDnsQuery dnsQuery, IOptions <Dto.ServiceDisvoveryOptions> serviceDiscoveryOptions)
        {
            _httpClient = httpClient;
            var address     = dnsQuery.ResolveService("service.consul", serviceDiscoveryOptions.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
            var port        = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}";
        }
Example #4
0
        public UserService(IHttpClient httpClient, IDnsQuery dnsQuery)
        {
            _httpClient = httpClient;
            _dnsQuery   = dnsQuery;
            var result  = dnsQuery.ResolveService("service.consul", ConsulServiceName);
            var address = result.First().AddressList.FirstOrDefault().ToString();
            var port    = result.First().Port;

            requesturl = $"http://{address}:{port}/api/user";
        }
Example #5
0
        public UserService(IHttpClient httpClient, IOptions <ServiceDiscoveryOptions> options, IDnsQuery dnsQuery, ILogger <UserService> logger)
        {
            _httpClient = httpClient;
            _logger     = logger;
            var address = dnsQuery.ResolveService("service.consul", options.Value.UserServiceName);
            var host    = address.First().AddressList.FirstOrDefault()?.ToString() ?? address.First().HostName;
            var post    = address.First().Port;

            _userServiceUrl = $"http://{host}:{post}";
        }
Example #6
0
        public UserService(IHttpClient httpClient, IOptions <ServiceDisvoveryOptions> serviceOptions, IDnsQuery dnsQuery)
        {
            _httpClient = httpClient;
            var address    = dnsQuery.ResolveService("service.consul", "UserApi");
            var addrssList = address.First().AddressList;
            var host       = addrssList.Any()? addrssList.First().ToString(): address.First().HostName;
            var port       = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}";
        }
Example #7
0
        public ContactService(IHttpClient httpClient, IOptions <ServiceDiscoveryOptions> options, IDnsQuery dnsQuery, ILogger <ContactService> logger)
        {
            _httpClient = httpClient;
            var address = dnsQuery.ResolveService("service.consul", options.Value.ContactServiceName);
            var host    = address.FirstOrDefault()?.HostName;
            var port    = address.FirstOrDefault().Port;

            _contactServiceUrl = $"http://{host}:{port}";
            _logger            = logger;
        }
Example #8
0
        public UserService(IOptions <ServiceDisvoveryOptions> serviceDisconverOptinos, IDnsQuery dnsQuery, IHttpClient httpClient)
        {
            _httpClient = httpClient;
            var address     = dnsQuery.ResolveService("service.consul", serviceDisconverOptinos.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any()?addressList.First().ToString():"127.0.0.1";
            var port        = address.First().Port;

            url = $"http://{host}:{port}/";
        }
        public UserService(IhttpClient client, IOptionsSnapshot <ServiceDisvoveryOptions> serviceOptions, IDnsQuery dnsQuery, ILogger <UserService> logger)
        {
            _client = client;
            _logger = logger;
            var address     = dnsQuery.ResolveService("service.consul", serviceOptions.Value.ServiceName);
            var addressList = address.First()?.AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
            var port        = address.First().Port;

            url = $"http://{host}:{port}";
        }
Example #10
0
        public UserService(IHttpClient httpClient, IDnsQuery dnsQuery, IOptions <ServiceDiscoveryOptions> serviceDisvoveryOptions, ILogger <ResilienceHttpClient> logger)
        {
            this.httpClient = httpClient;
            this._logger    = logger;
            var address     = dnsQuery.ResolveService("service.consul", serviceDisvoveryOptions.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
            var port        = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}";
        }
Example #11
0
        public UserService(IHttpClient httpClient, IDnsQuery dns, IOptions <ServiceDiscoveryOptions> options)
        {
            _httpClient = httpClient;
            var result = dns.ResolveService("service.consul", options.Value.ServiceName);

            var addressList = result.First().AddressList;
            var address     = addressList.Any() ? addressList.First().ToString() : result.First().HostName;
            var port        = result.First().Port;

            _userServiceUrl = $"http://{address.TrimEnd('.')}:{port}";
        }
Example #12
0
        public ContactService(IHttpClient httpClient, IDnsQuery dnsquery, IOptions <ServiceDisvoveryOptions> serviceDisvoveryOptions, ILogger <ContactService> logger)
        {
            _httpClient = httpClient;
            var address     = dnsquery.ResolveService("service.consul", serviceDisvoveryOptions.Value.ContactServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : addressList.First().Address.ToString();
            var port        = address.First().Port;

            _userContactUrl = $"http://{host}:{port}";
            _logger         = logger;
        }
Example #13
0
        public UserService(IHttpClient httpClient, ILogger <UserService> logger, IDnsQuery dnsQuery, IOptions <ServiceDisvoveryOptions> options)
        {
            _httpClient = httpClient;
            _logger     = logger;
            var result      = dnsQuery.ResolveService("service.consul", options.Value.UserServiceName);
            var addressList = result.First().AddressList;
            var address     = addressList.Any() ? addressList.First().ToString() : result.First().HostName;
            var port        = result.First().Port;

            _userServiceUrl = $"http://{address}:{port}";
        }
Example #14
0
        public UserService(IHttpClient httpClient, IDnsQuery dnsQuery, IOptions <Dtos.ServiceDisvoveryOptions> serviceDisvoveryOptions, ILogger <UserService> logger)
        {
            _httpClient = httpClient;
            var address     = dnsQuery.ResolveService("service.consul", serviceDisvoveryOptions.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any()? addressList.First().ToString(): address.First().HostName?.TrimEnd('.');
            var port        = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}";
            _logger         = logger;
            Console.WriteLine("current http url:" + _userServiceUrl);
        }
Example #15
0
        public ContactService(IHttpClient httpClient, IDnsQuery dns, IOptions <ServiceDisvoveryOptions> options, ILogger <UserServcie> logger)
        {
            _dns        = dns ?? throw new ArgumentNullException(nameof(dns));
            _options    = options ?? throw new ArgumentNullException(nameof(options));
            _httpClient = httpClient;
            _logger     = logger;
            var result      = _dns.ResolveService("service.consul", _options.Value.ContactServiceName);
            var addressList = result.First().AddressList;
            var address     = addressList.Any() ? addressList.First().ToString() : result.First().HostName.TrimEnd('.');
            var port        = result.First().Port;

            userServiceUrl = $"http://{address}:{port}/";
        }
Example #16
0
        public UserService(IHttpClient httpCliemt, IOptions <Dtos.ServiceDiscoveryOptions> options, IDnsQuery dnsQuery, ILogger <UserService> logger)
        {
            _httpCliemt = httpCliemt;
            _logger     = logger;
            var address = dnsQuery.ResolveService("service.consul", options.Value.UserServiceName);

            if (address.Count() > 0)
            {
                var addressList = address.First().AddressList;
                var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
                var port        = address.First().Port;
                _userServiceUrl = $"http://{host}:{port}";
            }
        }
Example #17
0
        public UserService(IHttpClient httpClient, IOptions <ServiceDiscoveryOptions> serviceDiscoveryOptions, IDnsQuery dnsQuery, ILogger <UserService> logger)
        {
            _httpClient = httpClient;
            _logger     = logger;

            //服务发现通过IDnsQuery完成
            //通过consul服务发现获取UserAPI地址
            var address     = dnsQuery.ResolveService("service.consul", serviceDiscoveryOptions.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any()?addressList.First().ToString():address.First().HostName;
            var port        = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}";
        }
        public UserService(IHttpClient httpClient
                           , IOptions <Dtos.ServiceDisvoveryOptions> serOp
                           , IDnsQuery dnsQuery
                           , ILogger <UserService> logger)
        {
            _logger     = logger;
            _httpClient = httpClient;

            var address     = dnsQuery.ResolveService("service.consul", serOp.Value.ServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
            var port        = address.First().Port;

            _userServiceUrl = $"http://localhost:{port}";
        }
Example #19
0
        public UserService(IHttpClient httpClient,
                           IDnsQuery dnsQuery,
                           IOptions <ServiceDiscoveryOptions> options,
                           ILogger <UserService> logger)
        {
            _httpClient = httpClient;
            var address     = dnsQuery.ResolveService("service.consul", options.Value.ServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;
            //host = host.Contains('.') ? host.Split('.')[0] : host;
            var port = address.First().Port;

            url     = $"http://{host}:{port}";
            _logger = logger;
        }
Example #20
0
        public UserService(IHttpClient httpclient,
                           IDnsQuery query,
                           IOptions <ServiceDiscoveryOptions> options,
                           ILogger <UserService> logger)
        {
            _httpClient = httpclient;
            _logger     = logger;
            var address     = query.ResolveService("service.consul", options.Value.UserServiceName);
            var addressList = address.First().AddressList;
            var host        = addressList.Any() ?
                              addressList.First().ToString() : address.First().HostName.Substring(0, address.First().HostName.Length - 1);
            var port = address.First().Port;

            _userServiceUrl = $"http://{host}:{port}";
        }
Example #21
0
        public UserService(IHttpClient httpClient, IOptions <ServiceDiscoveryOptions> serviceDiscoveryOptions, IDnsQuery dnsQuery, ILogger <UserService> logger)
        {
            _httpClient = httpClient;

            var address = dnsQuery.ResolveService("service.consul", serviceDiscoveryOptions.Value.UserServiceName);

            var addressList = address.First().AddressList;
            var host        = addressList.Any() ? addressList.First().ToString() : address.First().HostName;//这里返回的的localhost后为什么多个 "."


            var port = address.First().Port;

            _userServiceUrl = $"http://{host.Replace(".", "")}:{port}";

            _logger = logger;
        }
Example #22
0
        public ContactService(IHttpClient httpClient,
                              IOptions <ServiceDiscoveryOptions> serviceDiscoveryOptions,
                              IDnsQuery dnsQuery,
                              ILogger <ContactService> logger)
        {
            _httpClient = httpClient;
            _logger     = logger;

            var result = dnsQuery.ResolveService("service.consul",
                                                 serviceDiscoveryOptions.Value.ContactServiceName);
            var addressList = result.First().AddressList;
            var address     = addressList.Any() ? addressList.First().ToString() : result.First().HostName.TrimEnd('.');
            var port        = result.First().Port;

            _contactServiceUrl = $"http://{address}:{port}/";
        }
Example #23
0
        public BaseService(ILogger logger, IHttpClient httpClient, IDnsQuery dnsQuery, IHostingEnvironment env, string serviceName, string localDebugAddress)
        {
            _logger     = logger;
            _httpClient = httpClient;
            _serviceUrl = null;

            var result = dnsQuery.ResolveService("service.consul", serviceName).ToList();

            if (result.Count > 0)
            {
                var address = result.First().AddressList.FirstOrDefault();
                var port    = result.First().Port;
                if (address == null || string.IsNullOrWhiteSpace(address.ToString()) || !address.ToString().Contains("."))
                {
                    address = result.Last().AddressList.FirstOrDefault();
                    port    = result.Last().Port;
                }

                _serviceUrl = $"http://{address}:{port}";
                if (env.IsDevelopment())
                {
                    _serviceUrl = $"http://{localDebugAddress}:{port}";
                }
            }

            if (string.IsNullOrWhiteSpace(_serviceUrl))
            {
                if (serviceName.Equals("Service1"))
                {
                    _serviceUrl = $"http://{localDebugAddress}:{5001}";
                }
                if (serviceName.Equals("Service2"))
                {
                    _serviceUrl = $"http://{localDebugAddress}:{5002}";
                }

                if (serviceName.Equals("IdentityServer"))
                {
                    _serviceUrl = $"http://{localDebugAddress}:{5003}";
                }

                _logger.Debug($"localDebugAddress:{serviceName}_{localDebugAddress}");
            }

            _logger.Debug($"serviceName:{serviceName}_{_serviceUrl}");
        }
Example #24
0
        public static string ResolveDefaultServiceUrl(this IDnsQuery dnsQuery, string serviceName, string scheme = "http")
        {
            var addresses = dnsQuery.ResolveService("service.consul", serviceName);
            var address   = addresses.FirstOrDefault();

            if (address == null)
            {
                return(null);
            }

            var host = address.AddressList.Any()
                ? address.AddressList.FirstOrDefault()?.ToString()
                : address.HostName;
            var port = address.Port;

            return($"{scheme}://{host}:{port}");
        }
Example #25
0
        public UserService(HttpClient httpClient, IDnsQuery dns, IOptions <ServiceDiscoveryOptions> options)
        {
            _httpClient = httpClient;
            _dns        = dns ?? throw new ArgumentNullException(nameof(dns));
            _options    = options ?? throw new ArgumentNullException(nameof(options));

            var result = _dns.ResolveService("service.consul", _options.Value.UserServiceName);

            /*
             * 如果服务注册用的是localhost,那么AddressList为空,则取HostName
             * 必须是ip地址,比如127.0.0.1
             */
            var addressList = result.First().AddressList;
            //var address = result.First().AddressList.FirstOrDefault();

            var address = addressList.Any() ?
                          addressList.FirstOrDefault().ToString() :
                          result.First().HostName;
            var port = result.First().Port;

            _userServiceUrl = $"http://{address}:{port}";
        }