CrazyIter 5 år sedan
förälder
incheckning
4c235dba9e

+ 19 - 0
TEAMModelOS.GRPC/Startup.cs

@@ -2,9 +2,12 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
+using Grpc.Extension.Abstract;
+using Grpc.Extension.AspNetCore;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
 using Microsoft.Extensions.Hosting;
 
@@ -12,11 +15,18 @@ namespace TEAMModelOS.GRPC
 {
     public class Startup
     {
+        private IConfiguration _conf;
+        public Startup(IConfiguration conf)
+        {
+            _conf = conf;
+        }
         // This method gets called by the runtime. Use this method to add services to the container.
         // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
         public void ConfigureServices(IServiceCollection services)
         {
             services.AddGrpc();
+            //添加Grpc扩展
+            services.AddGrpcExtensions(_conf);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -38,6 +48,15 @@ namespace TEAMModelOS.GRPC
                     await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                 });
             });
+            //CodeFirst的Grpc(会自动扫描TStartup所在程序集下的IGrpcService)
+            app.UseGrpcExtensions<IGrpcService>(options =>
+            {
+                //CodeFirst配制
+                options.GlobalPackage = "TEAMModelOS.GRPC";
+                options.ProtoNameSpace = "TEAMModelOS.GRPC";
+            })
+            //CodeFirst生成proto
+            .UseProtoGenerate("Protos", false);
         }
     }
 }

+ 9 - 1
TEAMModelOS.GRPC/TEAMModelOS.GRPC.csproj

@@ -7,7 +7,11 @@
     <DocumentationFile>TEAMModelOS.GRPC.xml</DocumentationFile>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="Grpc.AspNetCore" Version="2.27.0" />
+    <PackageReference Include="Grpc.AspNetCore" Version="2.24.0" />
+    <PackageReference Include="Grpc.Tools" Version="2.24.0">
+      <PrivateAssets>all</PrivateAssets>
+      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+    </PackageReference>
   </ItemGroup>
 
   <ItemGroup>
@@ -18,4 +22,8 @@
     <Folder Include="Services\Learn\" />
     <Folder Include="Services\Syllabus\" />
   </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\TEAMModelOS.SDK\TEAMModelOS.SDK.csproj" />
+  </ItemGroup>
 </Project>

+ 32 - 1
TEAMModelOS.GRPC/appsettings.Development.json

@@ -4,7 +4,38 @@
       "Default": "Debug",
       "System": "Information",
       "Grpc": "Information",
-      "Microsoft": "Information"
+      "Microsoft": "Information",
+      "Microsoft.Hosting.Lifetime": "Information"
+    }
+  },
+  "urls": "https://[*]:5001",
+  "GrpcServer": {
+    //用于grpc启动后注册到服务发现的ip地址段
+    "ServiceAddress": "192.168.*.*:",
+    //是否启用服务注册和服务发现,默认是true
+    "EnableDiscovery": true,
+    //服务发现服务器地址
+    "DiscoveryUrl": "http://106.12.23.251:8500",
+    //注册到服务发现的服务名称
+    "DiscoveryServiceName": "TEAMModelOS.GRPC",
+    //服务发现主动TTL的时间(秒)
+    "DiscoveryTTLInterval": 10,
+    //注册到服务发现的服务Tag
+    "DiscoveryServiceTags": "v-1.0.0.1",
+    //默认错误码
+    "DefaultErrorCode": 4300000,
+    //Jaeger配制(OpenTracing)
+    "Jaeger": {
+      //是否启用Jaeger,默认false
+      "Enable": false,
+      "AgentIp": "192.168.8.11",
+      "AgentPort": 5775
+    }
+  },
+  "AllowedHosts": "*",
+  "Kestrel": {
+    "EndpointDefaults": {
+      "Protocols": "Http2"
     }
   }
 }

+ 24 - 0
TEAMModelOS.GRPC/appsettings.json

@@ -6,6 +6,30 @@
       "Microsoft.Hosting.Lifetime": "Information"
     }
   },
+  "urls": "https://[*]:5001",
+  "GrpcServer": {
+    //用于grpc启动后注册到服务发现的ip地址段
+    "ServiceAddress": "192.168.*.*:",
+    //是否启用服务注册和服务发现,默认是true
+    "EnableDiscovery": true,
+    //服务发现服务器地址
+    "DiscoveryUrl": "http://106.12.23.251:8500",
+    //注册到服务发现的服务名称
+    "DiscoveryServiceName": "TEAMModelOS.GRPC",
+    //服务发现主动TTL的时间(秒)
+    "DiscoveryTTLInterval": 10,
+    //注册到服务发现的服务Tag
+    "DiscoveryServiceTags": "v-1.0.0.1",
+    //默认错误码
+    "DefaultErrorCode": 4300000,
+    //Jaeger配制(OpenTracing)
+    "Jaeger": {
+      //是否启用Jaeger,默认false
+      "Enable": false,
+      "AgentIp": "192.168.8.11",
+      "AgentPort": 5775
+    }
+  },
   "AllowedHosts": "*",
   "Kestrel": {
     "EndpointDefaults": {

+ 2 - 2
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -13,8 +13,8 @@
     <PackageReference Include="ClouDASLibx" Version="1.1.4" />
     <PackageReference Include="Consul" Version="0.7.2.6" />
     <PackageReference Include="DocumentFormat.OpenXml" Version="2.10.1" />
-    <PackageReference Include="Grpc.AspNetCore.Server" Version="2.27.0" />
-    <PackageReference Include="Grpc.Core" Version="2.27.0" />
+    <PackageReference Include="Grpc.AspNetCore.Server" Version="2.24.0" />
+    <PackageReference Include="Grpc.Core" Version="2.24.0" />
     <PackageReference Include="HtmlAgilityPack" Version="1.11.23" />
     <PackageReference Include="IdentityModel" Version="4.2.0" />
     <PackageReference Include="Jaeger" Version="0.3.7" />