C# HttpClient 如何使用 Consul 发现服务
作者:zhouandke 发布时间:2021-09-28 01:06:18
试用了Overt.Core.Grpc, 把 GRPC 的使用改造得像 WCF, 性能测试也非常不错, 非常推荐各位使用.
但已有项目大多是 http 请求, 改造成 GRPC 的话, 工作量比较大, 于是又找到了 Steeltoe.Discovery, 在 Startup 给 HttpClient 添加 DelegatingHandler, 动态改变请求url中的 host 和 port, 将http请求指向consul 发现的服务实例, 这样就实现了服务的动态发现.
经过性能测试, Steeltoe.Discovery 只有 Overt.Core.Grpc 的20%, 非常难以接受, 于是自己实现了一套基于 consul 的服务发现工具. 嗯, 名字好难取啊, 暂定为 ConsulDiscovery.HttpClient 吧
功能很简单:
webapi 从json中读取配置信息 ConsulDiscoveryOptions;
如果自己是一个服务, 则将自己注册到consul中并设置健康检查Url;
ConsulDiscovery.HttpClient 内有一个consul client 定时刷新所有服务的url访问地址.
比较核心的两个类
using Consul;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace ConsulDiscovery.HttpClient
{
public class DiscoveryClient : IDisposable
{
private readonly ConsulDiscoveryOptions consulDiscoveryOptions;
private readonly Timer timer;
private readonly ConsulClient consulClient;
private readonly string serviceIdInConsul;
public Dictionary<string, List<string>> AllServices { get; private set; } = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
public DiscoveryClient(IOptions<ConsulDiscoveryOptions> options)
{
consulDiscoveryOptions = options.Value;
consulClient = new ConsulClient(x => x.Address = new Uri($"http://{consulDiscoveryOptions.ConsulServerSetting.IP}:{consulDiscoveryOptions.ConsulServerSetting.Port}"));
timer = new Timer(Refresh);
if (consulDiscoveryOptions.ServiceRegisterSetting != null)
{
serviceIdInConsul = Guid.NewGuid().ToString();
}
}
public void Start()
{
var checkErrorMsg = CheckParams();
if (checkErrorMsg != null)
{
throw new ArgumentException(checkErrorMsg);
}
RegisterToConsul();
timer.Change(0, consulDiscoveryOptions.ConsulServerSetting.RefreshIntervalInMilliseconds);
}
public void Stop()
{
Dispose();
}
private string CheckParams()
{
if (string.IsNullOrWhiteSpace(consulDiscoveryOptions.ConsulServerSetting.IP))
{
return "Consul服务器地址 ConsulDiscoveryOptions.ConsulServerSetting.IP 不能为空";
}
if (consulDiscoveryOptions.ServiceRegisterSetting != null)
{
var registerSetting = consulDiscoveryOptions.ServiceRegisterSetting;
if (string.IsNullOrWhiteSpace(registerSetting.ServiceName))
{
return "服务名称 ConsulDiscoveryOptions.ServiceRegisterSetting.ServiceName 不能为空";
}
if (string.IsNullOrWhiteSpace(registerSetting.ServiceIP))
{
return "服务地址 ConsulDiscoveryOptions.ServiceRegisterSetting.ServiceIP 不能为空";
}
}
return null;
}
private void RegisterToConsul()
{
if (string.IsNullOrEmpty(serviceIdInConsul))
{
return;
}
var registerSetting = consulDiscoveryOptions.ServiceRegisterSetting;
var httpCheck = new AgentServiceCheck()
{
HTTP = $"{registerSetting.ServiceScheme}{Uri.SchemeDelimiter}{registerSetting.ServiceIP}:{registerSetting.ServicePort}/{registerSetting.HealthCheckRelativeUrl.TrimStart('/')}",
Interval = TimeSpan.FromMilliseconds(registerSetting.HealthCheckIntervalInMilliseconds),
Timeout = TimeSpan.FromMilliseconds(registerSetting.HealthCheckTimeOutInMilliseconds),
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(10),
};
var registration = new AgentServiceRegistration()
{
ID = serviceIdInConsul,
Name = registerSetting.ServiceName,
Address = registerSetting.ServiceIP,
Port = registerSetting.ServicePort,
Check = httpCheck,
Meta = new Dictionary<string, string>() { ["scheme"] = registerSetting.ServiceScheme },
};
consulClient.Agent.ServiceRegister(registration).Wait();
}
private void DeregisterFromConsul()
{
if (string.IsNullOrEmpty(serviceIdInConsul))
{
return;
}
try
{
consulClient.Agent.ServiceDeregister(serviceIdInConsul).Wait();
}
catch
{ }
}
private void Refresh(object state)
{
Dictionary<string, AgentService>.ValueCollection serversInConsul;
try
{
serversInConsul = consulClient.Agent.Services().Result.Response.Values;
}
catch // (Exception ex)
{
// 如果连接consul出错, 则不更新服务列表. 继续使用以前获取到的服务列表
// 但是如果很长时间都不能连接consul, 服务列表里的一些实例已经不可用了, 还一直提供这样旧的列表也不合理, 所以要不要在这里实现 健康检查? 这样的话, 就得把检查地址变成不能设置的
return;
}
// 1. 更新服务列表
// 2. 如果这个程序提供了服务, 还要检测 服务Id 是否在服务列表里
var tempServices = new Dictionary<string, HashSet<string>>();
bool needReregisterToConsul = true;
foreach (var service in serversInConsul)
{
var serviceName = service.Service;
if (!service.Meta.TryGetValue("scheme", out var serviceScheme))
{
serviceScheme = Uri.UriSchemeHttp;
}
var serviceHost = $"{serviceScheme}{Uri.SchemeDelimiter}{service.Address}:{service.Port}";
if (!tempServices.TryGetValue(serviceName, out var serviceHosts))
{
serviceHosts = new HashSet<string>();
tempServices[serviceName] = serviceHosts;
}
serviceHosts.Add(serviceHost);
if (needReregisterToConsul && !string.IsNullOrEmpty(serviceIdInConsul) && serviceIdInConsul == service.ID)
{
needReregisterToConsul = false;
}
}
if (needReregisterToConsul)
{
RegisterToConsul();
}
var tempAllServices = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
foreach (var item in tempServices)
{
tempAllServices[item.Key] = item.Value.ToList();
}
AllServices = tempAllServices;
}
public void Dispose()
{
DeregisterFromConsul();
consulClient.Dispose();
timer.Dispose();
}
}
}
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace ConsulDiscovery.HttpClient
{
public class DiscoveryHttpMessageHandler : DelegatingHandler
{
private static readonly Random random = new Random((int)DateTime.Now.Ticks);
private readonly DiscoveryClient discoveryClient;
public DiscoveryHttpMessageHandler(DiscoveryClient discoveryClient)
{
this.discoveryClient = discoveryClient;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (discoveryClient.AllServices.TryGetValue(request.RequestUri.Host, out var serviceHosts))
{
if (serviceHosts.Count > 0)
{
var index = random.Next(serviceHosts.Count);
request.RequestUri = new Uri(new Uri(serviceHosts[index]), request.RequestUri.PathAndQuery);
}
}
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}
}
使用方法
为了简单, 我为新建的WebApi 增加了一个 HelloController, 提供 SayHelloService 服务, 并把自己注册到Consul.
当我们访问这个WebApi的 /WeatherForecast 时, 其Get()方法会访问 http://SayHelloService/Hello/NetCore, 这就相当于一次远程调用, 只是调用的就是这个WebApi的/Hello/NetCore
1. appsettings.json 增加
"ConsulDiscoveryOptions": {
"ConsulServerSetting": {
"IP": "127.0.0.1", // 必填
"Port": 8500, // 必填
"RefreshIntervalInMilliseconds": 1000
},
"ServiceRegisterSetting": {
"ServiceName": "SayHelloService", // 必填
"ServiceIP": "127.0.0.1", // 必填
"ServicePort": 5000, // 必填
"ServiceScheme": "http", // 只能是http 或者 https, 默认http,
"HealthCheckRelativeUrl": "/HealthCheck",
"HealthCheckIntervalInMilliseconds": 500,
"HealthCheckTimeOutInMilliseconds": 2000
}
}
2.修改Startup.cs
using ConsulDiscovery.HttpClient;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
namespace WebApplication1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// 注册 ConsulDiscovery 相关配置
services.AddConsulDiscovery(Configuration);
// 配置 SayHelloService 的HttpClient
services.AddHttpClient("SayHelloService", c =>
{
c.BaseAddress = new Uri("http://SayHelloService");
})
.AddHttpMessageHandler<DiscoveryHttpMessageHandler>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// 启动 ConsulDiscovery
app.StartConsulDiscovery(lifetime);
}
}
}
3. 添加 HelloController
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class HelloController : ControllerBase
{
[HttpGet]
[Route("{name}")]
public string Get(string name)
{
return $"Hello {name}";
}
}
}
4. 修改WeatherForecast
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly IHttpClientFactory httpClientFactory;
public WeatherForecastController(IHttpClientFactory httpClientFactory)
{
this.httpClientFactory = httpClientFactory;
}
[HttpGet]
public async Task<string> Get()
{
var httpClient = httpClientFactory.CreateClient("SayHelloService");
var result = await httpClient.GetStringAsync("Hello/NetCore");
return $"WeatherForecast return: {result}";
}
}
}
5. 启动consul
consul agent -dev
6. 启动 WebApplication1 并访问 http://localhost:5000/weatherforecast
以上示例可以到 https://github.com/zhouandke/ConsulDiscovery.HttpClient 下载, 请记住一定要 启动consul: consul agent -dev
End
来源:https://www.cnblogs.com/zhouandke/p/12957508.html


