当前位置:网站首页>When the publish / subscribe mode encounters NET
When the publish / subscribe mode encounters NET
2022-06-27 16:39:00 【51CTO】

Daily development , We usually deploy multiple applications with different roles in the same architecture , These applications need some mechanism to notify each other of what happened . These events may be temporary ( Changes made temporarily at run time ), It may also be a database event ( Due to changes in the database ). How to deal with such complex and changeable distributed events , It has always been a rather thorny problem . And this is the release - Where subscription design patterns come into play .
as everyone knows , Release - Subscription patterns are among many design patterns , Probably the most common 、 The most famous one . It defines a one to many relationship , Let multiple subscriber objects listen to a topic object at the same time , When the state of the topic object changes, all subscriber objects that subscribe to it will be notified , So that they can update themselves automatically .
stay “ Release - subscribe ” In the messaging paradigm of , Sender of message ( Publisher ) Do not know the intended recipient ( subscriber ). Besides , There is no direct interaction between publisher and subscriber applications , It depends on what is called “ The theme ” Public media . therefore , Release - The subscription pattern is a loosely coupled messaging cdxxd Model .
It's based on these characteristics , Release - A subscription model ( Referred to as “ Release / subscribe ”) It has become a way to build an enterprise .NET An indispensable tool for applications .
PART 01
Distributed event driven architecture exploration
To design a distributed event driven architecture , Developers used to prefer one of the following .
1、RDBMS Provide data notification
If data storage is limited to relational databases , The database notification function can indeed . In addition to allowing events to be registered with the database server , This function also updates the result set in the database 、 When changes occur due to addition or deletion , Notify the application side .
But there's a problem ,RDBMS Is essentially non extensible , It can easily become a performance bottleneck for applications . Typically , Developers don't want the database to take on unnecessary extra tasks . Besides , The database notification function itself is also slow , And does not support runtime data sharing .
It's not hard to understand , Using a database as a messaging medium is not the best design choice .
2、 Message queue
Another option is to introduce separate message queues into the architecture . Although these message queues do a good job of helping you transfer messages between applications , But these queues are not data centric , That is, they do not monitor data changes in the database or any other source . Besides , These message queues cannot be extended with the application layer .
3、 Custom solution
The last remaining option is , Create a messaging platform that suits your needs . The idea of a customized solution may seem tempting at first glance , But it may be difficult to predict the time and resources required , Because building and managing a robust and extensible messaging platform is a very difficult task .
The crux of the problem is : Which solution is easier to merge 、 Scalable 、 Highly available and very reliable ?
PART 02
Using distributed cache as messaging platform
Here we share a message delivery platform using distributed caching NCache. The fact proved that , This is not only a simple and feasible solution , It is also a more modern way to integrate a robust messaging platform .NCache It is the only real local product currently available in the market .NET/.NET Kernel distributed cache . It is a distributed cache technology in memory , Extremely fast , Very scalable . It enables applications to handle extreme transaction loads , Without making the database a bottleneck . Besides , You can also use it NCache Process data in real time / flow .
NCache Usually deployed in N The middle layer of the layer architecture . The following figure shows a more intuitive display of its architecture level .

chart 1:N Layer architecture NCache
NCache Is a cache server cluster , Can be .NET Application and Java Applications provide tens of thousands of requests —— By storing frequently used data in memory , So as to effectively avoid a large number of database access .
below , In depth analysis NCache. First , Let's see NCache The principle of message bus as an event driven architecture .
1、NCache Event driven messages
The image below shows NCache How to act as .NET and Java The message bus function of the application .

