Commit 0405a997 by dingsongjie

完善 grpc demo

parent 51b2b230
......@@ -12,8 +12,16 @@
</ItemGroup>
<ItemGroup>
<None Remove="Protos\commonCommandResponse.proto" AdditionalImportDirs="Protos" GrpcServices="Server" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\*" GrpcServices="Server" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.30" />
<PackageReference Include="Grpc.AspNetCore" Version="2.26.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.27.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
......@@ -38,6 +46,7 @@
<ProjectReference Include="..\..\..\src\Pole.Core\Pole.Core.csproj" />
<ProjectReference Include="..\..\..\src\Pole.EventBus.Rabbitmq\Pole.EventBus.Rabbitmq.csproj" />
<ProjectReference Include="..\..\..\src\Pole.EventStorage.PostgreSql\Pole.EventStorage.PostgreSql.csproj" />
<ProjectReference Include="..\..\..\src\Pole.Grpc\Pole.Grpc.csproj" />
<ProjectReference Include="..\..\..\src\Pole.Orleans.Provider.EntityframeworkCore\Pole.Orleans.Provider.EntityframeworkCore.csproj" />
</ItemGroup>
<ItemGroup>
......
using Backet.Api.Grains.Abstraction;
using Backet.Grpc;
using Grpc.Core;
using Orleans;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Backet.Api.GrpcServices
{
public class BacketService : Backet.Grpc.Backet.BacketBase
{
private readonly IClusterClient clusterClient;
public BacketService(IClusterClient clusterClient)
{
this.clusterClient = clusterClient;
}
public override Task<Pole.Grpc.ExtraType.CommonCommandResponse> AddBacket(AddBacketRequest backetDto, ServerCallContext context)
{
var newId = Guid.NewGuid().ToString("N").ToLower();
backetDto.Id = newId;
var grain = clusterClient.GetGrain<IAddBacketGrain>(newId);
return Task.FromResult(Pole.Grpc.ExtraType.CommonCommandResponse.SuccessResponse);
}
}
}
......@@ -37,6 +37,17 @@ namespace Backet.Api
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(option =>
{
option.ListenAnyIP(81, config =>
{
config.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1;
});
option.ListenAnyIP(82, config =>
{
config.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2;
});
});
});
}
}
syntax = "proto3";
package pole.Grpc.ExtraType;
message CommonCommandResponse {
// 1 成功 2 失败
int32 status = 1;
string message = 2;
}
\ No newline at end of file
syntax = "proto3";
option csharp_namespace = "Backet.Grpc";
import "Protos/Common/commonCommandResponse.proto";
package greet;
// The greeting service definition.
service Backet {
// Sends a greeting
rpc AddBacket (AddBacketRequest) returns (pole.Grpc.ExtraType.CommonCommandResponse);
}
// The request message containing the user's name.
message AddBacketRequest {
string name = 1;
string id = 2;
string userId = 3;
repeated AddBacketItemRequest Items = 4;
message AddBacketItemRequest{
string productId = 1;
string productName = 2;
int64 price = 3;
}
}
using Backet.Api.Grains;
using Backet.Api.GrpcServices;
using Backet.Api.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
......@@ -27,6 +28,10 @@ namespace Backet.Api
services.AddDbContextPool<BacketDbContext>(options => options.UseNpgsql(Configuration["postgres:write"]));
services.AddControllers();
services.AddGrpc();
services.AddGrpcValidation();
services.AddGrpcRequestValidator();
services.AddPole(config =>
{
config.AddRabbitMQ(option =>
......@@ -62,6 +67,7 @@ namespace Backet.Api
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapGrpcService<BacketService>();
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
......
using Pole.Application.Cqrs;
using Pole.Domain;
using Pole.Domain.UnitOfWork;
using System;
using System;
using System.Collections.Generic;
using System.Text;
......
......@@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pole.Domain\Pole.Domain.csproj" />
<ProjectReference Include="..\Pole.Core\Pole.Core.csproj" />
</ItemGroup>
</Project>
......@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace Pole.Grpc.Validation.Internal
{
class DefaultValidatorRegistrar : IValidatorRegistrar
class ValidatorRegistrar : IValidatorRegistrar
{
public Task Register(Type validatorType, IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
......
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Pole.Core.Utils;
using Pole.Grpc.Validation;
using Pole.Grpc.Validation.Internal;
using System;
......@@ -16,33 +18,26 @@ namespace Microsoft.Extensions.DependencyInjection
public static IServiceCollection AddGrpcValidation(this IServiceCollection services)
{
services.AddSingleton<IValidatorProvider, DefaultValidatorProvider>();
services.AddSingleton<IValidatorRegistrar, DefaultValidatorRegistrar>();
services.AddSingleton<IValidatorRegistrar, ValidatorRegistrar>();
services.AddSingleton<IValidatorErrorMessageHandler, DefaultValidatorErrorMessageHandler>();
return services;
}
public static IServiceCollection AddGrpcRequestValidator(this IServiceCollection services,Assembly validatorAssembly ,ServiceLifetime lifetime = ServiceLifetime.Singleton)
public static IServiceCollection AddGrpcRequestValidator(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
Action<ValidateOption> validateOptionConfig = validateOption => {
validateOption.ValidatorAssembly = validatorAssembly;
};
services.Configure(validateOptionConfig);
using (var serviceProvider= services.BuildServiceProvider())
using (var serviceProvider = services.BuildServiceProvider())
{
var option = serviceProvider.GetRequiredService<IOptions<ValidateOption>>();
var validatorRegistrar = serviceProvider.GetRequiredService<IValidatorRegistrar>();
var validators = option.Value.ValidatorAssembly.GetTypes().Where(m => m.GetInterfaces().FirstOrDefault(t =>
t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IValidator<>))!=null);
foreach (var validator in validators)
foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService<ILogger<ValidatorRegistrar>>()))
{
validatorRegistrar.Register(validator, services);
var validators = assembly.GetTypes().Where(m => m.GetInterfaces().FirstOrDefault(t =>t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IValidator<>)) != null);
foreach (var validator in validators)
{
validatorRegistrar.Register(validator, services);
}
}
return services;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Pole.Grpc.Validation
{
public class ValidateOption
{
public Assembly ValidatorAssembly { get; set; }
}
}
......@@ -27,23 +27,13 @@ namespace Pole.Samples.Backet.Api
// await uploader.UpdateAsync("\"pole\".\"Events\"", events);
//}
// Queue the task.
for (var i = 0; i < 100; i++)
{
ThreadPool.QueueUserWorkItem(ThreadProc, i);
}
string s = "11111111111111111111";
var bytes= Encoding.ASCII.GetBytes(s);
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(1000);
Console.WriteLine("Main thread exits.");
}
// This thread procedure performs the task.
static void ThreadProc(Object stateInfo)
{
var i = Convert.ToInt32(stateInfo);
// No state object was passed to QueueUserWorkItem, so stateInfo is null.
Console.WriteLine($"Hello from the thread pool.{i}");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment