Commit 73ced6fb authored by 黄奎's avatar 黄奎

新增CAP

parent 84fdcc16
......@@ -161,7 +161,7 @@ namespace Mall.Common
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private static string ReadConfigKey(string key)
public static string ReadConfigKey(string key)
{
try
{
......
......@@ -8,7 +8,6 @@
<PackageReference Include="Aliyun.Net.SDK.Core" Version="1.0.3" />
<PackageReference Include="Aliyun.OSS.SDK.NetCore" Version="2.10.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="RabbitMQ.Client" Version="6.0.0" />
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.10" />
</ItemGroup>
......
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Mall.ThirdCore.Commom;
using System;
using System.Threading;
using Mall.ThirdCore.Message;
namespace Mall.ThirdCore.Mq
{
/// <summary>
/// 消息队列配置文件
/// </summary>
public class RabbitConfig
{
/// <summary>
/// 主机名:ip地址
/// </summary>
public string HostName { get; set; }
/// <summary>
/// 端口
/// </summary>
public int Port { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 队列名称
/// </summary>
public string QueenName { get; set; }
}
}
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mall.ThirdCore.Mq
{
/// <summary>
/// 消息队列帮助类
/// </summary>
public class RabbitMQService
{
/// <summary>
/// 获取连接
/// </summary>
/// <param name="rabbitConfig">连接配置实体</param>
/// <returns></returns>
public static ConnectionFactory GetConnectionFactory(RabbitConfig rabbitConfig)
{
ConnectionFactory factory = new ConnectionFactory
{
HostName = rabbitConfig.HostName,
//默认端口
Port = rabbitConfig.Port,
UserName = rabbitConfig.UserName,
Password = rabbitConfig.Password
};
return factory;
}
/// <summary>
/// 发送信息
/// </summary>
/// <param name="rabbitConfig"></param>
/// <param name="message"></param>
public static void SendMessage(RabbitConfig rabbitConfig, string message)
{
using (IConnection conn = GetConnectionFactory(rabbitConfig).CreateConnection())
{
using (IModel channel = conn.CreateModel())
{
//在MQ上定义一个持久化队列,如果名称相同不会重复创建
channel.QueueDeclare(rabbitConfig.QueenName, true, false, false, null);
byte[] buffer = Encoding.UTF8.GetBytes(message);
IBasicProperties properties = channel.CreateBasicProperties();
properties.DeliveryMode = 2;
channel.BasicPublish("", rabbitConfig.QueenName, properties, buffer);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mall.Common.API;
using Mall.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Mall.WebApi.Controllers.CAP
{
[Route("api/[controller]/[action]")]
[ApiExceptionFilter]
[ApiController]
[EnableCors("AllowCors")]
public class TestController : BaseController
{
private readonly DotNetCore.CAP.ICapPublisher _capBus;
public TestController(DotNetCore.CAP.ICapPublisher capPublisher)
{
this._capBus = capPublisher;
}
[HttpPost]
[AllowAnonymous]
public ApiResult Get()
{
ApiResult apiResult = new ApiResult()
{
message = "操作成功",
resultCode = 1,
data = DateTime.Now
};
_capBus.Publish("show.time", Common.Plugin.JsonHelper.Serialize(apiResult));
return apiResult;
}
[NonAction]
[AllowAnonymous]
[DotNetCore.CAP.CapSubscribe("show.time")]
public void CheckReceiveMessage(string result)
{
string str = "";
Console.WriteLine(result);
}
}
}
\ No newline at end of file
......@@ -244,7 +244,7 @@ namespace Mall.WebApi.Controllers.Finance
}
else
{
string msg = userModule.SetRecommendOrdersBillRemit(BillId, TenantId, MallBaseId, 0,, "单据审核通过,已自动打款");
string msg = userModule.SetRecommendOrdersBillRemit(BillId, TenantId, MallBaseId, 0, "单据审核通过,已自动打款");
if (msg == "")
{
return ApiResult.Success();
......
......@@ -9,6 +9,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="DotNetCore.CAP" Version="3.0.0" />
<PackageReference Include="DotNetCore.CAP.MySql" Version="3.0.0" />
<PackageReference Include="DotNetCore.CAP.RabbitMQ" Version="3.0.0" />
<PackageReference Include="JWT" Version="5.3.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="System.IO.FileSystem" Version="4.3.0" />
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Controller_SelectedScaffolderID>MvcControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
<WebStackScaffolding_ControllerDialogWidth>600</WebStackScaffolding_ControllerDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
......
......@@ -5,6 +5,7 @@ using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;
using DotNetCore.CAP.Messages;
using Mall.ThirdCore.Message;
using Mall.WebApi.Filter;
using Microsoft.AspNetCore.Builder;
......@@ -56,11 +57,34 @@ namespace Mall.WebApi
"http://yx.oytour.com",
"http://mall.oytour.com",
"http://testmall.oytour.com",
"http://yx.oytour.com",
};
services.AddCors(options => options.AddPolicy("AllowCors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().WithOrigins(corsArray.ToArray())));
services.AddCap(x =>
{
x.UseMySql(config =>
{
config.ConnectionString = "server=192.168.2.214;user id=reborn;password=Reborn@2018;database=cap;CharSet=utf8; Convert Zero Datetime=true;";
});
x.UseRabbitMQ(cfg =>
{
cfg.HostName = "47.96.25.130";
cfg.VirtualHost = "/";
cfg.Port = Convert.ToInt32(5672);
cfg.UserName = "guest";
cfg.Password = "viitto2019";
});
//失败后的重试次数,默认50次;在FailedRetryInterval默认60秒的情况下,即默认重试50*60秒(50分钟)之后放弃失败重试
x.FailedRetryCount = 10;
//失败后的重拾间隔,默认60秒
x.FailedRetryInterval = 10;
//设置成功信息的删除时间默认24*3600秒
x.SucceedMessageExpiredAfter = 3600;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
......@@ -78,12 +102,12 @@ namespace Mall.WebApi
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
System.WebHttpContext.HttpContext.Configure(app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Http.IHttpContextAccessor>());
//启动信息发送
Task.Run(() => MessageCore.Init());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment