Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
丁松杰
/
Pole
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
0405a997
authored
Feb 28, 2020
by
dingsongjie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善 grpc demo
parent
51b2b230
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
109 additions
and
48 deletions
samples/apis/Backet.Api/Backet.Api.csproj
samples/apis/Backet.Api/GrpcServices/BacketService.cs
samples/apis/Backet.Api/Program.cs
samples/apis/Backet.Api/Protos/Common/commonCommandResponse.proto
samples/apis/Backet.Api/Protos/backet.proto
samples/apis/Backet.Api/Startup.cs
src/Pole.Grpc/ExtraType/CommonCommandResponse.cs
src/Pole.Grpc/Pole.Grpc.csproj
src/Pole.Grpc/Validation/Internal/DefaultValidatorRegistrar.cs → src/Pole.Grpc/Validation/Internal/ValidatorRegistrar.cs
src/Pole.Grpc/Validation/ServiceCollectionExtensions.cs
src/Pole.Grpc/Validation/ValidateOption.cs
test/Pole.Samples.Backet.Api/Program.cs
samples/apis/Backet.Api/Backet.Api.csproj
View file @
0405a997
...
...
@@ -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.2
6
.0" />
<PackageReference Include="Grpc.AspNetCore" Version="2.2
7
.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>
...
...
samples/apis/Backet.Api/GrpcServices/BacketService.cs
0 → 100644
View file @
0405a997
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
);
}
}
}
samples/apis/Backet.Api/Program.cs
View file @
0405a997
...
...
@@ -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
;
});
});
});
}
}
samples/apis/Backet.Api/Protos/Common/commonCommandResponse.proto
0 → 100644
View file @
0405a997
syntax
=
"proto3"
;
package
pole
.
Grpc.ExtraType
;
message
CommonCommandResponse
{
// 1 成功 2 失败
int32
status
=
1
;
string
message
=
2
;
}
\ No newline at end of file
samples/apis/Backet.Api/Protos/backet.proto
0 → 100644
View file @
0405a997
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
;
}
}
samples/apis/Backet.Api/Startup.cs
View file @
0405a997
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!"
);
...
...
src/Pole.Grpc/ExtraType/CommonCommandResponse.cs
View file @
0405a997
using
Pole.Application.Cqrs
;
using
Pole.Domain
;
using
Pole.Domain.UnitOfWork
;
using
System
;
using
System
;
using
System.Collections.Generic
;
using
System.Text
;
...
...
src/Pole.Grpc/Pole.Grpc.csproj
View file @
0405a997
...
...
@@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Pole.
Domain\Pole.Domain
.csproj" />
<ProjectReference Include="..\Pole.
Core\Pole.Core
.csproj" />
</ItemGroup>
</Project>
src/Pole.Grpc/Validation/Internal/
Default
ValidatorRegistrar.cs
→
src/Pole.Grpc/Validation/Internal/ValidatorRegistrar.cs
View file @
0405a997
...
...
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace
Pole.Grpc.Validation.Internal
{
class
Default
ValidatorRegistrar
:
IValidatorRegistrar
class
ValidatorRegistrar
:
IValidatorRegistrar
{
public
Task
Register
(
Type
validatorType
,
IServiceCollection
services
,
ServiceLifetime
lifetime
=
ServiceLifetime
.
Singleton
)
{
...
...
src/Pole.Grpc/Validation/ServiceCollectionExtensions.cs
View file @
0405a997
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
,
Default
ValidatorRegistrar
>();
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
;
}
}
}
}
}
src/Pole.Grpc/Validation/ValidateOption.cs
deleted
100644 → 0
View file @
51b2b230
using
System
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
System.Text
;
namespace
Pole.Grpc.Validation
{
public
class
ValidateOption
{
public
Assembly
ValidatorAssembly
{
get
;
set
;
}
}
}
test/Pole.Samples.Backet.Api/Program.cs
View file @
0405a997
...
...
@@ -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
}
"
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment