目录
1.准备工作
2.创建第一个项目
3.创建第二个项目
4.运行
1.1 rabbitmq
username:admin
password:admin
port:5672
1.2 mssql
2.1 创建webapi项目,起名为Cap.Step1.Service如图:
2.2 选择项目位置
2.3 选择框架,由于是windows上,所以不要docker
2.4 添加引用,如图
DotNetCore.CAP 6.1.0
DotNetCore.CAP.Dashboard 3.1.2
DotNetCore.CAP.RabbitMQ 6.1.0
DotNetCore.CAP.SqlServer 5.2.0
2.5 添加Cap的配置文件,MsSqlConnection用于存储业务数据的MsSql数据库,配置如下:
{
"RabbitMQOptions": {
"HostName": "192.168.2.8",
"UserName": "admin",
"Password": "admin",
"Port": 5672
},
"MsSqlConnection": "Server=192.168.2.8;userid=sa;password=china_2020;Database=PtE"
}
2.6 添加服务端口号配置文件,配置如下:
{
"urls": "http://*:5001"
}
2.7 最后配置文件位置如图:
2.8 在Startup中添加 cap的相关配置
services.Configure(Configuration.GetSection("RabbitMQOptions"));
string connection = this.Configuration.GetConnectionString("MsSqlConnection");
services.AddCap(x =>
{
//存储消息数据
x.UseSqlServer(configure => {
configure.ConnectionString = connection;
configure.UseSqlServer2008();
});
//连接RabbitMQ
RabbitMQOptions rabbitMQ = this.Configuration.GetSection("RabbitMQOptions").Get();
x.UseRabbitMQ((options) =>
{
options = rabbitMQ;
});
x.FailedRetryCount = 10; //失败后重试次数
x.FailedRetryInterval = 60; //重试间隔
x.FailedThresholdCallback = failed =>
{
var logger = failed.ServiceProvider.GetService>();
logger.LogError($"MessageType{failed.MessageType} 失败,重试{x.FailedRetryCount}" +
$"消息名称:{failed.Message.GetName()}");
};
});
2.9 添加webapi 名为:Step1Controller
[Route("[controller]")]
[ApiController]
public class Step1Controller : ControllerBase
{
private readonly ICapPublisher capPublisher;
private readonly IConfiguration configuration;
private readonly ILogger logger;
private string PublishName = "SHKF.Cap.Demo"; //下一步的消息队列Key
public Step1Controller(ICapPublisher capPublisher,
IConfiguration configuration,
ILogger logger)
{
this.capPublisher = capPublisher;
this.configuration = configuration;
this.logger = logger;
}
[HttpGet()]
public IActionResult Get()
{
//业务数据
Dictionary dicHeader = new Dictionary();
dicHeader.Add("Teacher", "WangPeng");
dicHeader.Add("Student", "Seven");
dicHeader.Add("Version", "1.2");
using (var connection = new SqlConnection(this.configuration.GetConnectionString("MsSqlConnection")))
{
using (var tran = connection.BeginTransaction(this.capPublisher, false))
{
try
{
this.capPublisher.Publish(this.PublishName, dicHeader);
}
catch (Exception ex)
{
this.logger.LogWarning(ex.Message);
tran.Rollback();
}
tran.Commit();
}
}
this.logger.LogWarning($"This is AdoTransaction Invoke");
return Ok();
}
}
2.10 加载配置文件
public static void Main(string[] args)
{
CreateHostBuilder(args)
.ConfigureHostConfiguration((configBuilder) => {
configBuilder.AddJsonFile("json/host.json", true);
configBuilder.AddJsonFile("json/cap.json", true);
})
.Build().Run();
}
3.11 修改launchSettings文件,删除iis相关内容,同时启动不访问默认页
{
"profiles": {
"Cap.Step1.Service": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
3.1 创建webapi项目
项目引用, 配置文件, launchSettings文件和第一个项目相同,项目结构如下:
3.2 由于不需要端口,所以把容器改成普通容器:如下图:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args)
.ConfigureServices(ConfigureServices)
.ConfigureHostConfiguration((configBuilder) => {
configBuilder.AddJsonFile("json/host.json", true);
configBuilder.AddJsonFile("json/cap.json", true);
})
.Build().Run();
.Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args);
private static void ConfigureServices(HostBuilderContext hostBuilder, IServiceCollection services)
{
IConfiguration Configuration = hostBuilder.Configuration;
services.Configure(Configuration.GetSection("RabbitMQOptions"));
services.AddControllers();
string connection = Configuration.GetConnectionString("MsSqlConnection");
services.AddCap(x =>
{
// If you are using ADO.NET, choose to add configuration you needed:
x.UseSqlServer(configure => {
configure.ConnectionString = connection;
configure.UseSqlServer2008();
});
// CAP support RabbitMQ,Kafka,AzureService as the MQ, choose to add configuration you needed:
RabbitMQOptions rabbitMQ = Configuration.GetSection("RabbitMQOptions").Get();
x.UseRabbitMQ((options) =>
{
options = rabbitMQ;
});
x.FailedRetryCount = 10;
x.FailedRetryInterval = 60;
x.FailedThresholdCallback = failed =>
{
var logger = failed.ServiceProvider.GetService>();
logger.LogError($"MessageType{failed.MessageType} 失败,重试{x.FailedRetryCount}" +
$"消息名称:{failed.Message.GetName()}");
};
});
}
}
3.4 添加Step2Controller
4.1 运行Step1项目,Step2项目
4.2 用postman发送请求
http://localhost:5001/step1
4.3 此时rabbmitmq中会多出交换机,cap.default.router
4.5 同时数据库中会增加两个表:Published,Received,表里会存储每一步的发送数据,和第二步的接收数据,这两个表是自动创建的