def _shedual_task(self): try: from rocketmq.client import PushConsumer except Exception as e: # print(traceback.format_exc()) raise ImportError(f'rocketmq包 只支持linux和mac {e}') consumer = PushConsumer(f'g-{self._queue_name}') consumer.set_namesrv_addr(frame_config.ROCKETMQ_NAMESRV_ADDR) consumer.set_thread_count(1) consumer.set_message_batch_max_size(self._concurrent_num) self._publisher = RocketmqPublisher(self._queue_name) def callback(rocketmq_msg): # self.logger.debug(f'从rocketmq的 [{self._queue_name}] 主题的queue_id {rocketmq_msg.queue_id} 中 取出的消息是:{rocketmq_msg.body}') self._print_message_get_from_broker('rocketmq', rocketmq_msg.body) kw = { 'body': json.loads(rocketmq_msg.body), 'rocketmq_msg': rocketmq_msg } self._submit_task(kw) consumer.subscribe(self._queue_name, callback) consumer.start() while True: time.sleep(3600)
class RocketmqPushConsumer(): consumer = None def __init__(self, conf): pid = uuid.uuid1().hex self.consumer = PushConsumer(pid) self.consumer.set_namesrv_addr(conf) # 创建长连接 def start(self): self.consumer.start() #关闭链接 def close(self): self.consumer.shutdown() def callback(self, msg): print('self.callback') print(msg.body) return msg # 获取消息 def getMessage(self, topic, callback=None): if not callback: callback = self.callback self.consumer.subscribe(topic, callback) self.start()
def consume(self): consumer = PushConsumer('CID_XXX') consumer.set_name_server_address('127.0.0.1:9876') consumer.subscribe('YOUR-TOPIC', self.callback) consumer.start() while True: time.sleep(3600) consumer.shutdown()
def start_consume_message(): consumer = PushConsumer('consumer_group') consumer.set_name_server_address('127.0.0.1:9876') consumer.subscribe('TopicTest', callback) print('start consume message') consumer.start() while True: time.sleep(3600)
def start(self, topic, name_server, consumer): Consumer = PushConsumer(consumer) try: Consumer.set_namesrv_addr(name_server) except: traceback.print_exc() Consumer.set_name_server_address(name_server) Consumer.subscribe(topic, self.run) Consumer.start() print("run {} task success and into the loop, wait for the message!". format(name_server)) while True: # 这里是必须的,而且每个实例都必须有单独的while阻塞,不可以共用. TODO pass
def consume_msg_once(): """ 消费方式PushConsumer(即时消费)(不可重复消费) """ def callback(msg): print(msg) consumer = PushConsumer('PID-001') consumer.set_namesrv_addr(con_str) consumer.subscribe("test", callback) consumer.start() while True: time.sleep(30) consumer.shutdown()
class ScConsumer(metaclass=Singleton): """RocketMQ message Consumer""" _consumer: PushConsumer = None _group_id: str = None _name_server_ip: str = None _port: int = None _topic: str = None def __init__(self): self._group_id = config.get("rocketmq.group_id") self._name_server_ip = config.get("rocketmq.name_server_ip") self._port = config.get("rocketmq.name_server_port") self._consumer = PushConsumer(self._group_id, orderly=True) self._consumer.set_name_server_address("{}:{}".format( self._name_server_ip, self._port)) self._topic = config.get("rocketmq.msg_topic") def _callback(self, msg): msg_body = str(msg.body, 'UTF-8') logging.getLogger(__name__).info("consume message: id: %s, body: %s", msg.id, msg_body) msg_data = json.loads(msg_body) event_type = msg_data.get('event_type') event_name = msg_data.get('event_name') handler = HandlerFactory.get_handler(event_type, event_name) try: logging.getLogger(__name__).info("using handler: %s", type(handler)) handler.handle(msg_data) return ConsumeStatus.CONSUME_SUCCESS except ConsumeException as e: logging.getLogger(__name__).exception( "failed to handle message:%s, event type: %s, event name: %s", msg.id, event_type, event_name, exc_info=e) return ConsumeStatus.RECONSUME_LATER def start(self): logging.getLogger(__name__).info("subscribe to topic: %s", self._topic) self._consumer.subscribe(self._topic, self._callback) logging.getLogger(__name__).info("connecting to %s:%s", self._name_server_ip, self._port) self._consumer.start() def shutdown(self): self._consumer.shutdown()
def serve(self): self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) inventory_pb2_grpc.add_InventoryServicer_to_server( InventoryService(), self.server) health_servicer = health.HealthServicer() health_pb2_grpc.add_HealthServicer_to_server(health_servicer, self.server) self.server.add_insecure_port(f'[::]:{self.SERVICE_PORT}') signal.signal(signal.SIGINT, self.onExit) signal.signal(signal.SIGTERM, self.onExit) logger.info("Start Inventory Service at {}:{}".format( self.SERVICE_HOST, self.SERVICE_PORT)) self.server.start() self.register() consumer = PushConsumer("mxshop_inventory") consumer.set_name_server_address( f"{settings.RocketMQ_HOST}:{settings.RocketMQ_PORT}") consumer.subscribe("order_reback", reback_inv) consumer.start() self.server.wait_for_termination() consumer.shutdown()
def serve(self): config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', # 全部 'param': 1, # 1 开启全部采样 0 表示关闭全部采样 }, 'local_agent': { 'reporting_host': '192.168.0.14', 'reporting_port': '6831', }, 'logging': True, }, service_name='order-srv', validate=True, ) tracer = config.initialize_tracer() self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=40)) tracing_interceptor = open_tracing_server_interceptor(tracer) self.server = intercept_server(self.server, tracing_interceptor) order_pb2_grpc.add_OrderServicer_to_server(OrderService(), self.server) health_servicer = health.HealthServicer() health_pb2_grpc.add_HealthServicer_to_server(health_servicer, self.server) self.server.add_insecure_port(f'[::]:{self.SERVICE_PORT}') signal.signal(signal.SIGINT, self.onExit) signal.signal(signal.SIGTERM, self.onExit) logger.info("Start Order Service at {}:{}".format( self.SERVICE_HOST, self.SERVICE_PORT)) self.server.start() self.register() #监听超时订单消息 consumer = PushConsumer("mxshop_order") consumer.set_name_server_address( f"{settings.RocketMQ_HOST}:{settings.RocketMQ_PORT}") consumer.subscribe("order_timeout", order_timeout) consumer.start() self.server.wait_for_termination() consumer.shutdown()
def _shedual_task(self): consumer = PushConsumer(f'g-{self._queue_name}') consumer.set_namesrv_addr(frame_config.ROCKETMQ_NAMESRV_ADDR) consumer.set_thread_count(1) consumer.set_message_batch_max_size(1) self._publisher = RocketmqPublisher(self._queue_name) def callback(rocketmq_msg): self.logger.debug( f'从rocketmq的 [{self._queue_name}] 主题的queue_id {rocketmq_msg.queue_id} 中 取出的消息是:{rocketmq_msg.body}' ) kw = { 'body': json.loads(rocketmq_msg.body), 'rocketmq_msg': rocketmq_msg } self._submit_task(kw) consumer.subscribe(self._queue_name, callback) consumer.start() while True: time.sleep(3600)
import time from rocketmq.client import PushConsumer, ConsumeStatus import os def callback(msg): print("?????\n") print(msg.id, msg.topic, msg.body) return ConsumeStatus.CONSUME_SUCCESS consumer = PushConsumer(os.environ['IP']) consumer.set_name_server_address('39.105.21.114:9876') consumer.subscribe('s2c39d105d21d114', callback) consumer.subscribe('HTTPServer', callback) consumer.start() while True: time.sleep(1) consumer.shutdown()
import time from rocketmq.client import PushConsumer, ConsumeStatus def callback(msg): print((msg.body).decode("utf-8")) return ConsumeStatus.CONSUME_SUCCESS consumer = PushConsumer('LOG_MONITOR') consumer.set_name_server_address('127.0.0.1:9876') consumer.subscribe('LOG_MONITOR_TOPIC', callback) consumer.start() while True: time.sleep(3600) # consumer.shutdown()
import time from rocketmq.client import PushConsumer, ConsumeStatus def callback(msg): print(msg.id, msg.body) return ConsumeStatus.CONSUME_SUCCESS consumer = PushConsumer('CID_XXX') consumer.set_name_server_address('127.0.0.1:9876') consumer.subscribe('YOUR-TOPIC', callback) consumer.start() while True: time.sleep(3600) consumer.shutdown()