forked from fanliang11/surging
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fanliang11#9 from dotnetcore/master
merge
- Loading branch information
Showing
18 changed files
with
461 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Autofac; | ||
using Microsoft.Extensions.Logging; | ||
using Surging.Core.CPlatform; | ||
using Surging.Core.CPlatform.Module; | ||
using Surging.Core.CPlatform.Runtime.Server; | ||
using Surging.Core.CPlatform.Runtime.Server.Implementation; | ||
using Surging.Core.Grpc.Runtime; | ||
using Surging.Core.Grpc.Runtime.Implementation; | ||
|
||
namespace Surging.Core.Grpc | ||
{ | ||
public class GrpcModule : EnginePartModule | ||
{ | ||
public override void Initialize(AppModuleContext context) | ||
{ | ||
base.Initialize(context); | ||
} | ||
|
||
protected override void RegisterBuilder(ContainerBuilderWrapper builder) | ||
{ | ||
builder.Register(provider => | ||
{ | ||
return new DefaultGrpcServiceEntryProvider( | ||
provider.Resolve<IServiceEntryProvider>(), | ||
provider.Resolve<ILogger<DefaultGrpcServiceEntryProvider>>(), | ||
provider.Resolve<CPlatformContainer>() | ||
); | ||
}).As(typeof(IGrpcServiceEntryProvider)).SingleInstance(); | ||
if (AppConfig.ServerOptions.Protocol == CommunicationProtocol.WS) | ||
{ | ||
RegisterDefaultProtocol(builder); | ||
} | ||
else if (AppConfig.ServerOptions.Protocol == CommunicationProtocol.None) | ||
{ | ||
RegisterGrpcProtocol(builder); | ||
} | ||
} | ||
|
||
private static void RegisterDefaultProtocol(ContainerBuilderWrapper builder) | ||
{ | ||
|
||
builder.Register(provider => | ||
{ | ||
return new GrpcServerMessageListener( | ||
provider.Resolve<ILogger<GrpcServerMessageListener>>(), | ||
provider.Resolve<IGrpcServiceEntryProvider>() | ||
); | ||
}).SingleInstance(); | ||
builder.Register(provider => | ||
{ | ||
var messageListener = provider.Resolve<GrpcServerMessageListener>(); | ||
return new DefaultServiceHost(async endPoint => | ||
{ | ||
await messageListener.StartAsync(endPoint); | ||
return messageListener; | ||
}, null); | ||
|
||
}).As<IServiceHost>(); | ||
} | ||
|
||
private static void RegisterGrpcProtocol(ContainerBuilderWrapper builder) | ||
{ | ||
builder.Register(provider => | ||
{ | ||
return new GrpcServerMessageListener(provider.Resolve<ILogger<GrpcServerMessageListener>>(), | ||
provider.Resolve<IGrpcServiceEntryProvider>() | ||
); | ||
}).SingleInstance(); | ||
builder.Register(provider => | ||
{ | ||
var messageListener = provider.Resolve<GrpcServerMessageListener>(); | ||
return new GrpcServiceHost(async endPoint => | ||
{ | ||
await messageListener.StartAsync(endPoint); | ||
return messageListener; | ||
}); | ||
|
||
}).As<IServiceHost>(); | ||
} | ||
} | ||
} | ||
|
84 changes: 84 additions & 0 deletions
84
src/Surging.Core/Surging.Core.Grpc/GrpcServerMessageListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using Grpc.Core; | ||
using Microsoft.Extensions.Logging; | ||
using Surging.Core.CPlatform.Messages; | ||
using Surging.Core.CPlatform.Transport; | ||
using Surging.Core.Grpc.Runtime; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Surging.Core.Grpc | ||
{ | ||
public class GrpcServerMessageListener: IMessageListener, IDisposable | ||
{ | ||
private Server _server; | ||
private readonly ILogger<GrpcServerMessageListener> _logger; | ||
private readonly IGrpcServiceEntryProvider _grpcServiceEntryProvider; | ||
|
||
public GrpcServerMessageListener(ILogger<GrpcServerMessageListener> logger, | ||
IGrpcServiceEntryProvider grpcServiceEntryProvider) | ||
{ | ||
_logger = logger; | ||
_grpcServiceEntryProvider = grpcServiceEntryProvider; | ||
} | ||
public Task StartAsync(EndPoint endPoint) | ||
{ | ||
var ipEndPoint = endPoint as IPEndPoint; | ||
_server = new Server() { Ports = { new ServerPort(ipEndPoint.Address.ToString(), ipEndPoint.Port, ServerCredentials.Insecure) } }; | ||
|
||
try | ||
{ | ||
var entries = _grpcServiceEntryProvider.GetEntries(); | ||
|
||
var serverServiceDefinitions = new List<ServerServiceDefinition>(); | ||
foreach (var entry in entries) | ||
{ | ||
|
||
var baseType = entry.Type.BaseType.BaseType; | ||
var definitionType = baseType?.DeclaringType; | ||
|
||
var methodInfo = definitionType?.GetMethod("BindService", new Type[] { baseType }); | ||
if (methodInfo != null) | ||
{ | ||
var serviceDescriptor = methodInfo.Invoke(null, new object[] { entry.Behavior }) as ServerServiceDefinition; | ||
if (serviceDescriptor != null) | ||
{ | ||
_server.Services.Add(serviceDescriptor); | ||
continue; | ||
} | ||
} | ||
} | ||
_server.Start(); | ||
if (_logger.IsEnabled(LogLevel.Debug)) | ||
_logger.LogDebug($"Grpc服务主机启动成功,监听地址:{endPoint}。"); | ||
} | ||
catch | ||
{ | ||
_logger.LogError($"Grpc服务主机启动失败,监听地址:{endPoint}。 "); | ||
} | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Server Server | ||
{ | ||
get | ||
{ | ||
return _server; | ||
} | ||
} | ||
|
||
public event ReceivedDelegate Received; | ||
|
||
public Task OnReceived(IMessageSender sender, TransportMessage message) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_server.ShutdownAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Surging.Core.CPlatform; | ||
using Surging.Core.CPlatform.Runtime.Server.Implementation; | ||
using Surging.Core.CPlatform.Transport; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Surging.Core.Grpc | ||
{ | ||
public class GrpcServiceHost : ServiceHostAbstract | ||
{ | ||
#region Field | ||
|
||
private readonly Func<EndPoint, Task<IMessageListener>> _messageListenerFactory; | ||
private IMessageListener _serverMessageListener; | ||
|
||
#endregion Field | ||
|
||
public GrpcServiceHost(Func<EndPoint, Task<IMessageListener>> messageListenerFactory) : base(null) | ||
{ | ||
_messageListenerFactory = messageListenerFactory; | ||
} | ||
|
||
#region Overrides of ServiceHostAbstract | ||
|
||
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> | ||
public override void Dispose() | ||
{ | ||
(_serverMessageListener as IDisposable)?.Dispose(); | ||
} | ||
|
||
/// <summary> | ||
/// 启动主机。 | ||
/// </summary> | ||
/// <param name="endPoint">主机终结点。</param> | ||
/// <returns>一个任务。</returns> | ||
public override async Task StartAsync(EndPoint endPoint) | ||
{ | ||
if (_serverMessageListener != null) | ||
return; | ||
_serverMessageListener = await _messageListenerFactory(endPoint); | ||
|
||
} | ||
|
||
public override async Task StartAsync(string ip, int port) | ||
{ | ||
if (_serverMessageListener != null) | ||
return; | ||
_serverMessageListener = await _messageListenerFactory(new IPEndPoint(IPAddress.Parse(ip), AppConfig.ServerOptions.Ports.GrpcPort)); | ||
} | ||
|
||
#endregion Overrides of ServiceHostAbstract | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Surging.Core/Surging.Core.Grpc/Runtime/GrpcServiceEntry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Surging.Core.CPlatform.Ioc; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Surging.Core.Grpc.Runtime | ||
{ | ||
public class GrpcServiceEntry | ||
{ | ||
|
||
public Type Type { get; set; } | ||
|
||
public IServiceBehavior Behavior { get; set; } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Surging.Core/Surging.Core.Grpc/Runtime/IGrpcServiceEntryProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Surging.Core.Grpc.Runtime | ||
{ | ||
public interface IGrpcServiceEntryProvider | ||
{ | ||
List<GrpcServiceEntry> GetEntries(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Surging.Core/Surging.Core.Grpc/Runtime/Implementation/DefaultGrpcServiceEntryProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Surging.Core.CPlatform; | ||
using Surging.Core.CPlatform.Ioc; | ||
using Surging.Core.CPlatform.Runtime.Server; | ||
using Surging.Core.CPlatform.Runtime.Server.Implementation.ServiceDiscovery.Attributes; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace Surging.Core.Grpc.Runtime.Implementation | ||
{ | ||
public class DefaultGrpcServiceEntryProvider: IGrpcServiceEntryProvider | ||
{ | ||
#region Field | ||
|
||
private readonly IEnumerable<Type> _types; | ||
private readonly ILogger<DefaultGrpcServiceEntryProvider> _logger; | ||
private readonly CPlatformContainer _serviceProvider; | ||
private List<GrpcServiceEntry> _grpcServiceEntries; | ||
|
||
#endregion Field | ||
|
||
#region Constructor | ||
|
||
public DefaultGrpcServiceEntryProvider(IServiceEntryProvider serviceEntryProvider, | ||
ILogger<DefaultGrpcServiceEntryProvider> logger, | ||
CPlatformContainer serviceProvider) | ||
{ | ||
_types = serviceEntryProvider.GetTypes(); | ||
_logger = logger; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
#endregion Constructor | ||
|
||
#region Implementation of IUdpServiceEntryProvider | ||
|
||
/// <summary> | ||
/// 获取服务条目集合。 | ||
/// </summary> | ||
/// <returns>服务条目集合。</returns> | ||
public List<GrpcServiceEntry> GetEntries() | ||
{ | ||
var services = _types.ToArray(); | ||
if (_grpcServiceEntries == null) | ||
{ | ||
_grpcServiceEntries = new List<GrpcServiceEntry>(); | ||
foreach (var service in services) | ||
{ | ||
var entry = CreateServiceEntry(service); | ||
if (entry != null) | ||
{ | ||
_grpcServiceEntries.Add(entry); | ||
} | ||
} | ||
if (_logger.IsEnabled(LogLevel.Debug)) | ||
{ | ||
_logger.LogDebug($"发现了以下grpc服务:{string.Join(",", _grpcServiceEntries.Select(i => i.Type.FullName))}。"); ; | ||
} | ||
} | ||
return _grpcServiceEntries; | ||
} | ||
|
||
public GrpcServiceEntry CreateServiceEntry(Type service) | ||
{ | ||
GrpcServiceEntry result = null; | ||
var objInstance = _serviceProvider.GetInstances(service); | ||
var behavior = objInstance as IServiceBehavior; | ||
if (behavior != null) | ||
result = new GrpcServiceEntry | ||
{ | ||
Behavior = behavior, | ||
Type = behavior.GetType() | ||
}; | ||
return result; | ||
} | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.