Skip to content

Commit

Permalink
修改rpc客户端
Browse files Browse the repository at this point in the history
  • Loading branch information
brucehu123 committed Mar 19, 2019
1 parent a1b4cb7 commit f36992b
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 46 deletions.
74 changes: 56 additions & 18 deletions Study.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Study.Core;
using IServiceProxyFactory = Study.ProxyGenerator.IServiceProxyFactory;
using IServiceProxyGenerater = Study.ProxyGenerator.IServiceProxyGenerater;

namespace Study.Client
{
Expand Down Expand Up @@ -107,32 +112,65 @@ class Program

static void Main(string[] args)
{
Console.WriteLine("开始客户端!");

var proxyGenerater = new ServiceProxyGenerater(new ServiceIdGenerator(), new LoggerFactory().CreateLogger<ServiceProxyGenerater>());
var services = proxyGenerater.GenerateProxys(new[] { typeof(IUserService) }).ToArray();
var proxyFactory = new ServiceProxyFactory(new RemoteServiceInvoker());
var userService = proxyFactory.CreateProxy<IUserService>(services.Single(typeof(IUserService).GetTypeInfo().IsAssignableFrom));

Console.WriteLine("开始一百次调用");
ServiceProvider provider = null;

var builder = new HostBuilder()
.ConfigureServices((context, services) =>
{
provider = services.BuildServiceProvider();
})
.ConfigureLogging((context, logger) =>
{
logger.AddConfiguration(context.Configuration.GetSection("Logging"));
logger.AddConsole();
})
.AddClientProxy()
.AddRpcClient();
using (var host=builder.UseConsoleLifetime().Build())
{
host.Start();

Stopwatch sw = Stopwatch.StartNew();
var proxyGenerater = provider.GetService<IServiceProxyGenerater>();
var remoteServices = proxyGenerater.GenerateProxys(new[] { typeof(IUserService) }).ToArray();
var proxyFactory = provider.GetService<IServiceProxyFactory>();
var userService = proxyFactory.CreateProxy<IUserService>(remoteServices.Single(typeof(IUserService).GetTypeInfo().IsAssignableFrom));

for (var i = 0; i < 100; i++)
{
Stopwatch watch = Stopwatch.StartNew();
var result = userService.GetUserNameAsync(i).Result;
var result = userService.GetUserNameAsync(10).Result;
Console.WriteLine(result);
watch.Stop();

Console.WriteLine($"{watch.ElapsedMilliseconds}");
host.WaitForShutdown();
}

sw.Stop();

Console.WriteLine($"调用结束,耗时:{sw.ElapsedMilliseconds} 毫秒");

Console.ReadLine();



//Console.WriteLine("开始客户端!");

//var proxyGenerater = new ServiceProxyGenerater(new ServiceIdGenerator(), new LoggerFactory().CreateLogger<ServiceProxyGenerater>());
//var services = proxyGenerater.GenerateProxys(new[] { typeof(IUserService) }).ToArray();
//var proxyFactory = new ServiceProxyFactory(new RemoteServiceInvoker());
//var userService = proxyFactory.CreateProxy<IUserService>(services.Single(typeof(IUserService).GetTypeInfo().IsAssignableFrom));

//Console.WriteLine("开始一百次调用");

//Stopwatch sw = Stopwatch.StartNew();

//for (var i = 0; i < 100; i++)
//{
// Stopwatch watch = Stopwatch.StartNew();
// var result = userService.GetUserNameAsync(i).Result;
// Console.WriteLine(result);
// watch.Stop();

// Console.WriteLine($"{watch.ElapsedMilliseconds}");
//}

//sw.Stop();

//Console.WriteLine($"调用结束,耗时:{sw.ElapsedMilliseconds} 毫秒");


}

Expand Down
1 change: 1 addition & 0 deletions Study.Client/Study.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="DotNetty.Codecs" Version="0.6.0" />
<PackageReference Include="DotNetty.Transport" Version="0.6.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

Expand Down
16 changes: 16 additions & 0 deletions Study.Core/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Linq;
using System.Reflection;
using System.Text;
using Study.Core.Runtime.Client;
using Study.Core.Runtime.Client.Imp;
using Study.Core.Runtime.Server.Imp;


Expand Down Expand Up @@ -46,5 +48,19 @@ public static IHostBuilder AddRpcServer(this IHostBuilder builder)
});
return builder;
}

public static IHostBuilder AddRpcClient(this IHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
{
services.AddOptions();
services.AddLogging();
services.AddHostedService<RpcClientHost>();
services.AddSingleton<IRemoteServiceInvoker, RemoteServiceInvoker>();
services.AddSingleton<IServiceIdGenerator, ServiceIdGenerator>();
});
return builder;
}
}
}
2 changes: 1 addition & 1 deletion Study.Core/Runtime/Client/Imp/RemoteServiceInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class RemoteServiceInvoker : IRemoteServiceInvoker
public Task<string> InvokeAsync(RemoteInvokeContext context)
{
if (context == null)
throw new ArgumentNullException("context");
throw new ArgumentNullException(nameof(context));
if (context.ServiceId == null)
throw new ArgumentNullException("serviceId");
TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
Expand Down
34 changes: 34 additions & 0 deletions Study.Core/Runtime/Client/RpcClientHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Study.Core.Runtime.Client
{
public class RpcClientHost : IHostedService
{
private readonly ILogger<RpcClientHost> _logger;

public RpcClientHost(ILogger<RpcClientHost> logger)
{
_logger = logger;
}

public Task StartAsync(CancellationToken cancellationToken)
{
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("客户端程序启动");
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken)
{
if (_logger.IsEnabled(LogLevel.Debug))
_logger.LogDebug("客户端程序停止");
return Task.CompletedTask;
}
}
}
32 changes: 32 additions & 0 deletions Study.ProxyGenerator/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Study.ProxyGenerator.Implementation;

namespace Study.ProxyGenerator
{
public static class HostBuilderExtensions
{
public static IHostBuilder AddClientProxy(this IHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
{
services.AddSingleton<IServiceProxyGenerater, ServiceProxyGenerater>();
services.AddSingleton<IServiceProxyFactory, ServiceProxyFactory>();
});
return builder;
}
}

public static class ServiceCollectionExtensions
{
public static ServiceCollection AddClientProxyServices(this ServiceCollection services)
{
services.AddSingleton<IServiceProxyGenerater, ServiceProxyGenerater>();
services.AddSingleton<IServiceProxyFactory, ServiceProxyFactory>();
return services;
}
}
}
23 changes: 0 additions & 23 deletions Study.ProxyGenerator/UserServiceClientProxy.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Study.Transport.DotNetty/DotNettySocketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ public void OnReceive(IChannelHandlerContext context, object message)
await context.WriteAndFlushAsync(buffer);
}).ConfigureAwait(false);


//todo: send result to remote client
}
}
}
2 changes: 1 addition & 1 deletion StudyRpc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Study.ProxyGenerator", "Stu
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Study.Transport.DotNetty", "Study.Transport.DotNetty\Study.Transport.DotNetty.csproj", "{737CFC61-6515-42D1-BD9E-201268E5E49D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{D66A4A7C-8488-4316-B0A9-0C5046216B38}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{D66A4A7C-8488-4316-B0A9-0C5046216B38}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
Expand Down

0 comments on commit f36992b

Please sign in to comment.