您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。
- 内容介绍
- 文章标签
- 相关推荐
本文共计510个文字,预计阅读时间需要3分钟。
使用Overt.Core.Grpc后,将GRPC的使用体验改造得像WCF一样流畅。性能测试也相当出色,强烈推荐各位试用。不过,现有项目大多使用http请求,若改用GRPC,工作量较大。最近发现Steeltoe.Discovery,可用于服务发现,进一步提升了工作效率。
试用了Overt.Core.Grpc, 把 GRPC 的使用改造得像 WCF, 性能测试也非常不错, 非常推荐各位使用.
但已有项目大多是 {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 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": "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 localhost:5000/weatherforecast
以上示例可以到github.com/zhouandke/ConsulDiscovery.HttpClient下载, 请记住一定要启动consul:consul agent -dev
End
以上就是C# HttpClient 如何使用 Consul 发现服务的详细内容,更多关于C# HttpClient使用 Consul 发现服务的资料请关注自由互联其它相关文章!
本文共计510个文字,预计阅读时间需要3分钟。
使用Overt.Core.Grpc后,将GRPC的使用体验改造得像WCF一样流畅。性能测试也相当出色,强烈推荐各位试用。不过,现有项目大多使用http请求,若改用GRPC,工作量较大。最近发现Steeltoe.Discovery,可用于服务发现,进一步提升了工作效率。
试用了Overt.Core.Grpc, 把 GRPC 的使用改造得像 WCF, 性能测试也非常不错, 非常推荐各位使用.
但已有项目大多是 {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 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": "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 localhost:5000/weatherforecast
以上示例可以到github.com/zhouandke/ConsulDiscovery.HttpClient下载, 请记住一定要启动consul:consul agent -dev
End
以上就是C# HttpClient 如何使用 Consul 发现服务的详细内容,更多关于C# HttpClient使用 Consul 发现服务的资料请关注自由互联其它相关文章!

