add Synapse System
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SimApi.Attributes;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class SynapseEventAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public SynapseEventAttribute(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace SimApi.Attributes;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class SynapseRpcAttribute : Attribute
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public SynapseRpcAttribute()
|
||||||
|
{
|
||||||
|
Name = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SynapseRpcAttribute(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,6 +52,12 @@ namespace SimApi.Configs
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public bool EnableLogger { get; set; } = false;
|
public bool EnableLogger { get; set; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用Synapse
|
||||||
|
/// </summary>
|
||||||
|
public bool EnableSynapse { get; set; } = false;
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Swagger文档相关配置,需要启用 EnableSimApiDoc
|
/// Swagger文档相关配置,需要启用 EnableSimApiDoc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -62,6 +68,17 @@ namespace SimApi.Configs
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
||||||
|
|
||||||
|
public SimApiSynapseOptions SimApiSynapseOptions { get; set; } = new SimApiSynapseOptions();
|
||||||
|
|
||||||
|
public void ConfigureSimApiSynapse(Action<SimApiSynapseOptions> options = null)
|
||||||
|
{
|
||||||
|
options?.Invoke(SimApiSynapseOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ConfigureSimApiSynapse(SimApiSynapseOptions options)
|
||||||
|
{
|
||||||
|
SimApiSynapseOptions = options;
|
||||||
|
}
|
||||||
|
|
||||||
public void ConfigureSimApiDoc(Action<SimApiDocOptions> options = null)
|
public void ConfigureSimApiDoc(Action<SimApiDocOptions> options = null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
namespace SimApi.Configs;
|
||||||
|
|
||||||
|
public class SimApiSynapseOptions
|
||||||
|
{
|
||||||
|
public string MqHost { get; set; }
|
||||||
|
|
||||||
|
public int MqPort { get; set; }
|
||||||
|
|
||||||
|
public string MqUser { get; set; }
|
||||||
|
|
||||||
|
public string MqPass { get; set; }
|
||||||
|
|
||||||
|
public string MqVHost { get; set; } = "/";
|
||||||
|
|
||||||
|
public string SysName { get; set; }
|
||||||
|
|
||||||
|
public string AppName { get; set; }
|
||||||
|
|
||||||
|
public string AppId { get; set; }
|
||||||
|
|
||||||
|
public int RpcTimeout { get; set; } = 3;
|
||||||
|
|
||||||
|
public ushort EventProcessorNum { get; set; } = 20;
|
||||||
|
|
||||||
|
public ushort RpcProcessorNum { get; set; } = 20;
|
||||||
|
|
||||||
|
public bool DisableEventClient { get; set; } = false;
|
||||||
|
|
||||||
|
public bool DisableRpcClient { get; set; } = false;
|
||||||
|
}
|
||||||
+1
-2
@@ -22,12 +22,11 @@
|
|||||||
<Folder Include="Communications\" />
|
<Folder Include="Communications\" />
|
||||||
<Folder Include="Controllers\" />
|
<Folder Include="Controllers\" />
|
||||||
<Folder Include="Middlewares\" />
|
<Folder Include="Middlewares\" />
|
||||||
<Folder Include="Attributes\" />
|
|
||||||
<Folder Include="Exceptions\" />
|
<Folder Include="Exceptions\" />
|
||||||
<Folder Include="Configurations\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Minio" Version="4.0.7" />
|
<PackageReference Include="Minio" Version="4.0.7" />
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="6.6.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
+13
-8
@@ -42,6 +42,11 @@ namespace SimApi
|
|||||||
policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); }));
|
policy => { policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (simApiOptions.EnableSynapse)
|
||||||
|
{
|
||||||
|
builder.AddSingleton<Synapse>();
|
||||||
|
}
|
||||||
|
|
||||||
// 使用SimApiDoc
|
// 使用SimApiDoc
|
||||||
if (simApiOptions.EnableSimApiDoc)
|
if (simApiOptions.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
@@ -77,10 +82,7 @@ namespace SimApi
|
|||||||
Id = "HeaderToken"
|
Id = "HeaderToken"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
new[]
|
new[] { "readAccess", "writeAccess" }
|
||||||
{
|
|
||||||
"readAccess", "writeAccess"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
haveSimApiAuth = true;
|
haveSimApiAuth = true;
|
||||||
@@ -143,10 +145,7 @@ namespace SimApi
|
|||||||
Id = "oauth2"
|
Id = "oauth2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
new[]
|
new[] { "SimApiAuth" }
|
||||||
{
|
|
||||||
"SimApiAuth"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -254,6 +253,12 @@ namespace SimApi
|
|||||||
logger.LogInformation("开始配置使用URL小写...");
|
logger.LogInformation("开始配置使用URL小写...");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options.EnableSynapse)
|
||||||
|
{
|
||||||
|
var synapse = builder.ApplicationServices.GetRequiredService<Synapse>();
|
||||||
|
synapse.Init();
|
||||||
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Unicode;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace SimApi;
|
||||||
|
|
||||||
|
public partial class Synapse
|
||||||
|
{
|
||||||
|
private void RunEventClient()
|
||||||
|
{
|
||||||
|
EventClientChannel = CreateChannel(0, "EventClient");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FireEvent(string eventName, object param)
|
||||||
|
{
|
||||||
|
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
});
|
||||||
|
var router = $"event.{Options.AppName}.{eventName}";
|
||||||
|
var props = EventClientChannel.CreateBasicProperties();
|
||||||
|
props.AppId = Options.AppId;
|
||||||
|
props.MessageId = Guid.NewGuid().ToString();
|
||||||
|
props.ReplyTo = Options.AppName;
|
||||||
|
props.Type = eventName;
|
||||||
|
EventClientChannel.BasicPublish(Options.SysName, router, false, props, Encoding.UTF8.GetBytes(paramJson));
|
||||||
|
Logger.LogDebug("Event Publish: {OptionsAppName}.{EventName}\n{ParamJson}", Options.AppName, eventName,
|
||||||
|
paramJson);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using RabbitMQ.Client.Events;
|
||||||
|
|
||||||
|
namespace SimApi;
|
||||||
|
|
||||||
|
public partial class Synapse
|
||||||
|
{
|
||||||
|
private void RunEventServer()
|
||||||
|
{
|
||||||
|
EventServerChannel = CreateChannel(Options.EventProcessorNum, "EventServer");
|
||||||
|
var queue = $"{Options.SysName}_{Options.AppName}_event";
|
||||||
|
EventServerChannel.QueueDeclare(queue, true, false, true, null);
|
||||||
|
foreach (var ev in EventRegistry.Where(ev => !ev.Key.Contains('*') && !ev.Key.Contains('#')))
|
||||||
|
{
|
||||||
|
EventServerChannel.QueueBind(queue, Options.SysName, $"event.{ev.Key}", null);
|
||||||
|
}
|
||||||
|
var consumer = new EventingBasicConsumer(EventServerChannel);
|
||||||
|
consumer.Received += (ch, ea) =>
|
||||||
|
{
|
||||||
|
var reqBody = Encoding.UTF8.GetString(ea.Body.ToArray());
|
||||||
|
Logger.LogDebug("Event Receive: {BasicPropertiesReplyTo}.{BasicPropertiesType}\n{S}",
|
||||||
|
ea.BasicProperties.ReplyTo, ea.BasicProperties.Type, reqBody);
|
||||||
|
|
||||||
|
var key = ea.RoutingKey.Replace("event.", string.Empty);
|
||||||
|
var method = EventRegistry.FirstOrDefault(x => x.Key == key);
|
||||||
|
if (method != null)
|
||||||
|
{
|
||||||
|
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
|
||||||
|
var mt = callClass.GetType().GetMethod(method.Method);
|
||||||
|
if (mt != null)
|
||||||
|
{
|
||||||
|
var pt = mt.GetParameters()[0].ParameterType;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
mt.Invoke(callClass, pt == typeof(string)
|
||||||
|
? new object[] { reqBody }
|
||||||
|
: new[] { JsonSerializer.Deserialize(reqBody, pt) });
|
||||||
|
EventServerChannel.BasicAck(ea.DeliveryTag, false);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
EventServerChannel.BasicNack(ea.DeliveryTag, false, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Logger.LogError("Event Callback not available: {Ev}", method.Key);
|
||||||
|
EventServerChannel.BasicNack(ea.DeliveryTag, false, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
EventServerChannel.BasicConsume(queue, true, "", false, false, null, consumer);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Unicode;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using RabbitMQ.Client.Events;
|
||||||
|
using SimApi.Communications;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
|
namespace SimApi;
|
||||||
|
|
||||||
|
public partial class Synapse
|
||||||
|
{
|
||||||
|
private Dictionary<string, byte[]> ResponseCache { get; } = new();
|
||||||
|
|
||||||
|
private void RunRpcClient()
|
||||||
|
{
|
||||||
|
RpcClientChannel = CreateChannel(0, "RpcClient");
|
||||||
|
var queue = $"{Options.SysName}_{Options.AppName}_client_{Options.AppId}";
|
||||||
|
var router = $"client.{Options.AppName}.{Options.AppId}";
|
||||||
|
RpcClientChannel.QueueDeclare(queue, true, false, true, null);
|
||||||
|
RpcClientChannel.QueueBind(queue, Options.SysName, router, null);
|
||||||
|
var consumer = new EventingBasicConsumer(RpcClientChannel);
|
||||||
|
consumer.Received += (ch, ea) =>
|
||||||
|
{
|
||||||
|
ResponseCache.Add(ea.BasicProperties.CorrelationId, ea.Body.ToArray());
|
||||||
|
RpcClientChannel.BasicAck(ea.DeliveryTag, false);
|
||||||
|
Logger.LogDebug(
|
||||||
|
"RPC Response: ({BasicPropertiesCorrelationId}) {BasicPropertiesType}@{BasicPropertiesReplyTo} -> {OptionsAppName}\n{S}",
|
||||||
|
ea.BasicProperties.CorrelationId, ea.BasicProperties.Type, ea.BasicProperties.ReplyTo, Options.AppName,
|
||||||
|
Encoding.UTF8.GetString(ea.Body.ToArray()));
|
||||||
|
};
|
||||||
|
RpcClientChannel.BasicConsume(queue, false, "", false, false, null, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private object FireRpc(string app, string action, object param)
|
||||||
|
{
|
||||||
|
var paramJson = JsonSerializer.Serialize(param, new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
});
|
||||||
|
var router = $"server.{app}";
|
||||||
|
SimApiBaseResponse response;
|
||||||
|
var props = RpcClientChannel.CreateBasicProperties();
|
||||||
|
props.AppId = Options.AppId;
|
||||||
|
props.MessageId = Guid.NewGuid().ToString();
|
||||||
|
props.Type = action;
|
||||||
|
props.ReplyTo = Options.AppName;
|
||||||
|
RpcClientChannel.BasicPublish(Options.SysName, router, false, props, Encoding.UTF8.GetBytes(paramJson));
|
||||||
|
Logger.LogDebug("RPC Request: ({PropsMessageId}) {OptionsAppName} -> {Action}@{App}\n{ParamJson}",
|
||||||
|
props.MessageId,
|
||||||
|
Options.AppName, action, app, paramJson);
|
||||||
|
var ts = SimApiUtil.TimestampNow;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (SimApiUtil.TimestampNow - ts > Options.RpcTimeout)
|
||||||
|
{
|
||||||
|
response = new SimApiBaseResponse(502, "timeout");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (ResponseCache.TryGetValue(props.MessageId, out var value))
|
||||||
|
{
|
||||||
|
response = JsonSerializer.Deserialize<SimApiBaseResponse<object>>(
|
||||||
|
Encoding.UTF8.GetString(value));
|
||||||
|
ResponseCache.Remove(props.MessageId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Unicode;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using RabbitMQ.Client.Events;
|
||||||
|
using SimApi.Communications;
|
||||||
|
using SimApi.Exceptions;
|
||||||
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||||
|
|
||||||
|
namespace SimApi;
|
||||||
|
|
||||||
|
public partial class Synapse
|
||||||
|
{
|
||||||
|
private void RunRpcServer()
|
||||||
|
{
|
||||||
|
RpcServerChannel = CreateChannel(Options.RpcProcessorNum, "RpcServer");
|
||||||
|
var queue = $"{Options.SysName}_{Options.AppName}_server";
|
||||||
|
var router = $"server.{Options.AppName}";
|
||||||
|
RpcServerChannel.QueueDeclare(queue, true, false, true, null);
|
||||||
|
RpcServerChannel.QueueBind(queue, Options.SysName, router, null);
|
||||||
|
var consumer = new EventingBasicConsumer(RpcServerChannel);
|
||||||
|
consumer.Received += (ch, ea) =>
|
||||||
|
{
|
||||||
|
var reqBody = Encoding.UTF8.GetString(ea.Body.ToArray());
|
||||||
|
Logger.LogDebug(
|
||||||
|
"RPC Receive: ({BasicPropertiesMessageId}) {BasicPropertiesReplyTo} -> {BasicPropertiesType}@{OptionsAppName}\n{S}",
|
||||||
|
ea.BasicProperties.MessageId, ea.BasicProperties.ReplyTo, ea.BasicProperties.Type, Options.AppName,
|
||||||
|
reqBody);
|
||||||
|
var res = new SimApiBaseResponse(404, "method not found");
|
||||||
|
var method = RpcRegistry.FirstOrDefault(x => x.Key == ea.BasicProperties.Type);
|
||||||
|
if (method != null)
|
||||||
|
{
|
||||||
|
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
|
||||||
|
var mt = callClass.GetType().GetMethod(method.Method);
|
||||||
|
if (mt != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var pt = mt.GetParameters()[0].ParameterType;
|
||||||
|
if (pt == typeof(string))
|
||||||
|
{
|
||||||
|
var ret = mt.Invoke(callClass, new[] { reqBody });
|
||||||
|
res = new SimApiBaseResponse<object>(ret);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var ret = mt.Invoke(callClass, new[] { JsonSerializer.Deserialize(reqBody, pt) });
|
||||||
|
res = new SimApiBaseResponse<object>(ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (SimApiException e)
|
||||||
|
{
|
||||||
|
res = new SimApiBaseResponse(e.Code, e.Message);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
res = new SimApiBaseResponse(500, e.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var returnJson = JsonSerializer.Serialize((SimApiBaseResponse<object>)res, new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
});
|
||||||
|
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
|
||||||
|
var props = RpcServerChannel.CreateBasicProperties();
|
||||||
|
props.AppId = Options.AppId;
|
||||||
|
props.CorrelationId = ea.BasicProperties.MessageId;
|
||||||
|
props.MessageId = Guid.NewGuid().ToString();
|
||||||
|
props.ReplyTo = Options.AppName;
|
||||||
|
props.Type = ea.BasicProperties.Type;
|
||||||
|
RpcServerChannel.BasicPublish(Options.SysName, reply, false, props, Encoding.UTF8.GetBytes(returnJson));
|
||||||
|
Logger.LogDebug(
|
||||||
|
"Rpc Return: ({BasicPropertiesMessageId}) {BasicPropertiesType}@{OptionsAppName} -> {BasicPropertiesReplyTo}\n{ReturnJson}",
|
||||||
|
ea.BasicProperties.MessageId, ea.BasicProperties.Type, Options.AppName, ea.BasicProperties.ReplyTo,
|
||||||
|
returnJson);
|
||||||
|
};
|
||||||
|
RpcServerChannel.BasicConsume(queue, true, "", false, false, null, consumer);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
using RabbitMQ.Client.Exceptions;
|
||||||
|
using SimApi.Attributes;
|
||||||
|
using SimApi.Communications;
|
||||||
|
using SimApi.Configs;
|
||||||
|
using SimApi.Helpers;
|
||||||
|
|
||||||
|
namespace SimApi;
|
||||||
|
|
||||||
|
public partial class Synapse
|
||||||
|
{
|
||||||
|
private IServiceProvider Sp { get; }
|
||||||
|
|
||||||
|
private SimApiSynapseOptions Options { get; }
|
||||||
|
|
||||||
|
private ILogger<Synapse> Logger { get; }
|
||||||
|
|
||||||
|
private IConnection Connection { get; set; }
|
||||||
|
|
||||||
|
private IModel EventClientChannel { get; set; }
|
||||||
|
|
||||||
|
private IModel EventServerChannel { get; set; }
|
||||||
|
|
||||||
|
private IModel RpcClientChannel { get; set; }
|
||||||
|
|
||||||
|
private IModel RpcServerChannel { get; set; }
|
||||||
|
|
||||||
|
private List<RegisterItem> EventRegistry { get; set; }
|
||||||
|
|
||||||
|
private List<RegisterItem> RpcRegistry { get; set; }
|
||||||
|
|
||||||
|
public Synapse(SimApiOptions simApiOptions, ILogger<Synapse> logger, IServiceProvider sp)
|
||||||
|
{
|
||||||
|
Logger = logger;
|
||||||
|
Sp = sp;
|
||||||
|
Options = simApiOptions.SimApiSynapseOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init()
|
||||||
|
{
|
||||||
|
ProcessAttribute();
|
||||||
|
Logger.LogDebug("Synapse初始化配置信息: {Json}", SimApiUtil.Json(Options));
|
||||||
|
if (string.IsNullOrEmpty(Options.AppName) || string.IsNullOrEmpty(Options.SysName))
|
||||||
|
{
|
||||||
|
Logger.LogCritical("Synapse初始化失败: AppName or SysName 错误");
|
||||||
|
}
|
||||||
|
Options.AppId ??= Guid.NewGuid().ToString();
|
||||||
|
Logger.LogInformation("System Name: {SysName}\nApp Name: {AppName}\nAppId: {AppId}", Options.SysName,
|
||||||
|
Options.AppName, Options.AppId);
|
||||||
|
CreateConnection();
|
||||||
|
CheckAndCreateExchange();
|
||||||
|
//事件客户端
|
||||||
|
if (Options.DisableEventClient)
|
||||||
|
{
|
||||||
|
Logger.LogWarning("Event Client Disabled: DisableEventClient set true");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RunEventClient();
|
||||||
|
Logger.LogInformation("Event Client Ready");
|
||||||
|
}
|
||||||
|
|
||||||
|
//RPC客户端
|
||||||
|
if (Options.DisableRpcClient)
|
||||||
|
{
|
||||||
|
Logger.LogWarning("Rpc Client Disabled: DisableEventClient set true");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RunRpcClient();
|
||||||
|
Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout);
|
||||||
|
}
|
||||||
|
RunRpcServer();
|
||||||
|
RunEventServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public SimApiBaseResponse<T> Rpc<T>(string appName, string method, dynamic param)
|
||||||
|
{
|
||||||
|
var res = new SimApiBaseResponse(500, "Rpc Client Disabled!");
|
||||||
|
if (Options.DisableRpcClient)
|
||||||
|
{
|
||||||
|
Logger.LogError("Rpc Client Disabled!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res = FireRpc(appName, method, param);
|
||||||
|
}
|
||||||
|
return res as SimApiBaseResponse<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimApiBaseResponse<object> Rpc(string appName, string method, dynamic param)
|
||||||
|
{
|
||||||
|
return Rpc<object>(appName, method, param);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Event(string eventName, dynamic param)
|
||||||
|
{
|
||||||
|
if (Options.DisableEventClient)
|
||||||
|
{
|
||||||
|
Logger.LogError("Event Client Disabled!");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
FireEvent(eventName, param);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateConnection()
|
||||||
|
{
|
||||||
|
var factory = new ConnectionFactory
|
||||||
|
{
|
||||||
|
HostName = Options.MqHost,
|
||||||
|
Port = Options.MqPort,
|
||||||
|
VirtualHost = Options.MqVHost,
|
||||||
|
UserName = Options.MqUser,
|
||||||
|
Password = Options.MqPass
|
||||||
|
};
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Connection = factory.CreateConnection();
|
||||||
|
Logger.LogInformation("连接RabbitMQ服务器成功");
|
||||||
|
}
|
||||||
|
catch (BrokerUnreachableException e)
|
||||||
|
{
|
||||||
|
Logger.LogError("连接RabbitMQ失败: \n{Err}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private IModel CreateChannel(ushort processNum = 0, string desc = "unknow")
|
||||||
|
{
|
||||||
|
IModel channel = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var log = $"Channel [{desc}] 创建成功...";
|
||||||
|
channel = Connection.CreateModel();
|
||||||
|
if (processNum != 0)
|
||||||
|
{
|
||||||
|
channel.BasicQos(0, processNum, false);
|
||||||
|
log += $"最大处理器数量: {processNum}";
|
||||||
|
}
|
||||||
|
Logger.LogInformation(log);
|
||||||
|
}
|
||||||
|
catch (ConnectFailureException e)
|
||||||
|
{
|
||||||
|
Logger.LogError("Channel [{{Desc}}] 创建失败...\n {0}", e);
|
||||||
|
}
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckAndCreateExchange()
|
||||||
|
{
|
||||||
|
var channel = CreateChannel(0, "Exchange");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
channel.ExchangeDeclare(Options.SysName, ExchangeType.Topic, true, true, null);
|
||||||
|
Logger.LogDebug("Register Exchange Success");
|
||||||
|
}
|
||||||
|
catch (ConnectFailureException e)
|
||||||
|
{
|
||||||
|
Logger.LogError("Failed to declare Exchange.\n {Err}", e);
|
||||||
|
}
|
||||||
|
channel.Close();
|
||||||
|
Logger.LogDebug("Exchange Channel Closed");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ProcessAttribute()
|
||||||
|
{
|
||||||
|
EventRegistry = new List<RegisterItem>();
|
||||||
|
RpcRegistry = new List<RegisterItem>();
|
||||||
|
var stackTrace = new StackTrace();
|
||||||
|
var callingMethod = stackTrace.GetFrame(stackTrace.FrameCount - 1).GetMethod();
|
||||||
|
var assembly = callingMethod.DeclaringType.Assembly;
|
||||||
|
var types = assembly.GetTypes(); // 获取程序集中的所有类型
|
||||||
|
foreach (var type in types)
|
||||||
|
{
|
||||||
|
var methods = type.GetMethods(); // 获取类型中的所有方法
|
||||||
|
foreach (var method in methods)
|
||||||
|
{
|
||||||
|
if (method.IsDefined(typeof(SynapseEventAttribute), false))
|
||||||
|
{
|
||||||
|
var attribute =
|
||||||
|
(SynapseEventAttribute)Attribute.GetCustomAttribute(method, typeof(SynapseEventAttribute));
|
||||||
|
if (attribute != null)
|
||||||
|
{
|
||||||
|
EventRegistry.Add(new RegisterItem
|
||||||
|
{
|
||||||
|
Key = attribute.Name ?? method.Name,
|
||||||
|
Class = type,
|
||||||
|
Method = method.Name
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (method.IsDefined(typeof(SynapseRpcAttribute), false))
|
||||||
|
{
|
||||||
|
var attribute =
|
||||||
|
(SynapseRpcAttribute)Attribute.GetCustomAttribute(method, typeof(SynapseRpcAttribute));
|
||||||
|
if (attribute != null)
|
||||||
|
{
|
||||||
|
RpcRegistry.Add(new RegisterItem
|
||||||
|
{
|
||||||
|
Key = attribute.Name ?? $"{type.Name}.{method.Name}",
|
||||||
|
Class = type,
|
||||||
|
Method = method.Name
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var events = EventRegistry.Aggregate(string.Empty,
|
||||||
|
(current, ev) => current + $"\n |- {ev.Key} -> {ev.Method}@{ev.Class.Name}");
|
||||||
|
var rpcList = RpcRegistry.Aggregate(string.Empty,
|
||||||
|
(current, ev) => current + $"\n |- {ev.Key} -> {ev.Method}@{ev.Class.Name}");
|
||||||
|
Logger.LogInformation(">>> Synapse System 读取Event方法:{Event}\n>>>Synapse System 读取RPC方法:{Rpc}", events, rpcList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RegisterItem
|
||||||
|
{
|
||||||
|
public string Key { get; set; }
|
||||||
|
|
||||||
|
public Type Class { get; set; }
|
||||||
|
|
||||||
|
public string Method { get; set; }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user