Exemplo n.º 1
0
 def RegisterService(self, name, host, port, tags=None):
     tags = tags or []
     self._consul.agent.service.register(
         name,
         name,
         host,
         port,
         tags,
         check=consul.Check().tcp(host, port, "5s", "30s", "30s")
     )
Exemplo n.º 2
0
 def getTags(this, name, host, port, tags=None):
     tags = tags or []
     this._consul.agent.service.register(name,
                                         name,
                                         host,
                                         port,
                                         tags,
                                         check=consul.Check().tcp(
                                             host, port, "5s", "30s",
                                             "30s"))
Exemplo n.º 3
0
 def RegisterService(self, name, host, port, tags=[],
                     interval="5s", timeout="30s", deregister="30s"):
     self._consul.agent.service.register(
         name,
         name,
         host,
         port,
         tags,
         check=consul.Check().tcp(host, port, interval, timeout, deregister)
     )
Exemplo n.º 4
0
 def RegisterService(self, name, host, port, tags=None):
     tags = tags or []
     # 注册服务
     self._consul.agent.service.register(
         name,
         name,
         host,
         port,
         tags,
         # 健康检查ip端口,检查时间:5,超时时间:30,注销时间:30s
         check=consul.Check().tcp(host, port, "5s", "30s", "30s"))
Exemplo n.º 5
0
 def register(self):
     """Register the service into Consul."""
     self.consul.agent.service.register(
         name=self.service_name,
         service_id=self.instance_id,
         address=platform.node(),
         port=grpc_config["port"],
         check=consul.Check().ttl("{}s".format(self.heartbeat_seconds)))
     self.registered = True
     self.interval = Interval(8, self.pass_heartbeat)
     self.pass_heartbeat()
Exemplo n.º 6
0
 def RegisterService(self, name, service_id, host, port, check_url, tags=None):
     tags = tags or []
     # 注册服务
     self._consul.agent.service.register(
         name,
         service_id,
         host,
         port,
         tags,
         # 健康检查ip端口,检查时间:5,超时时间:30,注销时间:30s
         check=consul.Check().http(check_url, "5s", "5s"))
Exemplo n.º 7
0
 def setup_register(self):
     r_options = self.roptions.copy()
     host_addr = self.get_host_byname()
     serv_name = self.gen_serv_name(self.container.service_cls.name)
     r_options.setdefault('port', 80)
     r_options.setdefault('service_id', self.serverid)
     r_options.setdefault('address', host_addr or '127.0.0.1')
     check = consul.Check().tcp(r_options['address'], r_options['port'],
                                '5s', '10s', '10s')
     r_options.setdefault('check', check)
     self.instance.agent.service.register(serv_name, **r_options)
Exemplo n.º 8
0
 def register(self, name, host, port):
     """注册服务"""
     self._consul.agent.service.register(name=name,
                                         service_id=name,
                                         address=host,
                                         port=port,
                                         check=consul.Check().tcp(
                                             host=host,
                                             port=port,
                                             interval='5s',
                                             timeout='30s',
                                             deregister='30s'))
Exemplo n.º 9
0
 def register_service(self, name, host, port, tags=None):
     tags = tags or []
     # 注册服务
     id = "{}_{}_{}".format(name, host, port)
     return self.consul.agent.service.register(
         name,
         id,
         host,
         port,
         tags,
         # 健康检查ip端口,检查时间:5,超时时间:30,注销时间:30s
         # check=consul.Check().tcp(host, port, "5s", "5s", "60s"))
         check=consul.Check().tcp(host, port, "5s", "5s"))
Exemplo n.º 10
0
 def register_service(self, service_name, host, port, tags=None):
     tags = tags or []
     # 注册服务
     self._consul.agent.service.register(
         service_name,
         service_name + '_' +
         time.strftime("%Y-%m-%d~%H:%M:%S", time.localtime()),  # service_id
         host,
         port,
         tags,
         # 健康检查ip端口,检查时间:5,超时时间:30,注销时间:30s
         check=consul.Check().tcp(host, port, "5s", "30s", "30s"))
     logger.debug(f"[consul] register service {service_name}@{host}:{port}")
Exemplo n.º 11
0
 def register(self, name, service_id, address, port, tags, http_check_url):
     try:
         self.consul.agent.service.register(
             name,
             service_id=service_id,
             address=address,
             port=port,
             tags=tags,
             check=consul.Check().http(http_check_url, '10s', '20s', '20s')
         )
         self.registered = True
         logging.critical('Successful registered to Consul: {}:{}'.format(address, port))
     except:
         self.registered = False
Exemplo n.º 12
0
def demo():
    host = "127.0.0.1"  # consul服务器的ip
    port = "8500"  # consul服务器对外的端口
    consul_client = ConsulClient(host, port)

    name = "maple"
    host = "127.0.0.1"
    port = 8510
    consul_client.registerService(name, host, port)

    check = consul.Check().tcp(host, port, "5s", "30s", "30s")
    print(check)
    res = consul_client.getService("maple")
    print(res)
Exemplo n.º 13
0
        self._consul.agent.service.register(
            name,
            name,
            host,
            port,
            tags,
            # 健康检查ip端口,检查时间:5,超时时间:30,注销时间:30s
            check=consul.Check().tcp(host, port, "5s", "30s", "30s"))

    def GetService(self, name):
        services = self._consul.agent.services()
        service = services.get(name)
        if not service:
            return None, None
        addr = "{0}:{1}".format(service['Address'], service['Port'])
        return service, addr

if __name__ == '__main__':
    host="127.0.0.1" #consul服务器的ip
    port="8500" #consul服务器对外的端口
    consul_client=Consul(host,port)

    name="maple"
    host="127.0.0.1" # 服务所在的ip
    port=8510  # 服务所在的端口
    consul_client.RegisterService(name,host,port)

    check = consul.Check().tcp(host, port, "5s", "30s", "30s")
    print(check)
    res=consul_client.GetService("maple")
    print(res)
Exemplo n.º 14
0
import requests
import uuid

app = FastAPI()
service_name = "consul-py"
service_id = service_name + '-' + str(uuid.uuid1())
# 实例consul类
c = consul.Consul(host="192.168.9.50", port=8500)
# 向server注册
service = c.agent.service
service.register(name=service_name,
                 address="192.168.9.52",
                 port=8000,
                 service_id=service_id,
                 check=consul.Check().tcp(host="192.168.9.52",
                                          port=8000,
                                          interval="3s",
                                          deregister="1m"))


def get_other_instance_result(service_name, prefix_url):
    # 调用consul api
    consul_service = c.catalog.service(service_name)
    print(consul_service)
    consul_service_instances = []
    # 获取实例的真实ip
    if len(consul_service[1]) > 0:
        instance_address_infos = consul_service[1]
        for instance_address_info in instance_address_infos:
            consul_service_instances.append(
                "http://" + instance_address_info["ServiceTaggedAddresses"]
                ["lan_ipv4"]["Address"] + ":" +
Exemplo n.º 15
0
# -*- coding: utf-8 -*-
# !/usr/bin/env python

import consul

c = consul.Consul(host='192.168.3.27', port=8500, scheme='http')
c.agent.service.register("test-tmj",
                         "test-tmj",
                         "192.168.3.80",
                         18888, ["aaa", "bbbb"],
                         check=consul.Check().tcp("192.168.3.80", 18888, "5s",
                                                  "30s", "30s"))
data = c.catalog.service('lxpay-gateway')
for value in data[1]:
    print("accountSrv addr: " + value['ServiceAddress'], value['ServicePort'])