Skip to content

Commit

Permalink
feat:服务注册发现
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiaolulu committed Aug 30, 2024
1 parent 6542179 commit c0e992b
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
80 changes: 80 additions & 0 deletions unit-executor/logs/Unit-Executor.log
Original file line number Diff line number Diff line change
Expand Up @@ -4201,3 +4201,83 @@
"tester": "tester"
}

2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 相等对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于等于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于等于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不等于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不为空对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 为空对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 字符串类型相等对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度相等对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于等于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于等于对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含常见类型对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 类型匹配对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 正则匹配对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 前缀相等对象已经在service_registry中注册!
2024-08-30 11:18:12 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 后缀相等对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 相等对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于等于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于等于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不等于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不为空对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 为空对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 字符串类型相等对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度相等对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于等于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于等于对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含常见类型对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 类型匹配对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 正则匹配对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 前缀相等对象已经在service_registry中注册!
2024-08-30 11:18:34 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 后缀相等对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 相等对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于等于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于等于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不等于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不为空对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 为空对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 字符串类型相等对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度相等对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于等于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于等于对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含常见类型对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 类型匹配对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 正则匹配对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 前缀相等对象已经在service_registry中注册!
2024-08-30 11:19:06 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 后缀相等对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 相等对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 大于等于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 小于等于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不等于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 不为空对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 为空对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 字符串类型相等对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度相等对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度大于等于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 长度小于等于对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 包含常见类型对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 类型匹配对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 正则匹配对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 前缀相等对象已经在service_registry中注册!
2024-08-30 11:19:08 [MainThread]-[registry._register:39]-[INFO]:组件注册 -> 后缀相等对象已经在service_registry中注册!
Empty file.
20 changes: 20 additions & 0 deletions unit-executor/register/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import abc


class Register(metaclass=abc.ABCMeta):

@abc.abstractmethod
def register(self, name, id, address, port, tags, check):
pass

@abc.abstractmethod
def deregister(self, service_id):
pass

@abc.abstractmethod
def get_all_service(self):
pass

@abc.abstractmethod
def filter_service(self, filter):
pass
67 changes: 67 additions & 0 deletions unit-executor/register/consul.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import uuid
from abc import ABC
import consul
import requests
import random
from register import base


class ConsulRegister(base.Register, ABC):
def __init__(self, host, port):
self.host = host
self.port = port
self.consul = consul.Consul(host=host, port=port)

def register_consul(
self,
name: str,
address: str,
port: int,
tags: str,
check: dict
):

if check is None:
check = {
"GRPC": f"{address}:{port}",
"GRPCUseTLS": False,
"Timeout": "5s",
"Interval": "5s",
"DeregisterCriticalServiceAfter": "15s"
}
else:
check = check

service_id = uuid.uuid4().hex
return self.consul.agent.service.register(
name=name,
service_id=service_id,
address=address,
port=port,
tags=tags,
check=check
)

def deregister(self, service_id):
return self.consul.agent.service.deregister(service_id)

def get_all_service(self):
return self.consul.agent.services()

def filter_service(self, filter):
url = f"http://{self.host}:{self.port}/v1/agent/services"
params = {
"filter": filter
}
return requests.get(url, params=params).json()

def get_host_port(self, filter):
url = f"http://{self.host}:{self.port}/v1/agent/services"
params = {
"filter": filter
}
data = requests.get(url, params=params).json()
if data:
service_info = random.choice(list(data.values()))
return service_info["Address"], service_info["Port"]
return None, None

0 comments on commit c0e992b

Please sign in to comment.