From 77b2d14312533beceab751e086c6442de50ff5e2 Mon Sep 17 00:00:00 2001 From: yilei <87119538@qq.com> Date: Thu, 30 Apr 2020 10:14:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0MathClientHost=EF=BC=8C?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=8F=AA=E6=9C=89GrpcClient=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=9C=8D=E5=8A=A1=EF=BC=88WebApi=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Grpc.Extensions.sln | 9 ++- .../MathClientHost/MathClientHost.csproj | 25 +++++++ examples/CodeFirst/MathClientHost/Program.cs | 68 +++++++++++++++++++ .../MathClientHost/config/appsettings.json | 17 +++++ .../MathClientHost}/config/hostsettings.json | 0 .../.gitignore | 0 .../GreeterClientHost.csproj} | 0 .../Program.cs | 0 .../Properties/AssemblyInfo.cs | 0 .../app.config | 0 .../config/appsettings.json | 0 .../config/hostsettings.json | 3 + .../packages.config | 0 13 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 examples/CodeFirst/MathClientHost/MathClientHost.csproj create mode 100644 examples/CodeFirst/MathClientHost/Program.cs create mode 100644 examples/CodeFirst/MathClientHost/config/appsettings.json rename examples/{Greeter/GreeterClientTest => CodeFirst/MathClientHost}/config/hostsettings.json (100%) rename examples/Greeter/{GreeterClientTest => GreeterClientHost}/.gitignore (100%) rename examples/Greeter/{GreeterClientTest/GreeterClientTest.csproj => GreeterClientHost/GreeterClientHost.csproj} (100%) rename examples/Greeter/{GreeterClientTest => GreeterClientHost}/Program.cs (100%) rename examples/Greeter/{GreeterClientTest => GreeterClientHost}/Properties/AssemblyInfo.cs (100%) rename examples/Greeter/{GreeterClientTest => GreeterClientHost}/app.config (100%) rename examples/Greeter/{GreeterClientTest => GreeterClientHost}/config/appsettings.json (100%) create mode 100644 examples/Greeter/GreeterClientHost/config/hostsettings.json rename examples/Greeter/{GreeterClientTest => GreeterClientHost}/packages.config (100%) diff --git a/Grpc.Extensions.sln b/Grpc.Extensions.sln index c6c2216..addbb02 100644 --- a/Grpc.Extensions.sln +++ b/Grpc.Extensions.sln @@ -35,12 +35,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Extension.AspNetCore", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathServer.AspNetCore", "examples\CodeFirst\MathServer.AspNetCore\MathServer.AspNetCore.csproj", "{CF0443B5-DC32-4C80-B80E-A3F8D6E1792B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterClientTest", "examples\Greeter\GreeterClientTest\GreeterClientTest.csproj", "{ACCF4597-3748-4117-8633-1CB767F8CCC3}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterClientHost", "examples\Greeter\GreeterClientHost\GreeterClientHost.csproj", "{ACCF4597-3748-4117-8633-1CB767F8CCC3}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreeterClient", "examples\Greeter\GreeterClient\GreeterClient.csproj", "{724DFC8C-4B57-4C3F-811C-0463BE2A2829}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AspNetCore3.0", "AspNetCore3.0", "{291086F1-5608-46C9-9976-BD969B8C8256}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MathClientHost", "examples\CodeFirst\MathClientHost\MathClientHost.csproj", "{5B3DC8DC-AC90-4BF2-9E1C-DE46F9782160}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -103,6 +105,10 @@ Global {724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Debug|Any CPU.Build.0 = Debug|Any CPU {724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Release|Any CPU.ActiveCfg = Release|Any CPU {724DFC8C-4B57-4C3F-811C-0463BE2A2829}.Release|Any CPU.Build.0 = Release|Any CPU + {5B3DC8DC-AC90-4BF2-9E1C-DE46F9782160}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5B3DC8DC-AC90-4BF2-9E1C-DE46F9782160}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5B3DC8DC-AC90-4BF2-9E1C-DE46F9782160}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5B3DC8DC-AC90-4BF2-9E1C-DE46F9782160}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -119,6 +125,7 @@ Global {CF0443B5-DC32-4C80-B80E-A3F8D6E1792B} = {BE41EB1C-CB4F-4EDD-9E47-17D54308B014} {ACCF4597-3748-4117-8633-1CB767F8CCC3} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F} {724DFC8C-4B57-4C3F-811C-0463BE2A2829} = {94B12F76-D786-4D94-A9F4-8DD7DD73685F} + {5B3DC8DC-AC90-4BF2-9E1C-DE46F9782160} = {BE41EB1C-CB4F-4EDD-9E47-17D54308B014} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {89FBBEF6-DF3C-43D4-BD32-075D5E3D14F2} diff --git a/examples/CodeFirst/MathClientHost/MathClientHost.csproj b/examples/CodeFirst/MathClientHost/MathClientHost.csproj new file mode 100644 index 0000000..b2a446c --- /dev/null +++ b/examples/CodeFirst/MathClientHost/MathClientHost.csproj @@ -0,0 +1,25 @@ + + + + Exe + netcoreapp2.1 + + + + + + + + + + + + + + + + Always + + + + diff --git a/examples/CodeFirst/MathClientHost/Program.cs b/examples/CodeFirst/MathClientHost/Program.cs new file mode 100644 index 0000000..e6ff96f --- /dev/null +++ b/examples/CodeFirst/MathClientHost/Program.cs @@ -0,0 +1,68 @@ +using Grpc.Extension.Client; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.IO; +using static MathGrpc.MathGrpc; +using Grpc.Extension.Abstract.Model; + +namespace MathClientTest +{ + class Program + { + static void Main(string[] args) + { + //使用配制文件 + var configPath = Path.Combine(AppContext.BaseDirectory, "config"); + var host = new HostBuilder() + .ConfigureAppConfiguration((ctx, conf) => + { + conf.SetBasePath(configPath); + conf.AddJsonFile("appsettings.json", false, true); + }) + .ConfigureServices((ctx, services)=> { + services.AddGrpcClientExtensions(ctx.Configuration);//注入GrpcClientExtensions + services.AddGrpcClient("Math.Test");//注入grpc client + }) + .Build(); + + var provider = host.Services; + //配制GrpcClientApp + var clientApp = provider.GetService(); + clientApp. + //使用日志(默认使用LoggerFactory) + UseLogger((log) => + { + log.LoggerMonitor += (msg, type) => Console.WriteLine(GetLogTypeName(type) + ":" + msg); + log.LoggerError += (ex, type) => Console.WriteLine(GetLogTypeName(type) + ":" + ex); + }).Run(); + + //从容器获取client + var client = provider.GetService(); + + Console.WriteLine("start:"); + try + { + for (int i = 0; i < 10; i++) + { + var reply = client.Add(new MathGrpc.AddRequest() { Num1 = 1, Num2 = i}); + Console.WriteLine($"Add {i.ToString()}: {reply.Value}"); + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + + + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(); + } + + private static string GetLogTypeName(LogType logtype) + { + return Enum.GetName(typeof(LogType), logtype); + } + } +} diff --git a/examples/CodeFirst/MathClientHost/config/appsettings.json b/examples/CodeFirst/MathClientHost/config/appsettings.json new file mode 100644 index 0000000..5361d48 --- /dev/null +++ b/examples/CodeFirst/MathClientHost/config/appsettings.json @@ -0,0 +1,17 @@ +{ + "GrpcClient": { + //ַַ + "DiscoveryUrl": "http://192.168.8.6:8500", + //ַʱ() + "ServiceAddressCacheTime": 10, + //ĬϵĴ + "DefaultErrorCode": 4200000, + //Jaeger(OpenTracing) + "Jaeger": { + //Jaegerϵķ + "ServiceName": "MathClient", + "AgentIp": "192.168.8.11", + "AgentPort": 5775 + } + } +} \ No newline at end of file diff --git a/examples/Greeter/GreeterClientTest/config/hostsettings.json b/examples/CodeFirst/MathClientHost/config/hostsettings.json similarity index 100% rename from examples/Greeter/GreeterClientTest/config/hostsettings.json rename to examples/CodeFirst/MathClientHost/config/hostsettings.json diff --git a/examples/Greeter/GreeterClientTest/.gitignore b/examples/Greeter/GreeterClientHost/.gitignore similarity index 100% rename from examples/Greeter/GreeterClientTest/.gitignore rename to examples/Greeter/GreeterClientHost/.gitignore diff --git a/examples/Greeter/GreeterClientTest/GreeterClientTest.csproj b/examples/Greeter/GreeterClientHost/GreeterClientHost.csproj similarity index 100% rename from examples/Greeter/GreeterClientTest/GreeterClientTest.csproj rename to examples/Greeter/GreeterClientHost/GreeterClientHost.csproj diff --git a/examples/Greeter/GreeterClientTest/Program.cs b/examples/Greeter/GreeterClientHost/Program.cs similarity index 100% rename from examples/Greeter/GreeterClientTest/Program.cs rename to examples/Greeter/GreeterClientHost/Program.cs diff --git a/examples/Greeter/GreeterClientTest/Properties/AssemblyInfo.cs b/examples/Greeter/GreeterClientHost/Properties/AssemblyInfo.cs similarity index 100% rename from examples/Greeter/GreeterClientTest/Properties/AssemblyInfo.cs rename to examples/Greeter/GreeterClientHost/Properties/AssemblyInfo.cs diff --git a/examples/Greeter/GreeterClientTest/app.config b/examples/Greeter/GreeterClientHost/app.config similarity index 100% rename from examples/Greeter/GreeterClientTest/app.config rename to examples/Greeter/GreeterClientHost/app.config diff --git a/examples/Greeter/GreeterClientTest/config/appsettings.json b/examples/Greeter/GreeterClientHost/config/appsettings.json similarity index 100% rename from examples/Greeter/GreeterClientTest/config/appsettings.json rename to examples/Greeter/GreeterClientHost/config/appsettings.json diff --git a/examples/Greeter/GreeterClientHost/config/hostsettings.json b/examples/Greeter/GreeterClientHost/config/hostsettings.json new file mode 100644 index 0000000..5fa9430 --- /dev/null +++ b/examples/Greeter/GreeterClientHost/config/hostsettings.json @@ -0,0 +1,3 @@ +{ + "environment": "Development" +} \ No newline at end of file diff --git a/examples/Greeter/GreeterClientTest/packages.config b/examples/Greeter/GreeterClientHost/packages.config similarity index 100% rename from examples/Greeter/GreeterClientTest/packages.config rename to examples/Greeter/GreeterClientHost/packages.config