Esempio n. 1
0
        public async Task Register(URL url)
        {
            var service = new ConsulService(url);
            await _client.RegisterService(service);

            await _client.CheckPass(service.Id);

            _heartbeat.AddServiceId(service.Id);
        }
Esempio n. 2
0
        AgentServiceRegistration ConvertService(ConsulService service)
        {
            var registration = new AgentServiceRegistration
            {
                Address = service.Address,
                ID      = service.Id,
                Name    = service.Name,
                Port    = service.Port,
                Tags    = service.Tags.ToArray()
            };
            var check = new AgentServiceCheck {
                TTL = TimeSpan.FromSeconds(service.Ttl)
            };

            registration.Check = check;
            return(registration);
        }
Esempio n. 3
0
        /**
         * 根据服务的url生成consul对应的service
         *
         * @param url
         * @return
         */
        public static ConsulService BuildService(URL url)
        {
            ConsulService service = new ConsulService
            {
                Address = url.Ip,
                Id      = ConvertServiceId(url),
                Name    = url.ServiceName,
                Port    = url.Port,
                Ttl     = 30,
            };
            List <string> tags = new List <string>
            {
                "protocol_" + url.Protocol,
                "URL_" + url.ToString(),
                "nodeType_" + url.GetParameter(Constants.SideKey, "")
            };

            service.Tags = tags;

            return(service);
        }
Esempio n. 4
0
        /**
         * 根据service生成URL
         *
         * @param service
         * @return
         */
        public static URL BuildUrl(ConsulService service)
        {
            URL url = null;

            foreach (var tag in service.Tags)
            {
                if (tag.StartsWith("URL_"))
                {
                    var encodeUrl = tag.Substring(tag.IndexOf("_") + 1);
                    url = URL.ValueOf(URL.Decode(encodeUrl));
                }
            }
            if (url == null)
            {
                var param = new Dictionary <string, string>();
                var group = service.Name.Substring("providers_".Length);
                param.Add(Constants.GroupKey, group);
                param.Add("nodeType", service.Tags[2]);
                string protocol = ConsulUtils.GetProtocolFromTag(service.Tags[0]);
                url = new URL(protocol, service.Address, service.Port,
                              ConsulUtils.GetPathFromServiceId(service.Id), param);
            }
            return(url);
        }
Esempio n. 5
0
 public Task RegisterService(ConsulService service)
 {
     return(_client.Agent.ServiceRegister(ConvertService(service)));
 }