Skip to content

Commit

Permalink
Merge pull request #10 from yileicn/feature/discovery
Browse files Browse the repository at this point in the history
Feature/discovery
  • Loading branch information
yileicn authored Dec 23, 2020
2 parents c938fdd + d10cf4b commit 5537737
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 35 deletions.
3 changes: 3 additions & 0 deletions examples/CodeFirst/MathServer/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grpc.Extension;
using Grpc.Extension.Discovery;
using Math;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -19,6 +20,8 @@ public void ConfigureServices(IServiceCollection services)
//grpc
services.AddGrpcExtensions<MathGrpc>(_conf); //注入GrpcExtensions
services.AddHostedService<GrpcHostServiceV2>();
//使用TCPCheck,默认使用TTLCheck
services.AddConsulDiscovery(p => p.UseTCPCheck());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>1.4.7</Version>
<Version>1.4.8</Version>
<PackageId>FM.Grpc.Extensions.AspNetCore</PackageId>
<Authors>RabbitYi</Authors>
<Description>一个基于GRPC的简单微服务框架
Expand Down
6 changes: 3 additions & 3 deletions src/Grpc.Extension.Client/Grpc.Extension.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
5.DashBoard(远程调用,手动熔断,日志输出控制)
6.Grpc CodeFirst</Description>
<Company />
<Version>1.4.8-alpha3</Version>
<Version>1.4.8</Version>
<RepositoryType>git</RepositoryType>
<PackageTags>grpc,dashboard,consul,micorservice,opentracing,polly</PackageTags>
<RepositoryUrl>https://github.com/yileicn/Grpc.Extensions/</RepositoryUrl>
<AssemblyVersion>1.4.7.1</AssemblyVersion>
<FileVersion>1.4.7.1</FileVersion>
<AssemblyVersion>1.4.8.0</AssemblyVersion>
<FileVersion>1.4.8.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
9 changes: 9 additions & 0 deletions src/Grpc.Extension.Discovery/Consul/Checks/IConsulCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Consul;

namespace Grpc.Extension.Discovery.Consul.Checks
{
public interface IConsulCheck
{
AgentCheckRegistration GetCheck(AgentServiceRegistration registration);
}
}
24 changes: 24 additions & 0 deletions src/Grpc.Extension.Discovery/Consul/Checks/TCPCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Consul;
using System;
using System.Collections.Generic;
using System.Text;

namespace Grpc.Extension.Discovery.Consul.Checks
{
public class TCPCheck : IConsulCheck
{
public AgentCheckRegistration GetCheck(AgentServiceRegistration registration)
{
var check = new AgentCheckRegistration
{
Name = "tcpcheck",
TCP = $"{registration.Address}:{registration.Port}",
Interval = TimeSpan.FromSeconds(15),
Timeout = TimeSpan.FromSeconds(3),
Status = HealthStatus.Passing,
DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1)
};
return check;
}
}
}
24 changes: 24 additions & 0 deletions src/Grpc.Extension.Discovery/Consul/Checks/TTLCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Consul;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Grpc.Extension.Discovery.Consul.Checks
{
public class TTLCheck : IConsulCheck
{
public AgentCheckRegistration GetCheck(AgentServiceRegistration registration)
{
var check = new NewAgentCheckRegistration
{
CheckID = $"service:{registration.ID}",//consul 1.3使用ID,consul 1.7使用CheckID
Name = $"ttlcheck",
TTL = TimeSpan.FromSeconds(15),
Status = HealthStatus.Passing,
DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
};
return check;
}
}
}
32 changes: 32 additions & 0 deletions src/Grpc.Extension.Discovery/Consul/ConsulServiceBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Grpc.Extension.Discovery.Consul.Checks;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Grpc.Extension.Discovery.Consul
{
public class ConsulServiceBuilder
{
private readonly IServiceCollection services;

public ConsulServiceBuilder(IServiceCollection services)
{
this.services = services;
}

public ConsulServiceBuilder UseTTLCheck()
{
services.AddSingleton<IConsulCheck, TTLCheck>();

return this;
}

public ConsulServiceBuilder UseTCPCheck()
{
services.AddSingleton<IConsulCheck, TCPCheck>();

return this;
}
}
}
32 changes: 11 additions & 21 deletions src/Grpc.Extension.Discovery/Consul/ConsulServiceRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Grpc.Extension.Abstract;
using Grpc.Extension.Abstract.Discovery;
using Grpc.Extension.Abstract.Model;
using Grpc.Extension.Discovery.Consul.Checks;
using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,13 +18,15 @@ public class ConsulServiceRegister : IServiceRegister
private string _guid;
private ConsulClient _client;
private ServiceRegisterModel _model;
private IConsulCheck _check;

/// <summary>
/// Consul服务注册
/// </summary>
public ConsulServiceRegister()
public ConsulServiceRegister(IConsulCheck check)
{
this._guid = Guid.NewGuid().ToString();
this._check = check;
}

