Skip to content

Commit

Permalink
Client和Server添加ChannelOption参数
Browse files Browse the repository at this point in the history
  • Loading branch information
yilei committed Aug 19, 2019
1 parent 0df9c08 commit 0be560b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Grpc.Extension/Internal/AutoChannelCallInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ namespace Grpc.Extension.Internal
/// </summary>
internal class AutoChannelCallInvoker : CallInvoker
{
private ChannelManager _channelManager;
private ChannelPool _channelManager;

/// <summary>
/// 自动负载Channel的CallInvoker
/// </summary>
public AutoChannelCallInvoker(ChannelManager channelManager)
public AutoChannelCallInvoker(ChannelPool channelManager)
{
this._channelManager = channelManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Grpc.Extension.Internal
/// <summary>
/// Channel统一管理
/// </summary>
internal class ChannelManager
internal class ChannelPool
{
private ConcurrentDictionary<string, ChannelInfo> _channels = new ConcurrentDictionary<string, ChannelInfo>();
private IServiceDiscovery _serviceDiscovery;
Expand All @@ -25,7 +25,7 @@ internal class ChannelManager
/// </summary>
/// <param name="serviceDiscovery"></param>
/// <param name="loadBalancer"></param>
public ChannelManager(IServiceDiscovery serviceDiscovery, ILoadBalancer loadBalancer,IMemoryCache memoryCache)
public ChannelPool(IServiceDiscovery serviceDiscovery, ILoadBalancer loadBalancer,IMemoryCache memoryCache)
{
this._serviceDiscovery = serviceDiscovery;
this._loadBalancer = loadBalancer;
Expand Down Expand Up @@ -99,7 +99,7 @@ private Channel GetChannelCore(string endpoint,ChannelConfig config)

private ChannelInfo CreateChannel(string endPoint, ChannelConfig config)
{
var channel = new Channel(endPoint, ChannelCredentials.Insecure);
var channel = new Channel(endPoint, ChannelCredentials.Insecure, config.ChannelOptions);

var tryCount = 0;//重试计数
//检查channel状态
Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Extension/Internal/GrpcClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public GrpcClientManager(IServiceProvider serviceProvider, IEnumerable<ClientInt
/// <returns></returns>
public T GetGrpcClient<T>() where T : ClientBase<T>
{
var channelManager = GrpcExtensions.ServiceProvider.GetService<ChannelManager>();
var channelManager = GrpcExtensions.ServiceProvider.GetService<ChannelPool>();
var bindFlags = BindingFlags.Static | BindingFlags.NonPublic;
var grpcServiceName = typeof(T).DeclaringType.GetFieldValue<string>("__ServiceName", bindFlags);

Expand Down
2 changes: 1 addition & 1 deletion src/Grpc.Extension/Internal/ServerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private void CheckUseJaeger()
/// <returns></returns>
public Server Build()
{
Server server = new Server();
Server server = new Server(GrpcServerOptions.Instance.ChannelOptions);
//使用拦截器
var serviceDefinitions = ApplyInterceptor(_serviceDefinitions, _interceptors);
//添加服务定义
Expand Down
25 changes: 23 additions & 2 deletions src/Grpc.Extension/Model/ChannelConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Grpc.Core;
using System.Collections.Generic;

namespace Grpc.Extension.Model
{
Expand All @@ -7,14 +8,34 @@ namespace Grpc.Extension.Model
/// </summary>
internal class ChannelConfig
{
/// <summary>
/// Discovery的服务器地址
/// </summary>
public string DiscoveryUrl { get; set; }

/// <summary>
/// Discovery上客户端服务名字
/// </summary>
public string DiscoveryServiceName { get; set; }

/// <summary>
/// 直接服务地址,不用服务现
/// </summary>
public string DirectEndpoint { get; set; }

/// <summary>
/// 是否使用直接服务地址
/// </summary>
public bool UseDirect { get; set; }

public string GrpcServiceName { get; set; }
/// <summary>
/// ChannelOption
/// </summary>
public IEnumerable<ChannelOption> ChannelOptions { get; set; }

/// <summary>
/// GrpcServiceName
/// </summary>
internal string GrpcServiceName { get; set; }
}
}
8 changes: 7 additions & 1 deletion src/Grpc.Extension/Options/GrpcServerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Grpc.Core;
using System;
using System.Collections.Generic;
using System.Text;

Expand Down Expand Up @@ -45,6 +46,11 @@ internal static GrpcServerOptions Instance
/// </summary>
public int DefaultErrorCode { get; set; } = 1;

/// <summary>
/// ChannelOption
/// </summary>
public IEnumerable<ChannelOption> ChannelOptions { get; set; }

#region 兼容老版本
/*
/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions src/Grpc.Extension/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static IServiceCollection AddGrpcClientExtensions(this IServiceCollection
services.AddSingleton<AutoChannelCallInvoker>();
services.AddSingleton<CallInvoker, InterceptorCallInvoker>();
//添加Channel的Manager
services.AddSingleton<ChannelManager>();
services.AddSingleton<ChannelPool>();
services.AddSingleton<GrpcClientManager>();

//默认使用轮询负载策略,在外面可以注入其它策略
Expand Down Expand Up @@ -88,20 +88,22 @@ public static IServiceCollection AddGrpcClientExtensions(this IServiceCollection
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="services"></param>
/// <param name="discoveryUrl"></param>
/// <param name="discoveryServiceName"></param>
/// <param name="discoveryUrl">Discovery的服务器地址</param>
/// <param name="discoveryServiceName">Discovery上客户端服务名字</param>
/// <param name="channelOptions">ChannelOption</param>
/// <returns></returns>
public static IServiceCollection AddGrpcClient<T>(this IServiceCollection services, string discoveryUrl, string discoveryServiceName) where T : ClientBase<T>
public static IServiceCollection AddGrpcClient<T>(this IServiceCollection services, string discoveryUrl, string discoveryServiceName, IEnumerable<ChannelOption> channelOptions = null) where T : ClientBase<T>
{
services.AddSingleton<T>();
var channelConfig = new ChannelConfig
{
DiscoveryUrl = discoveryUrl,
DiscoveryServiceName = discoveryServiceName
DiscoveryServiceName = discoveryServiceName,
ChannelOptions = channelOptions
};
var bindFlags = BindingFlags.Static | BindingFlags.NonPublic;
channelConfig.GrpcServiceName = typeof(T).DeclaringType.GetFieldValue<string>("__ServiceName", bindFlags);
ChannelManager.Configs.Add(channelConfig);
ChannelPool.Configs.Add(channelConfig);
return services;
}

Expand Down

0 comments on commit 0be560b

Please sign in to comment.