GitHub 您所在的位置:网站首页 cap框架怎样调用destination GitHub

GitHub

2024-07-11 07:15| 来源: 网络整理| 查看: 265

CAP                     中文

Docs&Dashboard AppVeyor NuGet NuGet Preview Member project of .NET Core Community GitHub license

CAP is a library based on .Net standard, which is a solution to deal with distributed transactions, has the function of EventBus, it is lightweight, easy to use, and efficient.

In the process of building an SOA or MicroService system, we usually need to use the event to integrate each service. In the process, simple use of message queue does not guarantee reliability. CAP adopts local message table program integrated with the current database to solve exceptions that may occur in the process of the distributed system calling each other. It can ensure that the event messages are not lost in any case.

You can also use CAP as an EventBus. CAP provides a simpler way to implement event publishing and subscriptions. You do not need to inherit or implement any interface during subscription and sending process.

Architecture overview

cap.png

CAP implements the Outbox Pattern described in the eShop ebook.

Getting Started NuGet

CAP can be installed in your project with the following command.

PM> Install-Package DotNetCore.CAP

CAP supports most popular message queue as transport, following packages are available to install:

PM> Install-Package DotNetCore.CAP.Kafka PM> Install-Package DotNetCore.CAP.RabbitMQ PM> Install-Package DotNetCore.CAP.AzureServiceBus PM> Install-Package DotNetCore.CAP.AmazonSQS PM> Install-Package DotNetCore.CAP.NATS PM> Install-Package DotNetCore.CAP.RedisStreams PM> Install-Package DotNetCore.CAP.Pulsar

CAP supports most popular database as event storage, following packages are available to install:

// select a database provider you are using, event log table will integrate into. PM> Install-Package DotNetCore.CAP.SqlServer PM> Install-Package DotNetCore.CAP.MySql PM> Install-Package DotNetCore.CAP.PostgreSql PM> Install-Package DotNetCore.CAP.MongoDB //need MongoDB 4.0+ cluster Configuration

First, you need to configure CAP in your Startup.cs:

public void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddCap(x=> { //... }); } Async subscription

You are able to implement async subscription. Subscription's method should return Task and receive CancellationToken as parameter.

public class AsyncSubscriber : ICapSubscribe { [CapSubscribe("name")] public async Task ProcessAsync(Message message, CancellationToken cancellationToken) { await SomeOperationAsync(message, cancellationToken); } } Use partials for topic subscriptions

To group topic subscriptions on class level you're able to define a subscription on a method as a partial. Subscriptions on the message queue will then be a combination of the topic defined on the class and the topic defined on the method. In the following example the Create(..) function will be invoked when receiving a message on customers.create

[CapSubscribe("customers")] public class CustomersSubscriberService : ICapSubscribe { [CapSubscribe("create", isPartial: true)] public void Create(Customer customer) { } } Subscribe Group

The concept of a subscription group is similar to that of a consumer group in Kafka. it is the same as the broadcast mode in the message queue, which is used to process the same message between multiple different microservice instances.

When CAP startups, it will use the current assembly name as the default group name, if multiple same group subscribers subscribe to the same topic name, there is only one subscriber that can receive the message. Conversely, if subscribers are in different groups, they will all receive messages.

In the same application, you can specify Group property to keep subscriptions in different subscribe groups:

[CapSubscribe("xxx.services.show.time", Group = "group1" )] public void ShowTime1(DateTime datetime) { } [CapSubscribe("xxx.services.show.time", Group = "group2")] public void ShowTime2(DateTime datetime) { }

ShowTime1 and ShowTime2 will be called one after another because all received messages are processed linear. You can change that behaviour to set UseDispatchingPerGroup true.

BTW, You can specify the default group name in the configuration:

services.AddCap(x => { x.DefaultGroup = "default-group-name"; }); Dashboard

CAP also provides dashboard pages, you can easily view messages that were sent and received. In addition, you can also view the message status in real time in the dashboard. Use the following command to install the Dashboard in your project.

PM> Install-Package DotNetCore.CAP.Dashboard

In the distributed environment, the dashboard built-in integrates Consul as a node discovery, while the realization of the gateway agent function, you can also easily view the node or other node data, It's like you are visiting local resources.

View Consul config docs

If your service is deployed in Kubernetes, please use our Kubernetes discovery package.

PM> Install-Package DotNetCore.CAP.Dashboard.K8s

View Kubernetes config docs

The dashboard default address is: http://localhost:xxx/cap , you can configure relative path /cap with x.UseDashboard(opt =>{ opt.MatchPath="/mycap"; }).

Contribute

One of the easiest ways to contribute is to participate in discussions and discuss issues. You can also contribute by submitting pull requests with code changes.

License

MIT



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有