ASP.NET Core分布式项目实战

默认教学计划
1066人加入学习
(33人评价)
价格 ¥398.00
教学计划

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;

 

host 如果通过hostname去取,会多一个点,这里注意下

[展开全文]

引用:

http://michaco.net/blog/ServiceDiscoveryAndHealthChecksInAspNetCoreWithConsul

[展开全文]

在user.identity的userService类中,

构造方法里

var address= dnsQuery.ResolveService("", options.Value.UserServiceName);
  var host = address.First().AddressList.FirstOrDefault();

这两句代码:address.First().AddressList.FirstOrDefault();可能获取到host为空,采用下面的方法

也可以获去到,首先要添加Consul的nuget包

List<Uri> _serverUrls = List<Uri>();
var consuleClient = new ConsulClient(c => c.Address = new Uri("http://127.0.0.1:8500"));
var services = consulClient.Agent.Services().Result.Response;
foreach (var service in services)
{
    var isSchoolApi = service.Value.Tags.Any(t => t == "School") &&
                      service.Value.Tags.Any(t => t == "Students");
    if (isSchoolApi)
    {
        var serviceUri = new Uri($"{service.Value.Address}:{service.Value.Port}");
        serverUrls.Add(serviceUri);
    }
}

而且要在注册的时候添加tag.代码如下

 var registration = new AgentServiceRegistration
                {
                    Checks = new[] { httpCheck },
                    Address = addr.Host,
                    ID = serverId,
                    Name = ServiceOptions.Value.ServiceName,
                    Port = addr.Port,
                    Tags=new[] {"user_api"}
                   
                };

 

[展开全文]

授课教师

程序员

课程特色

下载资料(2)
视频(144)
讨论(1)
图文(2)