猜你喜欢
- CoordinatorLayout 实现了多种Material Design中提到的滚动效果。目前这个框架提供了几种不用写动画代码就能工作的
- 前言最近都是Mybatis-Plus系列的小白文,算是对工作中最常使用的框架的细节扫盲。有在学习Mybatis-Plus使用的,可以关注一波
- 一个界面,实现在向页面添加图片时,在标题上显示一个水平进度条,当图片载入完毕后,隐藏进度条并显示图片具体实现方法:res/layout/ma
- package com.chen.lucene.image;import java.io.File;import java.io.FileI
- Java Benchmark 基准测试的实例详解import java.util.Arrays; import java.util.conc
- 本文实例讲述了C#动态执行批处理命令的方法。分享给大家供大家参考。具体方法如下:C# 动态执行一系列控制台命令,并允许实时显示出来执行结果时
- 写在前面SpringBoot创建定时任务的方式很简单,主要有两种方式:一、基于注解的方式(@Scheduled)二、数据库动态配置。实际开发
- 最近做的项目,需要将一些信息导出到word中。在网上找了好多解决方案,现在将这几天的总结分享一下。目前来看,java导出word大致有6种解
- 问题在Service层注入Mybatis的Mapper我们通常会使用@Autowired 自动注入@Autowiredprivate Pro
- 一. 可变字符串1. 简介在Java中,我们除了可以通过String类创建和处理字符串之外,还可以使用StringBuffer和String
- 前言最近因为同事bean配置的问题导致生产环境往错误的redis实例写入大量的数据,差点搞挂redis。经过快速的问题定位,发现是同事新增一
- 1. A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoi
- 本文实例讲述了Java Swing实现让窗体居中显示的方法。分享给大家供大家参考,具体如下:Swing组件是AWT组建的增强组件,是功能强大
- 前段时间学习JDBC,要连接mysql获取数据。按照老师的样例数据,要存一些名字之类的信息,用的都是英文名,我当时就不太想用英文,就把我室友
- 将IDEA的默认快捷键设置 设置成为 Eclipse的默认快捷键设置题意有些拗口,但没关系,就是将idea中的快捷键转为自己刚学Java时使
- Elasticsearch 通常如何工作?我们将文档索引到 Elasticsearch 中并对其运行查询以获得满足提供的搜索条件的文档。 我
- 大二的时候做的课程设计,图片管理器,当时遇到图片很多的文件夹,加载顺序非常慢。虽然尝试用多个Thread加载图片,却无法保证图片按顺序加载。
- CopyOnWriteArrayList介绍它相当于线程安全的ArrayList。和ArrayList一样,它是个可变数组;但是和Array
- 由于最近通过SDK-Manager更新了build-tools,当要用到dx.jar这个包时,自动调用最新build-tools中dx.ja
- 一、动态编译简介new创建对象是静态加载类,在编译时刻就需要加载所有可能使用到的类。一百个类,有一个类错了,都无法编译。通过动态加载类可以解