If you don't want to use Microsoft's IoC container, you can view ASP.NET Core documentation [here](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#default-service-container-replacement) to learn how to replace the default container implementation.
## 什么是最低配置?
## What is the minimum configuration?
最简单的回答就是,至少你要配置一个消息队列和一个事件存储,如果你想快速开始你可以使用下面的配置:
The simplest answer is that at least you have to configure a transport and a storage. If you want to get started quickly you can use the following configuration:
For specific transport and storage configuration, you can view the configuration items provided by the specific components in the [Transports](../transports/general.md) section and the [Persistent](../persistent/general.md) section.
The default consumer group name, corresponding to different names in different Transports, you can customize this value to customize the names in Transports for easy viewing.
This is a new configuration item introduced in the CAP v2.4 version. It is used to specify a version of a message to isolate messages of different versions of the service. It is often used in A/B testing or multi-service version scenarios. The following is its application scenario:
Due to the rapid iteration of services, the data structure of the message is not fixed during each service integration process. Sometimes we add or modify certain data structures to accommodate the newly introduced requirements. If you're a brand new system, there's no problem, but if your system is deployed to a production environment and serves customers, this will cause new features to be incompatible with the old data structure when they go online, and then these changes can cause serious problems. To work around this issue, you can only clean up message queues and persistent messages before starting the application, which is obviously fatal for production environments.
Sometimes, the server's server needs to provide multiple sets of interfaces to support different versions of the app. The data structures of the same interface and server interaction of these different versions of the app may be different, so usually the server does not provide the same. Routing addresses to adapt to different versions of App calls.
!!! info "Using the same persistent table/collection in different instance"
If you want multiple different instance services to use the same database, in versions prior to 2.4, we could isolate database tables for different instances by specifying different table names. That is to say, when configuring the CAP, it is implemented by configuring different table name prefixes.
> 查看博客来了解更多关于 Version 的信息: https://www.cnblogs.com/savorboard/p/cap-2-4.html
> Check out the blog to learn more about Version feature: https://www.cnblogs.com/savorboard/p/cap-2-4.html
#### FailedRetryInterval
默认值:60 秒
> Default: 60 sec
在消息发送的时候,如果发送失败,CAP将会对消息进行重试,此配置项用来配置每次重试的间隔时间。
In the process of message message sent to transport failed, the CAP will be retry to sent. This configuration item is used to configure the interval between each retry.
In the process of message consumption failed, the CAP will retry to execute. This configuration item is used to configure the interval between each retry.
By default, retry will start after **4 minutes** of failure to send or consume, in order to avoid possible problems caused by setting message state delays.
Failures in the process of sending and consuming messages will be retried 3 times immediately, and will be retried polling after 3 times, at which point the FailedRetryInterval configuration will take effect.
#### FailedRetryCount
默认值:50
> Default: 50
重试的最大次数。当达到此设置值时,将不会再继续重试,通过改变此参数来设置重试的最大次数。
Maximum number of retries. When this value is reached, retry will stop and the maximum number of retries will be modified by setting this parameter.
Failure threshold callback. This action is called when the retry reaches the value set by `FailedRetryCount`, and you can receive the notification by specifying this parameter to make a manual intervention. For example, send an email or notify.
The expiration time (in seconds) of the success message. When the message is sent or consumed successfully, it will be removed from persistent when the time reaches `SucceedMessageExpiredAfter` seconds. You can set the expiration time by specifying this value.
The data sent by using the `ICapPublisher` interface is called `Message`.
## 消息调度
## Scheduling
CAP 接收到消息之后会将消息发送到 Transport, 由 Transport 进行运输。
After the CAP receives the message, it sends the message to Transport, which is transported by transport.
When you send using the `ICapPublisher` interface, the CAP will dispatch the message to the corresponding Transport. Currently, bulk messaging is not supported.
The CAP will storage after receiving the message. For more information on storage, see the [Persistent](../persistent/general.md) section.
CAP 接收到消息之后会将消息进行 Persistent(持久化), 有关 Persistent 的更多信息,可以查看 [Persistent](../persistent/general.md) 章节。
## Retry
## 消息重试
Retrying plays an important role in the overall CAP architecture design, and CAPs retry for messages that fail to send or fail to execute. There are several retry strategies used throughout the CAP design process.
重试在整个CAP架构设计中具有重要作用,CAP 中会针对发送失败或者执行失败的消息进行重试。在整个 CAP 的设计过程中有以下几处采用的重试策略。
### Send retry
1、 发送重试
During the message sending process, when the broker crashes or the connection fails or an abnormality occurs, the CAP will retry the sending. Retry 3 times for the first time, retry every minute after 4 minutes, and +1 retries. When the total number of times reaches 50, the CAP will stop retrying.
在消息发送过程中,当出现 Broker 宕机或者连接失败的情况亦或者出现异常的情况下,这个时候 CAP 会对发送的重试,第一次重试次数为 3,4分钟后以后每分钟重试一次,进行次数 +1,当总次数达到50次后,CAP将不对其进行重试。
You can adjust the total number of default retries by setting `FailedRetryCount` in CapOptions.
你可以在 CapOptions 中设置FailedRetryCount来调整默认重试的总次数。
It will stop when the maximum number of times is reached. You can see the reason for the failure in Dashboard and choose whether to manually retry.
The consumer method is executed when the Consumer receives the message and will retry when an exception occurs. This retry strategy is the same as the send retry.
There is an `ExpiresAt` field in the database message table indicating the expiration time of the message. When the message is sent successfully, the status will be changed to `Successed`, and `ExpiresAt` will be set to **1 hour** later.
Consuming failure will change the message status to `Failed` and `ExpiresAt` will be set to **15 days** later.
CAP 默认情况下会每隔一个小时将消息表的数据进行清理删除,避免数据量过多导致性能的降低。清理规则为 ExpiresAt 不为空并且小于当前时间的数据。 也就是说状态为Failed的消息(正常情况他们已经被重试了 50 次),如果你15天没有人工介入处理,同样会被清理掉。
\ No newline at end of file
By default, the data of the message table is deleted **every hour** to avoid performance degradation caused by too much data. The cleanup strategy is `ExpiresAt` is not empty and is less than the current time.
That is to say, the message with the status Failed (normally they have been retried 50 times), if you do not have manual intervention for 15 days, it will **also be** cleaned up.
CAP 目前还不支持消息本身的序列化,在将消息发送到消息队列之前 CAP 使用 json 对消息对象进行序列化。
CAP does not currently support serialization for transport messages, and CAP uses json to serialize message objects before sending them to the transport.
## 内容序列化
## Content Serialization
CAP 支持对消息的 Content 字段进行序列化,你可以自定义 `IContentSerializer` 接口来做到这一点。
The CAP supports serializing the Message's Content field, which you can do by customizing the `IContentSerializer` interface.
目前由于消息对象需要进行数据库存储,所以只支持 string 的序列化和反序例化。
Currently, since the message object needs to be stored in the database, only the serialization and reverse ordering of `string` are supported.
```csharp
...
...
@@ -26,13 +26,21 @@ class MyContentSerializer : IContentSerializer
}
```
## 消息适配器
Configure the custom `MyContentSerializer` to the service.
在异构系统中,有时候需要和其他系统进行通讯,但是其他系统使用的消息对象可能和 CAP 的[**包装器对象**](../persistent/general.md#_7)不一样,这个时候就需要对消息进行自定义适配。
In heterogeneous systems, sometimes you need to communicate with other systems, but other systems use message objects that may be different from CAP's [**Wrapper Object**](../persistent/general.md#_7). This time maybe you need to customize the message wapper.
CAP 提供了 `IMessagePacker` 接口用于对 [**包装器对象**](../persistent/general.md#_7) 进行自定义,自定义的 MessagePacker 通常是将 `CapMessage` 进行打包和解包操作,在这个过程中可以添加自己的业务对象。
The CAP provides the `IMessagePacker` interface for customizing the [**Wrapper Object**](../persistent/general.md#_7). The custom MessagePacker usually packs and unpacks the `CapMessage` In this process you can add your own business objects.
使用方法:
Usage :
```csharp
...
...
@@ -72,7 +80,7 @@ class MyMessagePacker : IMessagePacker
}
```
接下来,配置自定义的 `MyMessagePacker` 到服务中。
Next, configure the custom `MyMessagePacker` to the service.
CAP is an EventBus and a solution for solving distributed transaction problems in microservices or SOA systems. It helps create a microservices system that is scalable, reliable, and easy to change.
In Microsoft's [eShopOnContainer](https://github.com/dotnet-architecture/eShopOnContainers) microservices sample project, it is recommended to use CAP as the EventBus available in the production environment.
In Microsoft's [eShopOnContainer](https://github.com/dotnet-architecture/eShopOnContainers) microservices sample project, it is recommended to use CAP as the EventBus available in the production environment.
Persistent storage of memory messages is often used in development and test environments, and if you use memory-based storage you lose the reliability of local transaction messages.
## 配置
## Configuration
如果要使用内存存储,你需要从 NuGet 安装以下扩展包:
To use in-memory storage, you need to install the following extensions from NuGet:
MongoDB supports ACID transactions since version 4.0, so CAP only supports MongoDB above 4.0, and MongoDB needs to be deployed as a cluster, because MongoDB's ACID transaction requires a cluster to be used.
## 配置
For a quick development of the MongoDB 4.0+ cluster for the development environment, you can refer to [this article](https://www.cnblogs.com/savorboard/p/mongodb-4-cluster-install.html).
要使用 MongoDB 存储,你需要从 NuGet 安装以下扩展包:
## Configuration
```shell
To use MongoDB storage, you need to install the following extensions from NuGet: