fly 2 ani în urmă
părinte
comite
158be0e6bc

+ 4 - 3
src/InteractionCenter/InteractionCenterService.cs

@@ -5,11 +5,12 @@ namespace WingCloudServer.InteractionCenter
 {
     public class InteractionCenterService : JsonRpcService
     {
-        protected IOpLogDBService OpLogDBService;
+        protected IOpLogDBService _opLogDBService;
 
-        protected ILiveRoomDBService _LiveRoomDBService;
+        protected ILiveRoomDBService _liveRoomDBService;
 
+        protected IDistributedServerInfoDBService _distributedServerInfoDBService;
 
     }
-    
+
 }

+ 28 - 34
src/InteractionCenter/MasterInteractionCenterService.cs

@@ -11,15 +11,17 @@ using WingServerCommon.Config;
 
 namespace WingCloudServer.InteractionCenter
 {
-    public class MasterInteractionCenterService : InteractionCenterService, IMasterInteractionCenterService
+    public partial class MasterInteractionCenterService : InteractionCenterService, IMasterInteractionCenterService
     {
+
         public override void Load(JsonRpcClientPool jsonRpcClientPool)
         {
             base.Load(jsonRpcClientPool);
-            OpLogDBService = GetProxy<IOpLogDBService>();
-            _LiveRoomDBService = GetProxy<ILiveRoomDBService>();
-        }
+            _opLogDBService = GetProxy<IOpLogDBService>();
+            _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();
+            _liveRoomDBService = GetProxy<ILiveRoomDBService>();
 
+        }
 
         /// <summary>
         /// Get op logs from master server
@@ -28,7 +30,7 @@ namespace WingCloudServer.InteractionCenter
         /// <returns>The op log list</returns>
         public async Task<List<OperationLogDTO>> GetOpLogsFromMasterAsync(GetOpLogsFormMasterRequest request)
         {
-            return await OpLogDBService.GetOpLogAsync(request);
+            return await _opLogDBService.GetOpLogAsync(request);
         }
 
         /// <summary>
@@ -38,7 +40,7 @@ namespace WingCloudServer.InteractionCenter
         /// <returns>The op log list</returns>
         public async Task<List<OperationLogDTO>> GetOpLogsByCodesFromMasterAsync(GetOpLogsByCodesFormMasterRequest request)
         {
-            return await OpLogDBService.GetOpLogsByCodesAsync(request);
+            return await _opLogDBService.GetOpLogsByCodesAsync(request);
         }
 
         /// <summary>
@@ -48,43 +50,35 @@ namespace WingCloudServer.InteractionCenter
         /// <returns></returns>
         public async Task<bool> SyncOpLogToMasterAsync(SyncOpLogToMasterRequest request)
         {
-            return await OpLogDBService.SyncOpLogAsync(request);
+            return await _opLogDBService.SyncOpLogAsync(request);
+        }
+
+    }
+
+    public class SlaveInteractionCenterService : InteractionCenterService, ISlaveInteractionCenterService
+    {
+
+        public override void Load(JsonRpcClientPool jsonRpcClientPool)
+        {
+            base.Load(jsonRpcClientPool);
+            _opLogDBService = GetProxy<IOpLogDBService>();
         }
 
         /// <summary>
-        /// 实时同步业务数据到主服务器
+        /// Sychronize Receive ServiceData for slave
         /// </summary>
-        /// <param name="request"></param>
         /// <returns></returns>
-        public async Task<bool> SyncReceiveSlaveServiceDataAsync(SyncReceiveServiceDataRequest request)
+        public async Task<bool> SyncReceiveMasterServiceDataAsync(SyncReceiveServiceDataRequest request)
         {
-            var localServerUrl = ConfigurationManager.Host;
-            var baseLiveConsultation = JsonConvert.DeserializeObject<BaseLiveConsultationJson>(request.ServiceDataJson);
-            // 调用manager
-
-            // 判断是否存在其他服务器的用户
-            var getLiveRoomByCodeDBRequest = new GetLiveRoomByCodeDBRequest();
-            getLiveRoomByCodeDBRequest.LiveRoomCode = baseLiveConsultation.RoomCode;
-            var roomDTO = await _LiveRoomDBService.GetLiveRoomByCodeAsync(getLiveRoomByCodeDBRequest);
-            var users = roomDTO.UserInfos;
-            //操作人所在服务器
-            var operatorUser = users.FirstOrDefault(x => x.Code == baseLiveConsultation.OperatorCode);
-            //连接其他副服务器的用户
-            var usersConnectOtherSlave = users.Where(x => x.LoginServerHost != operatorUser.LoginServerHost && x.LoginServerHost != localServerUrl);
-            if (usersConnectOtherSlave != null && usersConnectOtherSlave.Count() > 0)
+            //执行oplogs
+            if (request.Oplogs != null && request.Oplogs.Count > 0)
             {
-                // 获取oplogs
-                var getOpLogsByCodesFormMasterRequest = new GetOpLogsByCodesFormMasterRequest();
-                getOpLogsByCodesFormMasterRequest.Codes = new List<string> { baseLiveConsultation.ConsultationRecordCode, baseLiveConsultation.RoomCode };
-                var oplogs = await OpLogDBService.GetOpLogsByCodesAsync(getOpLogsByCodesFormMasterRequest);
-                //_masterInteractionCenterService.SyncReceiveSlaveServiceDataAsync();
+                var syncCompleteOpLogsRequest = new SyncCompleteOpLogsRequest { Oplogs = request.Oplogs };
+                await _opLogDBService.SyncCompleteOpLogsAsync(syncCompleteOpLogsRequest);
             }
+            //调用manager
+
             return true;
         }
     }
