-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from yileicn/feature/discovery
Feature/discovery
- Loading branch information
Showing
12 changed files
with
142 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/Grpc.Extension.Discovery/Consul/ConsulServiceBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Grpc.Extension.Discovery/Consul/NewAgentCheckRegistration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters