消息中心方案

本文探讨了消息中心的构建,包括模版定义、消息日志模型、服务间通信以及前端接收消息的两种方式。重点讨论了服务如何发送消息,消息中心的消费方式,以及使用MQ进行服务间通信的问题。还提到了邮件、钉钉和IM等不同渠道的消息处理,并涉及WebSocket在实时推送中的应用。

需求技术拆解:

0 业务启动后新增模版到消息中心,用户填写消息中心模版—

消息中心的模版model定义

消息日志model,

1 业务发送消息数据格式,与服务讨论消息的属性字段

 

2 消息中心模版表model,属性字段

 

3 消息中心解析type3(站内信)的消息,并分发给前端

方式一:前端订阅消息队列,通过网关转发到消息队列(网关是否可以支持)

方式二:前端通过ws连接消息中心获取消息(多页面连接,性能开销)

服务发送消息

 

 

5 消息中心消费端是否单独开启进程channel.start_consuming(),

无法通过子线程或子进程获取监听的消息

 

尝试本地队列Queue

add_callback_threadsafe

 

 

进程间是否需要通信,执行通知记录日志

 

6 是否启用多个线程:mail、dingding、IM、前端by用户id

 

7 消息队列,调试,定位、监控、部署、集群

 

8 服务之间通过MQ通信是否走网关,路由的协议是否支持

 

9 公共账号:

mail: 

阿里邮箱发件设置方式

EMAIL_HOST_USER = '327411586@qq.com'

EMAIL_HOST_PASSWORD = 'mdgzdsktsoavbiba'

EMAIL_HOST = 'smtp.qq.com'

 

dingding

 

 

 

—mq—

 

消息中心获取消息(保持mq-conn)

 

—解析消息—

 

notificationSender

 

 

Notification:

1 Mail : python mail

Django mail

 

2 Dingding

 

3 IM(站内信)

 

pika

django channel 

asgi、websocket

 

 

使用pika监听消息队列中的消息

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    ChatConsumer().send(msg)

channel.basic_consume(
    queue='hello', on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
# chat/consumers.py
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer

class ChatConsumer(WebsocketConsumer):
    def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name

        # Join room group
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )

        self.accept()

    def disconnect(self, close_code):
        # Leave room group
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )

    # Receive message from WebSocket
    def receive(self, text_data):
        text_data_json = json.loads(text_data)
        message = text_data_json['message']

        # Send message to room group
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name,
            {
                'type': 'chat_message',
                'message': message
            }
        )

    # Receive message from room group
    def chat_message(self, event):
        message = event['message']

        # Send message to WebSocket
        self.send(text_data=json.dumps({
            'message': message
        }))

 

然后根据websocket推送给前端

 

When a user posts a message, a JavaScript function will transmit the message over WebSocket to a ChatConsumer. The ChatConsumer will receive that message and forward it to the group corresponding to the room name. Every ChatConsumer in the same group (and thus in the same room) will then receive the message from the group and forward it over WebSocket back to JavaScript, where it will be appended to the chat log.

Several parts of the new ChatConsumer code deserve further

 

 

https://channels.readthedocs.io/en/latest/tutorial/part_2.html#enable-a-channel-layer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值