Distributed event bus implemented using sockets, supports CQRS, does not rely on third-party MQ.
is a lightweight, socket-based distributed event bus system designed to simplify event communication in distributed architectures. It allows processes to communicate with each other through a publish/subscribe model without relying on external message queuing services.
Command
Query
characterization
-
light-weight class (in athletics): Does not rely on any external MQ services, reducing system complexity and dependencies.
-
high performance: Direct socket-based communication that provides low-latency, high-throughput messaging.
-
dexterity: Support for custom event types and message handlers for easy integration into existing systems.
-
scalability: Supports multi-client connections for distributed system environments.
communications protocol
pass (a bill or inspection etc) TCP
protocol for data interaction, the protocol packet structure is as follows:
mounting
pass (a bill or inspection etc)NuGet
Package Manager Installation:
Install-Package
Server-side use
Running the Event Service
In the server-side code, create and start theEventServer
instance to listen for client connections and events:
using ;
// Create the event server instance
IEventServer eventServer = new EventServer();
// Start the event server, listening on the specified IP and port.
("127.0.0.1", 9100); // Start the event server, listening on the specified IP and port.
Stop Event Service
When the event service is no longer needed, call theStop
method to gracefully shut down the server:
();
Client Usage
Connection Event Service
In the client code, create theEventClient
instance and connect to the event server:
using ;
// Create the event client instance
IEventClient eventClient = new EventClient();
// Connect to the event server, checking the connection status using
("127.0.0.1", 9100)));
Subscribe to events
Subscribe to a specific type of event and specify the event handler function:
<NewEmailCommand>("", ReceiveNewEmailCommand).
private void ReceiveNewEmail(NewEmailCommand command)
{
// Handle new email notifications
($"Received new email with subject {}");
}
Issuing Commands
Publishes events to a specified topic for subscribed clients to process:
// Post a new email notification event
("", new NewEmailCommand { Subject = "Congratulations on winning the Github First Prize", Content = "We're so happy you're here in July 2024..." , SendTime = new DateTime(2024, 7, 27) });
Query
Querying a specified topic requires a receiving query end to subscribe to the same topic (i.e., a producer), receive the request, and then publish the query results with the same topic:
<EmailQuery>("", ReceiveEmailQuery);
private void ReceiveEmailQuery(EmailQuery query)
{
// Execution of query requests,Prepare search results
var response = new EmailQueryResponse { Emails = () };
// With the same theme,Publishing search results
if (_eventClient!.Publish("", response,
out var errorMessage))
{
($"Response query result: {response}");
}
else
{
($"Response query failed: {errorMessage}");
}
}
The other end can use the same subject query (i.e. consumer):
var response = _eventClient!.Query<EmailQuery, EmailQueryResponse>("",
new EmailQuery() { Subject = "Account" },
out var errorMessage);
if ((errorMessage) && response != null)
{
($"Query , result: {response}");
}
else
{
(
$"Query failed: [{errorMessage}]");
}
Unsubscribe event
You can unsubscribe when you no longer need to receive certain types of events:
<NewEmailNotification>("", ReceiveNewEmail);
Disconnect Event Service
To complete event processing or if you need to disconnect from the server, call theDisconnect
Methods:
();
("Disconnecting from the event service").
caveat
- Make sure that the server and client are using the same address and port number, and that the port is not occupied by another service.
- In a production environment, the server should be configured to listen to a public IP address or an appropriate network interface.
- Considering situations such as network anomalies and service restarts, the client may need to implement reconnection logic.
- Expandable according to actual needs
EventServer
cap (a poem)EventClient
class to support more complex features such as message encryption, authentication authorization, etc.