หากคุณมีคำถามใด ๆ โปรดเพิ่ม QQ: 2292709323 โดยตรง WeChat: yaofengv ติดต่อ
Overt.Core.Grpc v1.0.6
ตามไดรเวอร์ Grpc.Core ของ Google เหมาะสำหรับสถานการณ์บริการคอนโซลและใช้สถานการณ์ไมโครเซอร์วิสตามการเชื่อมต่อ TCP แบบยาว
|-Config 配置模型
|
|-Client 客户端功能实现,服务发现
|
|-dllconfigs 配置文件保存
|
|-Intercept 拦截器
|-|-IServerTracer 服务端拦截器接口
|-|-IClientTracer 客户端拦截器接口
|
|-Manager 启动、客户端调用类
|-|-GrpcServiceManager.cs 服务端启动类
|-|-GrpcClientManager.cs 客户端获取Client类
|
|-Service 服务端
|
|-GrpcServiceCollectionExtensions.cs netcore注入
- เวอร์ชันนูเก็ต: v1.0.6
- การสนับสนุนกรอบงาน: Framewok 4.5 - 4.6 / NetStandard 2.0
- เน็ตสแตนดาร์ด 2.0
Consul 0.7 .2 .6
Google . Protobuf 3.8 .0
Grpc 1.21 .0
Microsoft . Extensions . Configuration . Json 2.0 .0
Microsoft . Extensions . Options . ConfigurationExtensions 2.0 .0
- กรอบการทำงาน 4.5-4.7
Consul 0.7 .2 .6
Google . Protobuf 3.8 .0
Grpc 1.21 .0
Install - Package Overt . Core . Grpc - Version 1.0 .6
ลำดับความสำคัญ: {Third Party Configuration Center} > ตัวแปรสภาพแวดล้อม > การกำหนดค่าภายในของโฮสต์ > รับ อินทราเน็ต IP โดยอัตโนมัติ
- รองรับไฟล์การกำหนดค่าเริ่มต้น appsettings.json [ไม่จำเป็นต้องใช้โหนดกงสุล หากไม่จำเป็น ก็ไม่ใช่คลัสเตอร์]
{
"GrpcServer" : {
"Service" : {
"Name" : " OvertGrpcServiceApp " , // 服务名称使用服务名称去除点:OvertGrpcServiceApp
"Host" : " service.g.lan " , // 专用注册的域名 (可选)格式:ip[:port=default]
"HostEnv" : " serviceaddress " , // 获取注册地址的环境变量名字(可选,优先)环境变量值格式:ip[:port=default]
"Port" : 10001 , // 端口自定义
"Consul" : {
"Path" : " dllconfigs/consulsettings.json " // Consul路径
}
}
}
}
// 添加section
< configSections >
< section name = " grpcServer " type = " Overt.Core.Grpc.GrpcServerSection, Overt.Core.Grpc " />
</ configSections >
// 添加节点
< grpcServer >
< service name = " OvertGrpcServiceApp " port = " 10005 " host = "专用注册的域名(可选)格式:ip[:port=default] " hostEnv = "获取注册地址的环境变量名字(可选)环境变量值格式:ip[:port=default] " >
< registry >
< consul path = " dllconfigs/Consul.config " />
</ registry >
</ service >
</ grpcServer >
- การตั้งชื่อ: โฟลเดอร์ [namespace] .dll.json (dllconfigs)
{
"GrpcClient" : {
"Service" : {
"Name" : " grpcservice " , // 服务名称与服务端保持一致
"MaxRetry" : 0 , // 最大可重试次数,默认不重试
"Discovery" : {
"EndPoints" : [ // 单点模式
{
"Host" : " 127.0.0.1 " ,
"Port" : 10001
}
],
"Consul" : { // Consul集群,集群优先原则
"Path" : " dllconfigs/consulsettings.json "
}
}
}
}
}
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< configSections >
< section name = " grpcClient " type = " Overt.Core.Grpc.GrpcClientSection, Overt.Core.Grpc " />
</ configSections >
< grpcClient >
< service name = " " maxRetry = " 0 " >
< discovery >
< server >
< endpoint host = " " port = " " ></ endpoint >
< endpoint host = " " port = " " ></ endpoint >
</ server >
< consul path = " dllconfigs/Consul.config " ></ consul >
</ discovery >
</ service >
</ grpcClient >
</ configuration >
- ชื่อ: consulsettings.json ห้ามเปลี่ยนแปลง
{
"ConsulServer" : {
"Service" : {
"Address" : " http://consul.g.lan " // 默认8500端口
}
}
}
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< configuration >
< configSections >
< section name = " consulServer " type = " Overt.Core.Grpc.ConsulServerSection, Overt.Core.Grpc " />
</ configSections >
< consulServer >
< service address = " http://consul.g.lan " ></ service >
</ consulServer >
</ configuration >
- โหมดการฉีดแบบพึ่งพาแรง
services . AddSingleton < GrpcExampleService . GrpcExampleServiceBase , GrpcExampleServiceImpl > ( ) ; // Grpc服务的实现
services . AddSingleton < IHostedService , GrpcExampleHostedService > ( ) ; // Grpc服务启动服务类:如下
services . AddGrpcTracer < ConsoleTracer > ( ) ; // Grpc注入拦截器,继承IServerTracer(可选)
// 使用第三方配置
services . AddGrpcConfig ( config =>
{
// 以配置中心apollo为例
config . AddApollo ( context . Configuration . GetSection ( "apollo" ) ) . AddDefault ( ) ;
} ) ;
using Microsoft . Extensions . Hosting ;
using Overt . Core . Grpc ;
using Overt . GrpcExample . Service . Grpc ;
using System . Threading ;
using System . Threading . Tasks ;
namespace Overt . GrpcService . App
{
public class GrpcService : IHostedService
{
GrpcExampleService . GrpcExampleServiceBase _grpcServiceBase ;
IServerTracer _tracer ;
public GrpcService ( GrpcExampleService . GrpcExampleServiceBase serviceBase , IServerTracer tracer ) // 依赖注入Grpc服务基础类
{
_serviceBase = serviceBase ;
_tracer = tracer ;
}
public Task StartAsync ( CancellationToken cancellationToken )
{
return Task . Factory . StartNew ( ( ) =>
{
GrpcServiceManager . Start ( GrpcExampleService . BindService ( _serviceBase ) , _tracer ) ;
} , cancellationToken ) ;
}
public Task StopAsync ( CancellationToken cancellationToken )
{
return Task . Factory . StartNew ( ( ) =>
{
GrpcServiceManager . Stop ( ) ;
} , cancellationToken ) ;
}
}
}
- การเขียนชั้นเรียนการนำไปปฏิบัติ
原因:服务启动的时候是一个单例,那么所有服务之下的全部是单实例,而数据层需要使用多实例
// 只注入 IServiceProvider
IServiceProvider _provider ;
public GrpcExampleServiceImpl ( IServiceProvider provider )
{
_provider = provider ;
}
// 其他利用provider即时获取
using ( var scope = _provider . CreateSocpe ( ) )
{
var _userService = scope . ServiceProvider . GetService < IUserService > ( ) ;
}
- โทร GrpcServiceManager โดยตรงเพื่อเริ่มต้น
using Grpc . Core ;
using Overt . Core . Grpc ;
using Overt . Log ;
using System ;
namespace Overt . GrpcService
{
public class MainService
{
public MainService ( )
{
}
public void Start ( string serviceName ) // 启动服务
{
GrpcServiceManager . Start ( Library . GrpcService . BindService ( new GrpcServiceImpl ( ) ) , tracer : new ConsoleTracer ( ) , whenException : ( ex ) =>
{
LogHelper . Info ( "" , ex ) ;
} ) ;
}
public void Stop ( string serviceName ) // 停止服务
{
GrpcServiceManager . Stop ( ) ;
}
public void ShutDown ( string serviceName )
{
GrpcServiceManager . Stop ( ) ;
}
}
}
- โหมดการฉีดแบบพึ่งพาแรง
- ไฟล์การกำหนดค่าใช้ [namespace].dll.json ตามค่าเริ่มต้น แพ็คเกจ Nuget สามารถสร้างขึ้นได้ผ่านเครื่องมือ vs.menu
- โทรหาสิ่งต่อไปนี้โดยตรงระหว่างการฉีด:
// 注入Grpc客户端
services . AddGrpcClient ( ) ;
// 自定义配置文件 / 默认使用命名空间.dll.json
services . Configure < GrpcClientOptions < GrpcExampleServiceClient > > ( ( cfg ) =>
{
cfg . ConfigPath = "dllconfig/Overt.GrpcExample.Service.Grpc.dll.json" ; // 可不传递
} ) ;
// 使用第三方配置
services . AddGrpcConfig ( config =>
{
// 以配置中心apollo为例
config . AddApollo ( context . Configuration . GetSection ( "apollo" ) ) . AddDefault ( ) ;
} ) ;
// 获取注入的对象
IGrpcClient < GrpcExampleServiceClient > _grpcClient ;
public IndexModel ( IGrpcClient < GrpcExampleServiceClient > grpcClient )
{
_grpcClient = grpcClient ;
}
var res = _grpcClient . Client . Ask ( new Service . Grpc . AskRequest ( ) { Key = "abc" } ) ;
- คลาสพร็อกซีไคลเอ็นต์ที่คอมไพล์ใน Dll ซอร์สโค้ดมีดังนี้ สามารถละเว้นได้
using Grpc . Core ;
using Overt . Core . Grpc ;
using Overt . Core . Grpc . Intercept ;
using System ;
using System . Collections . Concurrent ;
using System . IO ;
using __GrpcService = Overt . GrpcExample . Service . Grpc . GrpcExampleService ;
namespace Overt . GrpcExample . Service . Grpc
{
#if NET45 || NET46 || NET47
public class ClientManager
{
public static IClientTracer Tracer { get ; set ; } = default ( IClientTracer ) ;
private static string DefaultConfigPath { get ; set ; } = "dllconfigs/Overt.GrpcExample.Service.Grpc.dll.config" ;
public static __GrpcService . GrpcExampleServiceClient Instance
{
get
{
return ClientManager < __GrpcService . GrpcExampleServiceClient > . Instance ;
}
}
private static readonly ConcurrentDictionary < Type , string > configMap = new ConcurrentDictionary < Type , string > ( ) ;
public static void Configure < T > ( string configPath ) { configMap . AddOrUpdate ( typeof ( T ) , configPath , ( t , s ) => configPath ) ; }
public static string GetConfigure < T > ( ) { if ( configMap . TryGetValue ( typeof ( T ) , out string configPath ) ) return configPath ; return DefaultConfigPath ; }
}
public class ClientManager < T > : ClientManager where T : ClientBase
{
public static new T Instance
{
get
{
var configPath = GetConfigure < T > ( ) ;
var abConfigPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , configPath ) ;
return GrpcClientManager < T > . Get ( abConfigPath , Tracer ) ;
}
}
}
#endif
}
- ดำเนินการโดยใช้คลาสพร็อกซี
// 自定义配置文件 / 默认使用命名空间.dll.json / 在主进程入口进行配置T为服务Client
ClientManger.Configure<T>("dllconfig/abc.config");
ClientManager.Instance.[Method]
// T为服务Client
ClientManager<T>.Instance.[Method]
- กงสุลสนับสนุนโทเค็น
- อัปเกรดไดรเวอร์พื้นฐานเป็นเวอร์ชันล่าสุด
- Grpc => 2.40.0
- อัปเกรดไดรเวอร์พื้นฐานเป็นเวอร์ชันล่าสุด
- Google.Protobuf => 3.17.3
- รีแฟคเตอร์ GrpcClientOptions
- อัปเกรดไดรเวอร์เป็นเวอร์ชันล่าสุด
- Tracer ให้ความสามารถในการแก้ไข Channel Target ช่วยให้สามารถเลือกโหนดภายนอกได้เอง
- อัปเกรดไดรเวอร์พื้นฐานเป็นเวอร์ชันล่าสุด
- เพิ่มการฉีด Interceptor แบบกำหนดเอง
- ปรับการกำหนดค่าพารามิเตอร์ของ ServiceStart และใช้ GrpcOptions เพื่อดำเนินการ
- เพิ่มการได้มาแบบกำหนดเองของผู้เรียกใช้ และเพิ่มการได้มาของ ServiceId
- สนับสนุนกลยุทธ์การรับผู้เรียกใช้แบบกำหนดเองสำหรับเวอร์ชันเฟรมเวิร์ก
- เพิ่มการรองรับการกำหนดค่าของบุคคลที่สาม เช่น Apollo ซึ่งสามารถขยายได้โดยใช้บริการ AddGrpcConfig (ปัจจุบันรองรับเฉพาะ dotnetcore เท่านั้น)
- ปรับการสนับสนุนให้เหมาะสมสำหรับกงสุลเวอร์ชันใหม่
- หลังจากที่ศูนย์ลงทะเบียนตรวจพบการเปลี่ยนแปลงแล้ว ศูนย์จะเพิกเฉยต่อบัญชีดำและรีเซ็ตการเชื่อมต่อภายในทั้งหมด
- ลูกค้าใช้ศูนย์การลงทะเบียนกงสุลเพื่อรองรับการตรวจสอบการเปลี่ยนแปลงบริการเดียว บริการลงทะเบียนใหม่หรือความล้มเหลวของบริการแบบเรียลไทม์มากขึ้น
- รองรับโหมดบริการที่หลากหลาย
- แก้ไขเนมสเปซ อัปเดตแพ็คเกจ nuget เป็น Overt.Core.Grpc และอัปเดตเวอร์ชันเริ่มต้นเป็น 1.0.0
- เมื่อไคลเอนต์ล้มเหลวในการปรับบริการการเชื่อมต่อให้เหมาะสม จะถูกขึ้นบัญชีดำ ส่งผลให้เกิดปัญหาที่ไม่มีโหนดอยู่
Overt.Core.Grpc.H2 v2.0.1
ใช้สำหรับเวอร์ชัน >= netcoreapp3.0 ใช้สำหรับโปรโตคอล http2 อาศัย grpc.net ของ Microsoft เหมาะสำหรับสถานการณ์บริการเว็บ การใช้สถานการณ์จำลองไมโครเซอร์วิสตาม http2
|-Config 配置模型
|
|-Client 客户端功能实现,服务发现
|
|-dllconfigs 配置文件保存
|
|-Service 服务端
|
|-GrpcServiceCollectionExtensions.cs netcore注入
- เวอร์ชัน Nuget: V 2.0.1
- การสนับสนุนกรอบงาน: netstandard2.1/netcoreapp3.0/net5.0/net6.0
Consul 1.6 .10 .7
Google . Protobuf 3.21 .5
Grpc . Net . Client 2.47 .0
Microsoft . Extensions . Configuration . Json 5.0 .0
Microsoft . Extensions . Options . ConfigurationExtensions 5.0 .0
Install - Package Overt . Core . Grpc . H2 - Version 2.1 .0
ลำดับความสำคัญ: {Third Party Configuration Center} > ตัวแปรสภาพแวดล้อม > การกำหนดค่าภายในโฮสต์ > รับ IP + เริ่มต้น อินทราเน็ต พอร์ตโดยอัตโนมัติ
- รองรับไฟล์การกำหนดค่าเริ่มต้น appsettings.json [ไม่จำเป็นต้องใช้โหนดกงสุล หากไม่จำเป็น ก็ไม่ใช่คลัสเตอร์]
{
"GrpcServer" : {
"Service" : {
"Name" : " OvertGrpcServiceApp " , // 服务名称使用服务名称去除点:OvertGrpcServiceApp
"Host" : " service.g.lan " , // 专用注册的域名 (可选)格式:ip[:port=default]
"HostEnv" : " serviceaddress " , // 获取注册地址的环境变量名字(可选,优先)环境变量值格式:ip[:port=default]
"Port" : 10001 , // 端口自定义
"Consul" : {
"Path" : " dllconfigs/consulsettings.json " // Consul路径
}
}
}
}
- การตั้งชื่อ: โฟลเดอร์ [namespace] .dll.json (dllconfigs)
{
"GrpcClient" : {
"Service" : {
"Name" : " grpcservice " , // 服务名称与服务端保持一致
"Scheme" : " http " , // foraddress 协议 http/https
"Discovery" : {
"EndPoints" : [ // 单点模式
{
"Host" : " 127.0.0.1 " ,
"Port" : 10001
}
],
"Consul" : { // Consul集群,集群优先原则
"Path" : " dllconfigs/consulsettings.json "
}
}
}
}
}
- ชื่อ: consulsettings.json ห้ามเปลี่ยนแปลง
{
"ConsulServer" : {
"Service" : {
"Address" : " http://consul.g.lan " // 默认8500端口
}
}
}
- ลงทะเบียนกงสุล
public void ConfigureServices ( IServiceCollection services )
{
.. .
// 其他
.. .
services . AddGrpcService ( ) ; // 应用关闭时,节点可自动移除
// 按需使用第三方配置
services . AddGrpcConfig ( config =>
{
// 以配置中心apollo为例
config . AddApollo ( context . Configuration . GetSection ( "apollo" ) ) . AddDefault ( ) ;
} ) ;
}
public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
{
.. .
// 其他
.. .
app . UseGrpcRegister ( ) ;
}
- โหมดการฉีดแบบพึ่งพาแรง
- ไฟล์การกำหนดค่าใช้ [namespace].dll.json ตามค่าเริ่มต้น แพ็คเกจ Nuget สามารถสร้างขึ้นได้ผ่านเครื่องมือ vs.menu
- โทรหาสิ่งต่อไปนี้โดยตรงระหว่างการฉีด:
// 注入Grpc客户端
services . AddGrpcClient ( ) ;
// 自定义配置文件 / 默认使用命名空间.dll.json
services . Configure < GrpcClientOptions < GrpcExampleServiceClient > > ( ( cfg ) =>
{
cfg . ConfigPath = "dllconfig/Overt.GrpcExample.Service.Grpc.dll.json" ; // 可不传递
} ) ;
// 使用第三方配置
services . AddGrpcConfig ( config =>
{
// 以配置中心apollo为例
config . AddApollo ( context . Configuration . GetSection ( "apollo" ) ) . AddDefault ( ) ;
} ) ;
// 获取注入的对象
IGrpcClient < GrpcExampleServiceClient > _grpcClient ;
public IndexModel ( IGrpcClient < GrpcExampleServiceClient > grpcClient )
{
_grpcClient = grpcClient ;
}
var res = _grpcClient . Client . Ask ( new Service . Grpc . AskRequest ( ) { Key = "abc" } ) ;
1.NET5_0_OR_GREATER รองรับช่องสัญญาณย่อย Resolver และ LoadBalancer และการปรับแต่ง
- รองรับศูนย์การกำหนดค่าภายนอก เช่น Apollo
- รองรับการกำหนดค่าไฟล์การเปลี่ยนแปลงรหัส
- เพิ่ม HostedService
- เวอร์ชันแรกใช้ไดรเวอร์ grpc.net ของ Microsoft และปรับให้เข้ากับ net5.0