-
-    public class SlaveInteractionCenterService : InteractionCenterService, ISlaveInteractionCenterService
-    {
-
-    }
 }

+ 93 - 0
src/InteractionCenter/MasterInteractionSyncService.cs

@@ -0,0 +1,93 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Threading.Tasks;
+using CSScriptLib;
+using Newtonsoft.Json;
+using WingInterfaceLibrary.DB.Request;
+using WingInterfaceLibrary.DTO.DistributedServerInfo;
+using WingInterfaceLibrary.Interface.DBInterface;
+using WingInterfaceLibrary.OpLog;
+using WingInterfaceLibrary.Request.DBRequest;
+using WingInterfaceLibrary.Result;
+using WingServerCommon.Config;
+using WingServerCommon.Service;
+
+namespace WingCloudServer.InteractionCenter
+{
+    public partial class MasterInteractionCenterService : IMasterInteractionCenterService
+    {
+
+        /// <summary>
+        /// 实时同步业务数据到主服务器
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        public async Task<bool> SyncReceiveSlaveServiceDataAsync(SyncReceiveServiceDataRequest request)
+        {
+
+            //todo 判断是当前服务器并且是主服务器 则只需要发送oplogs到其他副服务器
+
+            var localServerUrl = ConfigurationManager.Host;
+            var baseLiveConsultation = JsonConvert.DeserializeObject<BaseLiveConsultationJson>(request.ServiceDataJson);
+            // 调用manager
+
+            // 判断是否存在其他服务器的用户
+            var getLiveRoomByCodeDBRequest = new GetLiveRoomByCodeDBRequest();
+            getLiveRoomByCodeDBRequest.LiveRoomCode = baseLiveConsultation.RoomCode;
+            var roomDTO = await _liveRoomDBService.GetLiveRoomByCodeAsync(getLiveRoomByCodeDBRequest);
+            var users = roomDTO.UserInfos;
+            //操作人所在服务器
+            var operatorUser = users.FirstOrDefault(x => x.Code == baseLiveConsultation.OperatorCode);
+            //连接其他副服务器的用户
+            var usersConnectOtherSlave = users.Where(x => x.LoginServerHost != operatorUser.LoginServerHost && x.LoginServerHost != localServerUrl);
+            if (usersConnectOtherSlave != null && usersConnectOtherSlave.Count() > 0)
+            {
+                //获取oplogs
+                var getOpLogsByCodesFormMasterRequest = new GetOpLogsByCodesFormMasterRequest();
+                getOpLogsByCodesFormMasterRequest.Codes = new List<string> { baseLiveConsultation.ConsultationRecordCode, baseLiveConsultation.RoomCode };
+                var oplogs = await _opLogDBService.GetOpLogsByCodesAsync(getOpLogsByCodesFormMasterRequest);
+                //通知副服务器
+                var otherSlaves = usersConnectOtherSlave.Select(x => x.LoginServerHost).Distinct();
+                if (otherSlaves.Count() > 0)
+                {
+                    //动态注册副服务器服务
+                    var otherSlaveHosts = new List<string>();
+                    foreach (var item in otherSlaves)
+                    {
+                        var slaveHost = item.Replace("http:", "").Replace(".", "").Replace("/", "");
+                        DynamicSlaveService(request, slaveHost);
+                    }
+
+                }
+            }
+            return true;
+        }
+
+        public void DynamicSlaveService(SyncReceiveServiceDataRequest request, string host)
+        {
+            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;
+
+                        public class DynamicSlaveServiceClass : JsonRpcService
+                        {
+                            public void DynamicSlaveMethod(SyncReceiveServiceDataRequest request, IEnumerable<string> otherSlaveHosts)
+                            {
+                                var _dynamicService = GetProxy<IDynamicSlaveService" + host + @">();
+                                _dynamicService.DynamicSlaveAsync(request);
+                            }
+                        }";
+            Assembly compilecode = eval.CompileCode(str);
+            var ps = compilecode.GetType("css_root+DynamicSlaveServiceClass");
+            var obj = compilecode.CreateInstance("css_root+DynamicSlaveServiceClass");
+            var mes = ps.GetMethod("DynamicSlaveMethod");
+            mes.Invoke(obj, new object[] { request, host });
+        }
+    }
+}

