消息中心方案

需求技术拆解:

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

发布了108 篇原创文章 · 获赞 33 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览