第三篇,注册Consul服务的代码
nuget的信息:
Consul 版本:0.7.2.6
System.Configuration.ConfigurationManager 版本:4.5.0
using System; using System.Configuration; using System.Net; using System.Xml; namespace Core.Consul { public class ServiceManagerSection : IConfigurationSectionHandler { private static ServiceManagerSection _instance = null; public static ServiceManagerSection Instance { get { if (_instance == null) { _instance = ConfigurationManager.GetSection("serviceManager") as ServiceManagerSection; } return _instance; } } public string Address { get; set; } public bool IsRegister { get; set; } public string Service { get; set; } public string IP { get; set; } public int Port { get; set; } public object Create(object parent, object configContext, XmlNode section) { var result = new ServiceManagerSection(); bool isRegister = true; if (section.Attributes["register"] != null && !string.IsNullOrEmpty(section.Attributes["register"].Value)) { bool.TryParse(section.Attributes["register"].Value, out isRegister); } result.IsRegister = isRegister; if (section.Attributes["address"] != null && !string.IsNullOrEmpty(section.Attributes["address"].Value)) { result.Address = section.Attributes["address"].Value; } else { if (result.IsRegister) { throw new Exception("服务管理,未配置服务中心地址[address]"); } } if (section.Attributes["service"] != null && !string.IsNullOrEmpty(section.Attributes["service"].Value)) { result.Service = section.Attributes["service"].Value; } else { if (result.IsRegister) { throw new Exception("服务管理,未配置服务名称[service]"); } } if (section.Attributes["ip"] != null && !string.IsNullOrEmpty(section.Attributes["ip"].Value)) { IPAddress ip; if (IPAddress.TryParse(section.Attributes["ip"].Value, out ip)) { result.IP = section.Attributes["ip"].Value; } else { throw new Exception("服务管理,IP配置不正确[ip]"); } } else { if (result.IsRegister) { throw new Exception("服务管理,未配置IP[ip]"); } } if (section.Attributes["port"] != null && !string.IsNullOrEmpty(section.Attributes["port"].Value)) { result.Port = int.Parse(section.Attributes["port"].Value); } else { if (result.IsRegister) { throw new Exception("服务管理,未配置端口[port]"); } } return result; } } }
using Consul; using System; using System.Collections.Generic; namespace Core.Consul { public class ServiceManager { static ServiceManager() { } public static void Register() { if (ServiceManagerSection.Instance == null) return; string name = ServiceManagerSection.Instance.Service; string address = ServiceManagerSection.Instance.IP; int port = ServiceManagerSection.Instance.Port; string checkHttpAddress = string.Empty; if (ServiceManagerSection.Instance.IsRegister) ConsulManager.Register(name, address, port, checkHttpAddress); } public static void Remove(string address, int port) { ConsulManager.Remove(address, port); } private static Dictionary<string, List<AgentService>> GetServices() { Dictionary<string, List<AgentService>> cache = new Dictionary<string, List<AgentService>>(); var services = ConsulManager.FindAll(); foreach (var service in services) { if (!cache.ContainsKey(service.Value.Service)) { cache[service.Value.Service] = new List<AgentService>(); } cache[service.Value.Service].Add(service.Value); } return cache; } private static string BuildUrl(AgentService service) { string result = ""; if (service.Port == 80) { result = string.Format("http://{0}/", service.Address); } else { result = string.Format("http://{0}:{1}/", service.Address, service.Port); } return result; } } }
using Consul; using System; using System.Collections.Generic; using System.Text; namespace Core.Consul { internal class ConsulManager { private static ConsulClient client = null; static ConsulManager() { if (client == null) { var address = ServiceManagerSection.Instance.Address; ConsulClientConfiguration config = new ConsulClientConfiguration() { Address = new Uri(address), //Token = "245d0a09-7139-bbea-aadc-ff170a0562b1" }; client = new ConsulClient(config); } } private ConsulManager() { } /// <summary> /// 注册服务 /// </summary> public static void Register(string name, string address, int port, string checkHttpAddress = "") { var service = new AgentServiceRegistration(); service.Name = name; service.Address = address; service.Port = port; service.ID = address + ":" + port; AgentServiceCheck checkHttp = new AgentServiceCheck(); if (string.IsNullOrEmpty(checkHttpAddress)) { if (port == 80) { checkHttpAddress = string.Format("http://{0}/default/test", address); } else { checkHttpAddress = string.Format("http://{0}:{1}/default/test", address, port); } } checkHttp.HTTP = checkHttpAddress; checkHttp.Interval = new TimeSpan(0, 0, 10); checkHttp.DeregisterCriticalServiceAfter = new TimeSpan(0, 0, 120); service.Checks = new List<AgentServiceCheck>() { checkHttp }.ToArray(); var result = client.Agent.ServiceRegister(service).Result; if (result.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception($@"注册{address}-{name}服务失败"); } } public static void Remove(string address, int port) { string id = address + ":" + port; client.Agent.ServiceDeregister(id); } public static Dictionary<string, AgentService> FindAll() { var result = client.Agent.Services().Result.Response; return result; } } }
不懂可以扫二维码交流


浙公网安备 33010602011771号