/// <summary>
Expand All @@ -37,9 +40,6 @@ public async Task RegisterService(ServiceRegisterModel model)
this._client = CreateConsulClient();

await RegisterServiceCore();

//因为公司的consul不支持consul主动检查服务状态,所以启动定时器主动去检测
_timerTTL = new Timer(async state => await DoTTL(), null, 0, Timeout.Infinite);
}

private async Task RegisterServiceCore()
Expand All @@ -52,25 +52,15 @@ private async Task RegisterServiceCore()
EnableTagOverride = true,
Address = _model.ServiceIp,
Port = _model.ServicePort,
//因为公司的consul不支持consul主动检查服务状态,所以注释掉
//Check = new AgentServiceCheck
//{
// TCP = $"{_model.ServiceIp}:{_model.ServicePort}",
// Interval = TimeSpan.FromSeconds(15),
// Status = HealthStatus.Passing,
// DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1)
//}
//因为公司的consul不支持consul主动检查服务状态,所以主动去TTL consul
Check = new AgentCheckRegistration
{
ID = GetTTLCheckId(),
Name = "ttlcheck",
TTL = TimeSpan.FromSeconds(15),
Status = HealthStatus.Passing,
DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
}
};
var check = _check.GetCheck(registration);
registration.Check = check;
await _client.Agent.ServiceRegister(registration);

if (check.Name.StartsWith("ttlcheck"))
{
_timerTTL = new Timer(async state => await DoTTL(), null, 0, Timeout.Infinite);
}
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Grpc.Extension.Discovery/Consul/NewAgentCheckRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Consul;
using System;
using System.Collections.Generic;
using System.Text;

namespace Grpc.Extension.Discovery.Consul
{
/// <summary>
/// consul 1.3版本使用ID正常
/// consul 1.7版本使用ID报错,Request decode failed: json: unknown field "ID",使用CheckID正常
/// </summary>
public class NewAgentCheckRegistration : AgentCheckRegistration
{
public string CheckID { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Grpc.Extension.Discovery/Grpc.Extension.Discovery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.4.8-alpha3</Version>
<Version>1.4.8</Version>
<PackageId>FM.Grpc.Extensions.Discovery</PackageId>
<Authors>RabbitYi</Authors>
<Description>默认使用Consul实现服务注册和服务发现</Description>
Expand All @@ -11,8 +11,8 @@
<RepositoryType>git</RepositoryType>
<PackageTags>grpc,dashboard,consul,micorservice,opentracing,polly</PackageTags>
<Product>Grpc.Extension.Discovery</Product>
<AssemblyVersion>1.4.7.1</AssemblyVersion>
<FileVersion>1.4.7.1</FileVersion>
<AssemblyVersion>1.4.8.0</AssemblyVersion>
<FileVersion>1.4.8.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 13 additions & 4 deletions src/Grpc.Extension.Discovery/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Grpc.Extension.Abstract.Discovery;
using Grpc.Extension.Discovery.Consul;
using Grpc.Extension.Discovery.Consul.Checks;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;

namespace Grpc.Extension.Discovery
{
Expand All @@ -14,11 +17,17 @@ public static class ServiceCollectionExtensions
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddConsulDiscovery(this IServiceCollection services)
public static IServiceCollection AddConsulDiscovery(this IServiceCollection services, Action<ConsulServiceBuilder> builder = null)
{
services.AddSingleton<IServiceRegister, ConsulServiceRegister>();
services.AddSingleton<IServiceDiscovery, ConsulServiceDiscovery>();

if (!services.Any(p => p.ImplementationType == typeof(ConsulServiceRegister)))
{
services.AddSingleton<IServiceRegister, ConsulServiceRegister>();
services.AddSingleton<IServiceDiscovery, ConsulServiceDiscovery>();
services.AddSingleton<IConsulCheck, TTLCheck>();
}
var obj = new ConsulServiceBuilder(services);
builder?.Invoke(obj);

return services;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Grpc.Extension/Grpc.Extension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.4.8-alpha3</Version>
<Version>1.4.8</Version>
<Description>一个基于GRPC的简单微服务框架
1.服务注册和发现
2.服务自动负载均衡
Expand All @@ -16,8 +16,8 @@
<Product>Grpc.Extensions</Product>
<Company></Company>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<AssemblyVersion>1.4.7.1</AssemblyVersion>
<FileVersion>1.4.7.1</FileVersion>
<AssemblyVersion>1.4.8.0</AssemblyVersion>
<FileVersion>1.4.8.0</FileVersion>
<RepositoryType>git</RepositoryType>
<PackageTags>grpc,dashboard,consul,micorservice,opentracing,polly</PackageTags>
<PackageProjectUrl>https://github.com/yileicn/Grpc.Extensions/</PackageProjectUrl>
Expand Down

0 comments on commit 5537737

Please sign in to comment.