+ 94 - 47
src/WingServer.cs

@@ -23,6 +23,7 @@ using WingInterfaceLibrary.Enum;
 using WingCloudServer.Plugin;
 using System.Reflection;
 using CSScriptLib;
+using WingInterfaceLibrary.DTO.DistributedServerInfo;
 
 namespace WingCloudServer
 {
@@ -79,9 +80,7 @@ namespace WingCloudServer
         {
             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());
             var inProcessServicesString = ConfigurationManager.GetParammeter<StringParameter>("Services", "InProcess").Value;
             var inProcessServices = inProcessServicesString.Split(',');
             var rpcHttpServicesString = ConfigurationManager.GetParammeter<StringParameter>("Services", "JsonRpcHttp").Value;
@@ -115,9 +114,8 @@ namespace WingCloudServer
 
                 }
             }
-
-            //TestCSScript(rpcClientPool);
-
+            var remoteRpcHttpServices = LoadRemoteServiceConfig(folder);//new RemoteServiceInfo[0]; 
+            rpcClientPool.Initialize(remoteRpcHttpServices.ToArray());
             //Rpc http service load and register to rpc http server
             if (ConfigurationManager.IsDistributed)
             {
@@ -126,7 +124,6 @@ namespace WingCloudServer
                     var masterInteractionCenterService = new MasterInteractionCenterService();
                     _rpcHttpServer.RegisterService(typeof(IMasterInteractionCenterService), masterInteractionCenterService);
                     masterInteractionCenterService.Load(rpcClientPool);
-
                 }
                 else
                 {
@@ -163,13 +160,6 @@ namespace WingCloudServer
                 }
             }
 
-            //load services
-            foreach (var service in _rpcServices)
-            {
-                var method = service.Key.GetMethod("Load");
-                method.Invoke(service.Value, new object[] { rpcClientPool });
-            }
-
             //load service if distrubuted system
             if (ConfigurationManager.IsDistributed)
             {
@@ -179,9 +169,18 @@ namespace WingCloudServer
                 }
                 else
                 {
-                    //TODO
+                    //动态注册副服务器实时同步服务
+                    RegisterDynamicSlaveInteractionCenterService(rpcClientPool);
                 }
             }
+
+            //load services
+            foreach (var service in _rpcServices)
+            {
+                var method = service.Key.GetMethod("Load");
+                method.Invoke(service.Value, new object[] { rpcClientPool });
+            }
+
             //最快服务器
             var vinnoServerService = new VinnoServerService();
             _rpcHttpServer.RegisterService(typeof(WingInterfaceLibrary.Interface.IVinnoServerService), vinnoServerService);
@@ -199,63 +198,53 @@ namespace WingCloudServer
             _serverListPlugin.Load(rpcClientPool);
 
         }
