class AddService:
name = 'add_service'
@event_handler('productor_service', 'a')
@event_handler('productor_service', 'b')
def handle_message(self, payload: str):
logger.debug(f'handle_message {payload}')
@rpc
@context_id_dec
def add(self, a: int, b: int, /, context_id: str) -> int | None:
try:
logger.debug(f'收到一个加法计算请求:{a},{b},{asgi_context_id.get()}')
# time.sleep(1000)
return a+b
except Exception as error:
logger.exception(error)
return None
@http('GET', '/hi')
def hi(self, request):
return "hello world"
@http('GET', '/ha')
def ha(self, request):
time.sleep(10)
return 'ok'
nameko run services:AddService --config ./config.yaml
使用该命令,发现只有一个连接:
class TrashCanService:
name = 'trash_scan_service'
@event_handler('hi_api', 'rubbish')
def receive(self, message: str):
logger.debug(f'开始消费消息, {message}')
response = requests.get('http://127.0.0.1:50010')
logger.debug(f'{message}, {response.text}')
receive.create_queue_only = True
@event_handler('drop_throw_rubbish_service', 'drop_rubbish')
def receive_by_rpc(self, message: str):
pass
class AddService:
name = 'add_service'
@event_handler('productor_service', 'a')
@event_handler('productor_service', 'b')
def handle_message(self, payload: str):
logger.debug(f'handle_message {payload}')
@rpc
@context_id_dec
def add(self, a: int, b: int, /, context_id: str) -> int | None:
try:
logger.debug(f'收到一个加法计算请求:{a},{b},{asgi_context_id.get()}')
# time.sleep(1000)
return a+b
except Exception as error:
logger.exception(error)
return None
@http('GET', '/hi')
def hi(self, request):
return "hello world"
@http('GET', '/ha')
def ha(self, request):
time.sleep(10)
return 'ok'
但是这样,两个服务,一共两个 amqp 连接:
所以 nameko 是一个服务一个 amqp 连接,还是每个队列 work 函数一个 amqp 连接?
这个问题的结论就是:nameko 是一个服务一个 amqp 连接