Event bus
What is a thing
Things Everything that is seen is a thing, and everything that cannot be seen is a thing
For example: team microservices, member microservices, aggregation microservices, gateway apis, certification authority, and more including classes, objects
All events are the result of changes in things
You’ve been exposed to events for the first time in js or c# advanced features. People don’t default to events, but they don’t understand them very well
What is an event
An event is a change in the state of things, and the result of each change of things is called an event
What is an event bus
A mechanism for managing all events is called an event bus
Including event publishing, event storage, event subscription, event processing collectively
Function:
An event bus is a mechanism that allows different components to communicate with each other without understanding each other. Components can send events to Eventbus without knowing who is answering or how many other people are answering. Components can also listen for events on Eventbus without knowing who sent the event. In this way, components can communicate with each other without having to depend on each other. Again, it’s easy to replace a component. As long as the new component knows about the events being sent and received, the other components will never know.
Why use the event bus
Decouple the components of the microservice system
Use the development of the business
Event Bus framework
CAP
masstransit
Internal concept of CAP
Events: Are status information
Publisher: Cap, the role that publishes the event
Subscriber: Cap subscribes to the role of consuming events
Message transporter: Transmits events
Message storage: Stores events
CAP stores the event message queue type Transport
Azure
rabbitmq
kafaka
In Memory Queue
CAP stores the event persistence type
SQL Server
MySQL
PostgreSQL
MongoDB
InMemoryStorage
CAP Event Monitoring
Dashboard
How to use CAP in microservice system
conditions
1. Microservice system
2, the RabbitMQ
3, essentially
4, CAP
steps
1. Preparation of microservice system
The microservice system is all set up
2. Prepare RabbitMQ
2.1 Environment Preparation
Download Erlang from www.erlang.org/downloads
The RabbitMQ download address: www.rabbitmq.com/download.ht…
2.2 the RabbitMQ start
1. Add the visual plug-in in the installation directory
rabbitmq-plugins enable rabbitmq_management
Copy the code
2. Start in the installation directory
rabbitmq-server
Copy the code
3. Check the rabbitMQ status
rabbitmqctl status
Copy the code
4. Enter http://127.0.0.1:15672 in the browser
Access the RabbitMQ background system
3. Prepare SqlServer
SqlServer is started and installed
4. CAP preparation
4.1 CAP environment
Cap.dotnetcore.xyz /user-guide/…
4.2 CPA configuration
1, in RuanMou. MicroService. Add rely on the Core project
CAP Nuget dotnetcore. CAP CAP Transport Nuget dotnetcore.cap. RabbitMQ CAP Persist dotnetcore.cap. SqlServerCopy the code
2, in RuanMou. MicroService. AggregateService service startup. Cs to add
AddCap(x => {// 8.1 Using memory to Store Messages (Handling message sending failures). RabbitMQ(rb => {rb.HostName = "localhost"; // 8.2 Using RabbitMQ for event center processing. rb.UserName = "guest"; rb.Password = "guest"; rb.Port = 5672; rb.VirtualHost = "/"; }); });Copy the code
2.1 Inject ICapPublisher into Aggregatecontroller.cs
private readonly ICapPublisher capPublisher;
public TeamsController(ICapPublisher capPublisher)
{
this.capPublisher = capPublisher;
}
Copy the code
3, in RuanMou. MicroService. VideoService service startup. Cs to add
AddCap(x => {// 8.1 UseRabbitMQ for event center processing. userabbitmq (rb => {rb.HostName = "localhost"; rb.UserName = "guest"; rb.Password = "guest"; rb.Port = 5672; rb.VirtualHost = "/"; }); });Copy the code
3.1 Adding features to methods in VideoController.cs [CapSubscribe]
/ / / < summary > / / / Video add add (asynchronous) / / / < summary > / / / < param name = "Video" > < param > / / / < returns > < / returns > [NonAction] [CapSubscribe("tontcap")] public ActionResult<Video> PostVideo(Video Video) { videoService.Create(Video); return CreatedAtAction("GetVideo", new { id = Video.Id }, Video); }Copy the code
4. Effect display
RabbitMQ is down
steps
1. Disable RabbitMQ
The event message could not be sent and was stored in memory cache
2. When RabbitMQ is started, messages are sent normally
Internal use of timer polling mechanism
AggregateService breakdown
The AggregateService runs services successfully, but breaks down before sending messages
Solution using local message tables (idea: persistent operations)
conditions
1. Local message table
steps
1, in RuanMou. MicroService. AggregateService services
1.1 Create a Context file, and then create AggregateContext in the Context folder
// </summary> // Aggregate service context /// </summary> public class AggregateContext: DbContext { public AggregateContext(DbContextOptions<AggregateContext> options) : base(options) { } }Copy the code
1.2 Add in AppSettings. json
{ "ConnectionStrings": { "DefaultConnection": "Data Source=.; Initial Catalog=aggregateservice; Persist Security Info=True; User ID=sa; Password=tony" } }Copy the code
1.3 Adding message persistence in startup.cs
AddDbContext<AggregateContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")); }); AddCap(x => {// 8.1 Using EntityFramework for storage operations. / / 8.2 using essentially the transaction x.U seSqlServer (Configuration. The GetConnectionString (" DefaultConnection ")); RabbitMQ(rb => {rb.HostName = "localhost"; // 8.2 Using RabbitMQ for event center processing. rb.UserName = "guest"; rb.Password = "guest"; rb.Port = 5672; rb.VirtualHost = "/"; }); });Copy the code
1.4 Test demonstration effect
There are two more tables in the database
When the business is successfully executed and the message is sent, the converged microservice goes down and the message is persisted to the database
When the converged micro service is restarted, the message is successfully sent and consumed
1.5 the principle
1. Timer message retry
2. Idempotence A function has the same result every time and only one state
VideoService breakdown
VideoService failed to accept the message
Failed to receive messages when VideoService directly went down.
Then restart VideoService message consumption successfully
VideoService failed to receive message successfully
conditions
1. Local message table
steps
1, in RuanMou. MicroService. The Core project
1.1 installation is essentially
Nuget DotNetCore.CAP.SqlServer
Copy the code
2, in RuanMou. MicroService. VideoService project
2.1 Adding message persistence in startup.cs
AddCap(x => {// 8.1 Using EntityFramework for storage operations. / / 8.2 using essentially the transaction x.U seSqlServer (Configuration. The GetConnectionString (" DefaultConnection ")); RabbitMQ(rb => {rb.HostName = "localhost"; // 8.2 Using RabbitMQ for event center processing. rb.UserName = "guest"; rb.Password = "guest"; rb.Port = 5672; rb.VirtualHost = "/"; }); });Copy the code
2.2 Effect Display
There are two more tables in the database
2.3 the principle
1. Timer message retry
2. Idempotence A function has the same result every time and only one state
Message retry or consumption failure
Using human intervention
conditions
1. Dashboard – Background management page
steps
1, in RuanMou. MicroService. The Core project
1.1 installation Dashboard
Nuget DotNetCore.CAP.Dashboard
Copy the code
2, in RuanMou. MicroService. VideoService project
2.1 Adding a Dashboard to startup.cs
AddCap(x => {// 8.1 Using EntityFramework for storage operations. / / 8.2 using essentially the transaction x.U seSqlServer (Configuration. The GetConnectionString (" DefaultConnection ")); // 8.3 Using RabbitMQ for event center processing. User AbbitMQ (rb => {rb.HostName = "localhost"; rb.UserName = "guest"; rb.Password = "guest"; rb.Port = 5672; rb.VirtualHost = "/"; }); // 8.4 Adding the background Monitoring page of CAP x. user Dashboard(); });Copy the code
2.2 Open the CAP background monitoring page
http://localhost:5007/cap
The message that fails to be sent is repeated
Re-consume messages that fail to consume