chart 2:NCache As a messaging platform
In the architecture above ,NCache Cross platform communication by using fast and compact serialization , This serialization can be converted to .NET or Java Object to binary , Then transfer them to the cache cluster . therefore , This gives NCache How to support .NET Application and Java The inherent logic of application interaction . More information about this , Please search for portable data types by yourself .
NCache Manage publishing with the name of the event / Subscription design pattern , And provide different ways to propagate messages to other listening applications . Let's look at these two message types , To understand how distributed caches propagate them .
First , Consider the data changes that your application needs to listen for . because NCache Is also used .NET Key value storage technology , It provides the ability to update the application when any data changes in the cache . Because all this happens in memory , So there is no performance bottleneck . These data changes can be :
- Cache level item changes , Whether it's updating or deleting
- The entire cache level data changes
- Continuous query , You can register a similar one SQL Query for , To monitor whether the result set changes in the cache . If so , Will notify the application
- If the cluster changes as a result of any new node additions or deletions or crashes .( For management )
NCache It also allows the dependency registration of the database , Include SQL、Oracle and OleDb etc. . This helps keep the database and application cache data up to date . A complete list of supported dependency types , Please check the database dependency content , No more details here . These dependencies are data change notifications registered in different data stores ; however , It can make NCache Deal with it . You can also notify the database to NCache Data notification , So as to further enrich the project construction strategy .
On the other hand , If you only want to propagate simple messages to complex .NET or Java object , Then you should use custom messaging . ad locum , An application can generate data and trigger events , The interested listener will receive the event almost instantaneously . More about this , Readers can view articles related to custom events by themselves .
2、NCache Release / subscribe API
NCache Provides memory publishers / subscriber ( Release / subscribe ) Features and dedicated releases / Subscribe to the message store , To support the .NET Web Real time information sharing in applications , Ultimately help applications communicate better .
Release / Subscription mode provides a channel for interested users to publish and subscribe to messages , Naturally decouple publishers and subscribers . Now? , When NCache When acting as a messaging bus , Release / The subscription model benefits from the underlying NCache Distributed architecture and many convenient functions .
3、 Basic framework
Let's first get to know NCache Release / The basic components of subscription and how they work .NCache Release / The general process of subscribing to messages is as follows : First , Used by publishers ITopic Interface publishes topic messages . Next , Subscribers can create subscriptions to one or more topics , And receive relevant messages . After successful message delivery ,NCache Will receive a confirmation ; otherwise ,NCache Will continue to retry before the message expires ( If expiration information is set ). Undelivered messages will reside in the cache , Until the expired message is triggered , Please refer to the diagram below .