-
-        public void TestCSScript(JsonRpcClientPool rpcClientPool)
+        //todo 待验证是否可以调用本地的service
+        public void RegisterDynamicSlaveInteractionCenterService(JsonRpcClientPool rpcClientPool)
         {
-            //接口
-                    var eval = CSScript.Evaluator.ReferenceDomainAssemblies(DomainAssemblies.AllStaticNonGAC);
-                    var host = ConfigurationManager.Host.Replace("http:", "").Replace(".", "").Replace("/", "");
-                    var str = @"
+            var eval = CSScript.Evaluator.ReferenceDomainAssemblies(DomainAssemblies.AllStaticNonGAC);
+            var host = ConfigurationManager.Host.Replace("http:", "").Replace(".", "").Replace("/", "");
+            var str = @"
                     using System;
                     using System.Threading.Tasks;
-                    using System;
                     using WingServerCommon.Service;
                     using WingInterfaceLibrary.OpLog;
-                    using System.Threading.Tasks;
                     using System.Collections.Generic;
                     using JsonRpcLite.Rpc;
                     using WingInterfaceLibrary.Interface.DBInterface;
                     using WingInterfaceLibrary.DB.Request;
                                      
-                    public interface ICSScriptTest"+host+@"
+                    public interface IDynamicSlaveService" + host + @"
                     {
-                        Task<bool> CSScriptTestAsync();
+                        Task<bool> DynamicSlaveAsync(SyncReceiveServiceDataRequest request);
                     }
-                    public class CSScriptTestService : JsonRpcService,ICSScriptTest"+host+@"
+                    public class DynamicSlaveService : JsonRpcService,IDynamicSlaveService" + host + @"
                     {
-                        protected IOpLogDBService OpLogDBService;
-                        protected IDeviceInfoDBService deviceInfoDBService;
+                        protected ISlaveInteractionCenterService _slaveInteractionCenterService;
                         public override void Load(JsonRpcClientPool jsonRpcClientPool)
                         {
                             base.Load(jsonRpcClientPool);
-                            OpLogDBService = GetProxy<IOpLogDBService>();
-                            deviceInfoDBService = GetProxy<IDeviceInfoDBService>();
+                            _slaveInteractionCenterService = GetProxy<ISlaveInteractionCenterService>();
                         }
-                            public async Task<bool> CSScriptTestAsync()
+                        public async Task<bool> DynamicSlaveAsync(SyncReceiveServiceDataRequest request)
                         {
-                            var res = deviceInfoDBService.FindDictionaryItemsAsync(new FindDictionaryItemsDBRequest()).Result;
-                            Console.WriteLine(""flytest""+res.Count);
+                            await _slaveInteractionCenterService.SyncReceiveMasterServiceDataAsync(request);
                             return true;
                         }
                     }
-                    public class Test
+                    public class DynamicMethodClass
                     {
-                        public void Test1(JsonRpcClientPool rpcClientPool,JsonRpcServer _rpcHttpServer){
-                            var csScriptTestService = new CSScriptTestService();
-                            _rpcHttpServer.RegisterService(typeof(ICSScriptTest"+host+@"), csScriptTestService);
-                            csScriptTestService.Load(rpcClientPool);
+                        public void DynamicMethod(JsonRpcClientPool rpcClientPool,JsonRpcServer _rpcHttpServer){
+                            var DynamicSlaveService = new DynamicSlaveService();
+                            _rpcHttpServer.RegisterService(typeof(IDynamicSlaveService" + host + @"), DynamicSlaveService);
+                            DynamicSlaveService.Load(rpcClientPool);
                         }
                     }
                     ";
-
-                    Assembly compilecode = eval.CompileCode(str);
-
-
-
-                    var ps = compilecode.GetType("css_root+Test");
-                    var obj = compilecode.CreateInstance("css_root+Test");
-                    var mes = ps.GetMethod("Test1");
-                    mes.Invoke(obj, new object[] { rpcClientPool, _rpcHttpServer });
+            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 });
         }
 
         void LoadDataAfterRegister()
@@ -297,7 +286,7 @@ namespace WingCloudServer
             {
                 if (ConfigurationManager.IsMaster)
                 {
-                    //TODO determin if we need to assess slave server on master server
+                    DynamicRemoteSlaveService(resList);
                 }
                 else
                 {
@@ -342,6 +331,64 @@ namespace WingCloudServer
             return resList;
         }
 
+        public void DynamicRemoteSlaveService(List<RemoteServiceInfo> resList)
+        {
+            var slaveList = new List<DistributedServerInfoDTO>();
+            var host = ConfigurationManager.Host;
+            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;
+            using WingInterfaceLibrary.DTO.DistributedServerInfo;
+            using WingInterfaceLibrary.Request.DBRequest;
+            using System.Linq;
+
+            public class GetUrlListService : JsonRpcService
+            {
+                protected IDistributedServerInfoDBService _distributedServerInfoDBService;
+                public override void Load(JsonRpcClientPool jsonRpcClientPool)
+                {
+                    base.Load(jsonRpcClientPool);
+                    _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();
+                }
+
+                public List<DistributedServerInfoDTO> GetSlaveList(string host,List<DistributedServerInfoDTO> slaveList)
+                {
+                    var distributedServerInfoDBPagesRequest = new GetDistributedServerInfoDBPagesRequest();
+                    distributedServerInfoDBPagesRequest.CurrentPage = 1;
+                    distributedServerInfoDBPagesRequest.PageSize = 9999;
+                    var result = _distributedServerInfoDBService.GetDistributedServerInfoPagesAsync(distributedServerInfoDBPagesRequest).Result;
+                    if (result != null && result.TotalCount > 0)
+                    {
+                        slaveList = result.PageData;
+                        slaveList = slaveList.Where(x => x.ServerUrl != host).ToList();
+                    }
+                    return slaveList;
+                }
+
+            }
+            ";
+
+            //动态注册副服务器服务
+
+            foreach (var item in slaveList)
+            {
+                var serviceInfo = new RemoteServiceInfo()
+                {
+                    ServiceName = "IDynamicSlaveService" + item.ServerUrl.Replace("http:", "").Replace(".", "").Replace("/", ""),
+                    Url = ConfigurationManager.MasterUrl
+                };
+                resList.Add(serviceInfo);
+            }
+
+        }
+
         /// <summary>
         /// 初始化AIDiagnosis
         /// </summary>