热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Consul学习笔记—服务发现

前言:上一篇文章简单实用Consul试下服务注册,本篇继续学习Consul中的另外特性:服


前言:

  上一篇文章简单实用Consul试下服务注册,本篇继续学习Consul中的另外特性:服务发现、KV操作 ;以及对上篇文章中存在的问题进行解决


问题解决

 在上一篇文章中,注册服务提示检查失败。


  通过排查发现为在docker 中运行的容器中配置的心跳检查api地址配置错误:

“Consul”: {
“Address”: “http://host.docker.internal:8500”,
“HealthCheck”: “/api/healthcheck”,//心跳检查api地址
“Name”: “czapigoods”,
“Ip”: “host.docker.internal”,
“Port”: “5602” //未指定成当前docker运行对于端口
}

“Consul”: {
“Address”: “http://host.docker.internal:8500”,
“HealthCheck”: “/api/healthcheck”,//心跳检查api地址
“Name”: “czapigoods”,
“Ip”: “host.docker.internal”,
“Port”: “5602” //未指定成当前docker运行对于端口
}

 解决方法(docker修改配置方式):修改docker中配置文件appsettings.json:

进入docker命令行:docker exec -it 容器id /bin/bash  例如:docker exec -it f38a7a2ddfba /bin/bash

更新软件列表:apt-get update 

安装vim命令:apt-get install vim

进入appsettings.json 修改:vim appsettings.json

修改appsettings中Consul.Port节点为对于docker映射端口

按Esc键,并输入:wq命令(退出保存修改)

重启对于容器效果如下


  Ps:Doker相关操作后面单独详细


服务发现

  服务注册问题解决了,接下来我们了解下服务如何发现;首先创建一个web项目Consul.Client并添加Consul包引用

Install-Package Consul

Install-Package Consul

 1、添加一个服务调用接口ICallService.cs用于调用我们添加的服务

public interface ICallService
{
///


/// 获取 Goods Service 返回数据
///

///
Task GetGoodsService();
///
/// 获取 Order Service 返回数据
///

///
Task GetOrderService();
///
/// 初始化服务
///

void InitServices();
}

public interface ICallService
{
///


/// 获取 Goods Service 返回数据
///

///
Task GetGoodsService();
///
/// 获取 Order Service 返回数据
///

///
Task GetOrderService();
///
/// 初始化服务
///

void InitServices();
}

 

 2、实现该接口CallService.cs调用Goods、Order的api方法  