chart 3:NCache Release / Subscription message mechanism
4、 Subscription type
NCache For the release of / Subscription messages provide two different types of subscriptions , That is, non persistent subscription and persistent subscription . Besides ,NCache Support exclusive and shared subscription policies . The details are discussed below .
- Non durable subscriptions : By default , All subscriptions created on the topic are non persistent . It only sends the expected message to the subscriber before the connection is maintained . If the subscriber's connection is lost for any reason , You will not receive an old message when you rejoin the network . This type of subscription is exclusive , This means that a subscription belongs to only one subscriber
- Persistent subscription : It takes into account the loss of messages when the subscriber disconnects .NCache Keep the subscriber's subscription when the connection is lost . therefore , Subscribers can receive their published messages of interest when reconnecting
Persistent subscription provides two strategies :
- exclusive : One subscription at a time belongs to only one active subscriber
- Shared : A subscription can register multiple subscribers at the same time , And provide load sharing
PART 03
NCahce Release / Subscribe to application cases
Suppose there is an e-commerce store , Different suppliers regularly provide new products to the store . meanwhile , It also provides product sales and discounts . Store managers and customers interested in the products offered need to keep abreast of new products 、 Update on products for sale and discounts .NCache Release / The subscription function enables the distributed notification system in this scenario . So , You can first create NCache Dedicated release / Subscribe to the message store .
Next , Let's talk about using in the above scenario NCache Release / Subscribe to news API The step-by-step process of implementing distributed messages .
1、 Create a theme
As a first step , You need to create a theme , So that different suppliers can release new product updates . have access to NCache Medium ITopic Interface to create a new topic with a unique name . The following code shows how the publisher application uses methods CreateTopic To create a name newProducts The theme of .
Slide left and right to see the full code
If a topic with the provided name already exists , An instance of this topic will be used as ITopic return .
NCache Allows you to set the topic priority when creating a topic . When some events need to be communicated at a higher priority than others , It's very useful . for example , Information about manufactured goods is urgent ; Again , Product price updates due to discounts or sales are for seller / It is also the most important for the buyer . under these circumstances , You can set the topic priority to high when creating a topic , And can receive relevant notifications without delay .
2、 Release the news
After creating a theme , Publisher applications can use Publish Method to publish relevant messages to the topic . So , First, get the instance of the topic by specifying the topic name .NCache There are two delivery modes for publishing messages :
- All( Default ): Deliver the message to all registered subscribers . When broadcasting information is required , It's very useful .
- Any: Deliver the message to any registered subscriber .
Besides , To manage your releases effectively / Subscription cache storage space , You can also set the message expiration time .
In the following code , The publisher will be in the existing theme newProducts Broadcast news about new products on .
// Prerequisite : Cache connected
// The theme “newProducts” Created
string topicName = "newProducts"
// Get theme
ITopic productTopic = cache.MessagingService.GetTopic(topicName);
// Create an object to send in a message
Product product = FetchProductFromDB(10248);
// Create a new message corresponding to the order of the objects
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
Slide left and right to see the full code
3、 Subscribe to topic news
After creating a theme , A subscriber application can receive messages published to the topic by obtaining a subscription . Because it supports different types of subscriptions , Therefore, subscribers interested in non persistent subscriptions can use CreateSubscription Method ; For persistent subscriptions , You can use CreateDurableSubscription Method .
The following code shows how the subscriber application is themed newProducts Create subscription .MessageReceived Register a callback to perform any expected actions when a notification is received . for example , Subscribers can receive a sales notice at MessageReceived Update the product price in the callback .
// Prerequisite : Cache connected
// The theme “newProducts” Created
string topicName = "newProducts"
// Get theme
ITopic productTopic = cache.MessagingService.GetTopic(topicName);
// Create and register topics newProducts Subscribers
// Specify the callback function MessageReceived
ITopicSubscription orderSubscriber = orderTopic.CreateSubscription(MessageReceived);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
Slide left and right to see the full code
In the example above , Created a non persistent subscription . Besides , When you need to receive old messages from a subscribed topic when reconnecting , Subscribers can create persistent subscriptions .
NCache A pattern - based subscription method is also provided , among NCache Multiple wildcards are supported to subscribe to a single... In the provided mode / Multiple themes .
4、 Registration notice
NCache Enable publishers to understand the status of messages and the availability of topics . The publisher application can register the following notifications :
- MessageDeliveryFailure: If the message cannot be delivered due to any problems , Then notify the publisher
- OnTopicDeleted: Notify the publisher when a message is deleted
Here is a simple way for publishers to register for both notifications :
Slide left and right to see the full code
By following the above steps , You can publish the basic / The subscription message architecture is integrated into any one ASP.NET perhaps .NET In the core application .
PART 04
summary
thus , To sum up NCache Release / What benefits does the subscription model offer to overcome the limitations of existing solutions .
- Because of its linear scalability ,NCache Release / Subscriptions can handle more and more subscription requests by adding cache servers and performing load balancing frequently . This scaling feature is transparent to the user , And will not interfere with the communication process . therefore , You can use NCache Release / Subscription mode to easily extend communication performance .
- NCache Release / Subscriptions help keep subscriptions going 、 Notification of message delivery failure and delivery failure , To avoid message loss . Besides ,NCache Our distributed and replication architecture ensures NCache High availability , To accommodate subscriber connections in a dynamic environment . All these features ensure reliable communication .
- because NCache Is a distributed cache in memory , So the message store itself that resides in the cache is very fast . Besides ,NCache Allows you to specify whether data items that reside in the cache expire , To intelligently manage storage space .
NCache extensibility 、 Reliability and storage efficiency , And release / Loosely coupled and asynchronous messaging patterns for subscriptions , bring NCache Release / Subscription function in the future .NET/NET The core application has great prospects in the development of distributed messaging .
边栏推荐
- MySQL中符号@的作用
- Julia constructs diagonal matrix
- 带你认识图数据库性能和场景测试利器LDBC SNB
- 阿里云刘珅孜:云游戏带来的启发——端上创新
- Pragma once Usage Summary
- 实现简单的三D立方体自动旋转
- Four characteristics of transactions
- 3.1 simple condition judgment
- New method of cross domain image measurement style relevance: paper interpretation and code practice
- Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end
猜你喜欢
随机推荐
Bit. Store: long bear market, stable stacking products may become the main theme
3.1 simple condition judgment
New method of cross domain image measurement style relevance: paper interpretation and code practice
锚文本大量丢失的问题
The array of C language is a parameter to pass a pointer
Open source 23 things shardingsphere and database mesh have to say
Raspberry pie preliminary use
华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
Taishan Office Technology Lecture: the first difficulty is vertical positioning
Cesium realizes satellite orbit detour
What should the ultimate LAN transmission experience be like
Deeply digitise, lead cloud nativity and serve more developers
C système de gestion de la charge de travail des enseignants en langues
C language teacher workload management system
Construction and management practice of ByteDance buried point data flow
Qt5 signal and slot mechanism (basic introduction to signal and slot)
等保三级密码复杂度是多少?多久更换一次?
当发布/订阅模式遇上.NET
MySQL中符号@的作用








