|
@@ -21,16 +21,21 @@ using WingInterfaceLibrary.Request.Authentication;
|
|
|
using JsonRpcLite.Services;
|
|
|
using WingInterfaceLibrary.Enum;
|
|
|
using WingCloudServer.Plugin;
|
|
|
+using System.Reflection;
|
|
|
+using CSScriptLib;
|
|
|
|
|
|
namespace WingCloudServer
|
|
|
{
|
|
|
- internal class WingServer
|
|
|
+ public class WingServer
|
|
|
{
|
|
|
private JsonRpcServer _rpcHttpServer;
|
|
|
private JsonRpcServer _rpcInProcessServer;
|
|
|
private JsonRpcInProcessEngine _inProcessEngine;
|
|
|
private JsonRpcHttpServerEngine _jsonRpcHttpServerEngine;
|
|
|
private Dictionary<Type, object> _rpcServices;
|
|
|
+ private static JsonRpcClientPool _rpcClientPool;
|
|
|
+ private static Dictionary<string, IDynamicSlaveService> _remoteServices = new Dictionary<string, IDynamicSlaveService>();
|
|
|
+
|
|
|
|
|
|
public WingServer(string host)
|
|
|
{
|
|
@@ -73,13 +78,56 @@ namespace WingCloudServer
|
|
|
_rpcInProcessServer.Stop();
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 动态添加远程服务
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="serviceName"></param>
|
|
|
+ /// <param name="url"></param>
|
|
|
+ public static void AddRemoteService<T>(string serviceName, string url) where T : IDynamicSlaveService
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (_remoteServices.Any(x => x.Key == serviceName))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var serviceInfo = new RemoteServiceInfo()
|
|
|
+ {
|
|
|
+ ServiceName = serviceName,
|
|
|
+ Url = url
|
|
|
+ };
|
|
|
+ _rpcClientPool.Add(serviceInfo);
|
|
|
+
|
|
|
+ var service = _rpcClientPool.GetJsonRpcClient<T>().CreateProxy<T>();
|
|
|
+ _remoteServices.TryAdd(serviceName, service);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineWarn("AddService err" + ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static IDynamicSlaveService WingServerGetProxy(string serviceName)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var service = _remoteServices.FirstOrDefault(x => x.Key == serviceName);
|
|
|
+ return service.Value;
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineWarn("WingServerGetProxy err" + ex);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
void InitializeServices()
|
|
|
{
|
|
|
var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory);
|
|
|
//initiaize rpc client pool
|
|
|
var remoteRpcHttpServices = LoadRemoteServiceConfig(folder);//new RemoteServiceInfo[0];
|
|
|
- var rpcClientPool = new JsonRpcClientPool(_inProcessEngine);
|
|
|
- rpcClientPool.Initialize(remoteRpcHttpServices.ToArray());
|
|
|
+ _rpcClientPool = new JsonRpcClientPool(_inProcessEngine);
|
|
|
+ _rpcClientPool.Initialize(remoteRpcHttpServices.ToArray());
|
|
|
var inProcessServicesString = ConfigurationManager.GetParammeter<StringParameter>("Services", "InProcess").Value;
|
|
|
var inProcessServices = inProcessServicesString.Split(',');
|
|
|
var rpcHttpServicesString = ConfigurationManager.GetParammeter<StringParameter>("Services", "JsonRpcHttp").Value;
|
|
@@ -114,7 +162,6 @@ namespace WingCloudServer
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//Rpc http service load and register to rpc http server
|
|
|
if (ConfigurationManager.IsDistributed)
|
|
|
{
|
|
@@ -122,12 +169,12 @@ namespace WingCloudServer
|
|
|
{
|
|
|
var masterInteractionCenterService = new MasterInteractionCenterService();
|
|
|
_rpcHttpServer.RegisterService(typeof(IMasterInteractionCenterService), masterInteractionCenterService);
|
|
|
- masterInteractionCenterService.Load(rpcClientPool);
|
|
|
-
|
|
|
+ masterInteractionCenterService.Load(_rpcClientPool);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//TODO determin if we need to assess slave server on master server
|
|
|
+ RegisterDynamicSlaveInteractionCenterService(_rpcClientPool);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -164,39 +211,91 @@ namespace WingCloudServer
|
|
|
foreach (var service in _rpcServices)
|
|
|
{
|
|
|
var method = service.Key.GetMethod("Load");
|
|
|
- method.Invoke(service.Value, new object[] { rpcClientPool });
|
|
|
+ method.Invoke(service.Value, new object[] { _rpcClientPool });
|
|
|
}
|
|
|
|
|
|
- //load service if distrubuted system
|
|
|
- if (ConfigurationManager.IsDistributed)
|
|
|
- {
|
|
|
- if (ConfigurationManager.IsMaster)
|
|
|
- {
|
|
|
- //_rpcHttpServer.RegisterService(IMaster); TODO
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //TODO
|
|
|
- }
|
|
|
- }
|
|
|
//最快服务器
|
|
|
var vinnoServerService = new VinnoServerService();
|
|
|
_rpcHttpServer.RegisterService(typeof(WingInterfaceLibrary.Interface.IVinnoServerService), vinnoServerService);
|
|
|
- vinnoServerService.Load(rpcClientPool);
|
|
|
+ vinnoServerService.Load(_rpcClientPool);
|
|
|
|
|
|
//plugin
|
|
|
_tokenVerifyPlugin = new TokenVerifyPlugin();
|
|
|
_rpcHttpServer.RegisterService(typeof(ITokenVerifyPlugin), _tokenVerifyPlugin);
|
|
|
- _tokenVerifyPlugin.Load(rpcClientPool);
|
|
|
+ _tokenVerifyPlugin.Load(_rpcClientPool);
|
|
|
_ipAddressPlugin = new IPAddressPlugin();
|
|
|
_rpcHttpServer.RegisterService(typeof(IIPAddressPlugin), _ipAddressPlugin);
|
|
|
- _ipAddressPlugin.Load(rpcClientPool);
|
|
|
+ _ipAddressPlugin.Load(_rpcClientPool);
|
|
|
_serverListPlugin = new ServerListPlugin();
|
|
|
_rpcHttpServer.RegisterService(typeof(IServerListPlugin), _serverListPlugin);
|
|
|
- _serverListPlugin.Load(rpcClientPool);
|
|
|
+ _serverListPlugin.Load(_rpcClientPool);
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 副服务器动态服务
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="rpcClientPool"></param>
|
|
|
+ public void RegisterDynamicSlaveInteractionCenterService(JsonRpcClientPool rpcClientPool)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var serverID = ConfigurationManager.GetParammeter<StringParameter>("General", "ServerID").Value;
|
|
|
+ //先注册 下面要调用
|
|
|
+ var slaveInteractionCenterService = new SlaveInteractionCenterService();
|
|
|
+ _rpcHttpServer.RegisterService(typeof(ISlaveInteractionCenterService), slaveInteractionCenterService);
|
|
|
+ slaveInteractionCenterService.Load(rpcClientPool);
|
|
|
+
|
|
|
+ var eval = CSScript.Evaluator.ReferenceDomainAssemblies(DomainAssemblies.AllStaticNonGAC);
|
|
|
+ var str = @$"
|
|
|
+ using System;
|
|
|
+ using System.Threading.Tasks;
|
|
|
+ using WingServerCommon.Service;
|
|
|
+ using WingInterfaceLibrary.OpLog;
|
|
|
+ using System.Collections.Generic;
|
|
|
+ using JsonRpcLite.Rpc;
|
|
|
+ using WingInterfaceLibrary.Interface.DBInterface;
|
|
|
+ using WingInterfaceLibrary.DB.Request;
|
|
|
+
|
|
|
+ public interface IDynamicSlaveService{serverID}
|
|
|
+ {{
|
|
|
+ Task<bool> DynamicSlaveAsync(SyncReceiveServiceDataRequest request);
|
|
|
+ }}
|
|
|
+ public class DynamicSlaveService : JsonRpcService,IDynamicSlaveService{serverID}
|
|
|
+ {{
|
|
|
+ protected ISlaveInteractionCenterService _slaveInteractionCenterService;
|
|
|
+ public override void Load(JsonRpcClientPool jsonRpcClientPool)
|
|
|
+ {{
|
|
|
+ base.Load(jsonRpcClientPool);
|
|
|
+ _slaveInteractionCenterService = GetProxy<ISlaveInteractionCenterService>();
|
|
|
+ }}
|
|
|
+ public async Task<bool> DynamicSlaveAsync(SyncReceiveServiceDataRequest request)
|
|
|
+ {{
|
|
|
+ await _slaveInteractionCenterService.SyncReceiveMasterServiceDataAsync(request);
|
|
|
+ return true;
|
|
|
+ }}
|
|
|
+ }}
|
|
|
+ public class DynamicMethodClass
|
|
|
+ {{
|
|
|
+ public void DynamicMethod(JsonRpcClientPool rpcClientPool,JsonRpcServer _rpcHttpServer) {{
|
|
|
+ var DynamicSlaveService = new DynamicSlaveService();
|
|
|
+ _rpcHttpServer.RegisterService(typeof(IDynamicSlaveService{serverID}), DynamicSlaveService);
|
|
|
+ DynamicSlaveService.Load(rpcClientPool);
|
|
|
+ }}
|
|
|
+ }}
|
|
|
+ ";
|
|
|
+ Assembly compilecode = eval.CompileCode(str);
|
|
|
+ var ps = compilecode.GetType("css_root+DynamicMethodClass");
|
|
|
+ var obj = compilecode.CreateInstance("css_root+DynamicMethodClass");
|
|
|
+ var mes = ps.GetMethod("DynamicMethod");
|
|
|
+ mes.Invoke(obj, new object[] { rpcClientPool, _rpcHttpServer });
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineWarn("RegisterDynamicSlaveInteractionCenterService err:" + ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
void LoadDataAfterRegister()
|
|
|
{
|
|
|
foreach (var service in _rpcServices)
|
|
@@ -281,49 +380,49 @@ namespace WingCloudServer
|
|
|
return resList;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 初始化AIDiagnosis
|
|
|
- /// </summary>
|
|
|
- private void InitAIDiagnosis()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- var dllsRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AIDiagnosis");
|
|
|
- var destFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Dependencies");
|
|
|
- CopyFilesAndDirs(dllsRootPath, destFolder);
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
+ /// <summary>
|
|
|
+ /// 初始化AIDiagnosis
|
|
|
+ /// </summary>
|
|
|
+ private void InitAIDiagnosis()
|
|
|
{
|
|
|
- Logger.WriteLineError($"InitAIDiagnosis error:{ex}");
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var dllsRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AIDiagnosis");
|
|
|
+ var destFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Dependencies");
|
|
|
+ CopyFilesAndDirs(dllsRootPath, destFolder);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ Logger.WriteLineError($"InitAIDiagnosis error:{ex}");
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- private void CopyFilesAndDirs(string sourceDir, string destDir)
|
|
|
- {
|
|
|
- if (!Directory.Exists(sourceDir))
|
|
|
- {
|
|
|
- return;
|
|
|
- }
|
|
|
- if (!Directory.Exists(destDir))
|
|
|
- {
|
|
|
- Directory.CreateDirectory(destDir);
|
|
|
- }
|
|
|
- string newPath;
|
|
|
- FileInfo fileInfo;
|
|
|
- string[] files = Directory.GetFiles(sourceDir);
|
|
|
- foreach (string path in files)
|
|
|
- {
|
|
|
- fileInfo = new FileInfo(path);
|
|
|
- newPath = destDir + "\\" + fileInfo.Name;
|
|
|
- File.Copy(path, newPath, true);
|
|
|
- }
|
|
|
- string[] dirs = Directory.GetDirectories(sourceDir);
|
|
|
- foreach (string path in dirs)
|
|
|
+ private void CopyFilesAndDirs(string sourceDir, string destDir)
|
|
|
{
|
|
|
- DirectoryInfo directory = new DirectoryInfo(path);
|
|
|
- string newDir = destDir + "\\" + directory.Name;
|
|
|
- CopyFilesAndDirs(path + "\\", newDir + "\\");
|
|
|
+ if (!Directory.Exists(sourceDir))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!Directory.Exists(destDir))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(destDir);
|
|
|
+ }
|
|
|
+ string newPath;
|
|
|
+ FileInfo fileInfo;
|
|
|
+ string[] files = Directory.GetFiles(sourceDir);
|
|
|
+ foreach (string path in files)
|
|
|
+ {
|
|
|
+ fileInfo = new FileInfo(path);
|
|
|
+ newPath = destDir + "\\" + fileInfo.Name;
|
|
|
+ File.Copy(path, newPath, true);
|
|
|
+ }
|
|
|
+ string[] dirs = Directory.GetDirectories(sourceDir);
|
|
|
+ foreach (string path in dirs)
|
|
|
+ {
|
|
|
+ DirectoryInfo directory = new DirectoryInfo(path);
|
|
|
+ string newDir = destDir + "\\" + directory.Name;
|
|
|
+ CopyFilesAndDirs(path + "\\", newDir + "\\");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
}
|