public class CallService : ICallService
{
private readonly IConfiguration _configuration;
private readonly ConsulClient _consulClient;
private ConcurrentBag _serviceAUrls;
private ConcurrentBag _serviceBUrls;
private IHttpClientFactory _httpClient;
public CallService(IConfiguration configuration, IHttpClientFactory httpClient)
{
_cOnfiguration= configuration;
_cOnsulClient= new ConsulClient(optiOns=>
{
options.Address = new Uri(_configuration[“Consul:Address”]);
});
_httpClient = httpClient;
}
public async Task GetGoodsService()
{
if (_serviceAUrls == null)
return await Task.FromResult(“Goods Service Initializing…”);
using var httpClient = _httpClient.CreateClient();
//随机获取一个服务地址调用
var serviceUrl = _serviceAUrls.ElementAt(new Random().Next(_serviceAUrls.Count()));
Console.WriteLine(“Goods Service:” + serviceUrl);
var result = await httpClient.GetStringAsync($”{serviceUrl}/goods”);
return result;
}
public async Task GetOrderService()
{
if (_serviceBUrls == null)
return await Task.FromResult(“Order Service Initializing…”);
using var httpClient = _httpClient.CreateClient();
//随机获取一个服务地址调用
var serviceUrl = _serviceBUrls.ElementAt(new Random().Next(_serviceBUrls.Count()));
Console.WriteLine(“Order Service:” + serviceUrl);
var result = await httpClient.GetStringAsync($”{serviceUrl}/order”);
return result;
}
public void InitServiceList()
{
var serviceNames = new string[] { “czapigoods”, “czapiorder” };
foreach (var item in serviceNames)
{
Task.Run(async () =>
{
var queryOptiOns= new QueryOptions
{
WaitTime = TimeSpan.FromMinutes(5)
};
while (true)
{
await InitServicesAsync(queryOptions, item);
}
});
}
}
private async Task InitServicesAsync(QueryOptions queryOptions, string serviceName)
{
//获取心跳检查服务
var result = await _consulClient.Health.Service(serviceName, null, true, queryOptions);
if (queryOptions.WaitIndex != result.LastIndex)
{
queryOptions.WaitIndex = result.LastIndex;
var services = result.Response.Select(x => $”http://{x.Service.Address}:{x.Service.Port}”);
if (serviceName == “czapigoods”)
{
_serviceAUrls = new ConcurrentBag(services);
}
else if (serviceName == “czapiorder”)
{
_serviceBUrls = new ConcurrentBag(services);
}
}
}
}

public class CallService : ICallService
{
private readonly IConfiguration _configuration;
private readonly ConsulClient _consulClient;
private ConcurrentBag _serviceAUrls;
private ConcurrentBag _serviceBUrls;
private IHttpClientFactory _httpClient;
public CallService(IConfiguration configuration, IHttpClientFactory httpClient)
{
_cOnfiguration= configuration;
_cOnsulClient= new ConsulClient(optiOns=>
{
options.Address = new Uri(_configuration[“Consul:Address”]);
});
_httpClient = httpClient;
}
public async Task GetGoodsService()
{
if (_serviceAUrls == null)
return await Task.FromResult(“Goods Service Initializing…”);
using var httpClient = _httpClient.CreateClient();
//随机获取一个服务地址调用
var serviceUrl = _serviceAUrls.ElementAt(new Random().Next(_serviceAUrls.Count()));
Console.WriteLine(“Goods Service:” + serviceUrl);
var result = await httpClient.GetStringAsync($”{serviceUrl}/goods”);
return result;
}
public async Task GetOrderService()
{
if (_serviceBUrls == null)
return await Task.FromResult(“Order Service Initializing…”);
using var httpClient = _httpClient.CreateClient();
//随机获取一个服务地址调用
var serviceUrl = _serviceBUrls.ElementAt(new Random().Next(_serviceBUrls.Count()));
Console.WriteLine(“Order Service:” + serviceUrl);
var result = await httpClient.GetStringAsync($”{serviceUrl}/order”);
return result;
}
public void InitServiceList()
{
var serviceNames = new string[] { “czapigoods”, “czapiorder” };
foreach (var item in serviceNames)
{
Task.Run(async () =>
{
var queryOptiOns= new QueryOptions
{
WaitTime = TimeSpan.FromMinutes(5)
};
while (true)
{
await InitServicesAsync(queryOptions, item);
}
});
}
}
private async Task InitServicesAsync(QueryOptions queryOptions, string serviceName)
{
//获取心跳检查服务
var result = await _consulClient.Health.Service(serviceName, null, true, queryOptions);
if (queryOptions.WaitIndex != result.LastIndex)
{
queryOptions.WaitIndex = result.LastIndex;
var services = result.Response.Select(x => $”http://{x.Service.Address}:{x.Service.Port}”);
if (serviceName == “czapigoods”)
{
_serviceAUrls = new ConcurrentBag(services);
}
else if (serviceName == “czapiorder”)
{
_serviceBUrls = new ConcurrentBag(services);
}
}
}
}

 3、接下来添加接口依赖注入、以及服务初始化调用  

public class Startup
{
public Startup(IConfiguration configuration)
{
COnfiguration= configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient();
     //依赖注入CallService
services.AddSingleton();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ICallService service)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
     //初始化服务列表调用
service.InitServiceList();
}
}  

public class Startup
{
public Startup(IConfiguration configuration)
{
COnfiguration= configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHttpClient();
     //依赖注入CallService
services.AddSingleton();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ICallService service)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
     //初始化服务列表调用
service.InitServiceList();
}
}  

 4、使用Postman测试调用


  

KV操作

 除了提供服务发现和健康检查的集成.Consul提供了一个易用的键/值存储.这可以用来保持动态配置,协助服务协调,领袖选举,做开发者可以想到的任何事情.

 1、创建/修改

命令方式:consul kv put key val 如:consul kv put port 9000 –添加key为port值为9000

Http方式:


 2、查询  

命令方式:consul kv get key 如:consul kv get port –查询key为port的KV

Http方式:value为baisc


 3、删除

命令方式:consul kv delete key 如:consul kv delete port –删除key为port的KV

Http方式:



 其他

 网上找了下:常用服务发现框架consul、zookeeper及etcd比较:


参考:

consul手册:https://blog.csdn.net/liuzhuchen/article/details/81913562 

https://www.consul.io/docs

https://www.consul.io/api/kv.html

github:

https://github.com/cwsheng/Consul.Demo.git



推荐阅读
  • 本文介绍了为什么要使用多进程处理TCP服务端,多进程的好处包括可靠性高和处理大量数据时速度快。然而,多进程不能共享进程空间,因此有一些变量不能共享。文章还提供了使用多进程实现TCP服务端的代码,并对代码进行了详细注释。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
author-avatar
只想活得快乐的魔羯
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有