- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python 使用 consul 做服务发现示例详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
前面一章讲了微服务的一些优点和缺点,那如何做到 。
我们可以直接使用官方提供的二进制文件来进行安装部署,其官网地址为 https://www.consul.io/downloads 。
下载后为可执行文件,在我们开发试验过程中,可以直接使用 consul agent -dev 命令来启动一个单节点的 consul 。
在启动的打印日志中可以看到 agent: Started HTTP server on 127.0.0.1:8500 (tcp), 我们可以在浏览器直接访问 127.0.0.1:8500 即可看到如下 。
这里我们的 consul 就启动成功了 。
在网络编程中,一般会提供项目的 IP、PORT、PROTOCOL,在服务治理中,我们还需要知道对应的服务名、实例名以及一些自定义的扩展信息 。
在这里使用 ServiceInstance 接口来规定注册服务时必须的一些信息 。
class ServiceInstance: def __init__(self, service_id: str, host: str, port: int, secure: bool = False, metadata: dict = None, instance_id: str = None): self.service_id = service_id self.host = host self.port = port self.secure = secure self.metadata = metadata self.instance_id = instance_id def get_instance_id(self): return
定义基类 。
在上面规定了需要注册的服务的必要信息,下面定义下服务注册和剔除的方法,方便以后实现 Eureka 和 Redis 的方式 。
import abcclass ServiceRegistry(abc.ABC): @abc.abstractmethod def register(self, service_instance: ServiceInstance): pass @abc.abstractmethod def deregister(self): pass
具体实现 。
因为 consul 提供了 http 接口来对consul 进行操作,我们也可以使用 http 请求方式进行注册和剔除操作,具体 http 接口文档见 https://www.consul.io/api-docs, consul 并没有提供 Python 语言的实现,这里使用 python-consul 来访问 consul 。
import consulclass ConsulServiceRegistry(ServiceRegistry): _consul = None _instance_id = None def __init__(self, host: str, port: int, token: str = None): self.host = host self.port = port self.token = token self._consul = consul.Consul(host, port, token=token) def register(self, service_instance: ServiceInstance): schema = "http" if service_instance.secure: schema = "https" check = consul.Check.http(f'{schema}:{service_instance.host}:{service_instance.port}/actuator/health', "1s", "3s", "10s") self._consul.agent.service.register(service_instance.service_id, service_id=service_instance.instance_id, address=service_instance.host, port=service_instance.port, check=check) self._instance_id = service_instance.instance_id def deregister(self): if self._instance_id: self._consul.agent.service.deregister(service_id=self._instance_id) self._instance_id = None
在服务发现中,一般会需要两个方法 。
基类定义 。
import abcclass DiscoveryClient(abc.ABC): @abc.abstractmethod def get_services(self) -> list: pass @abc.abstractmethod def get_instances(self, service_id: str) -> list: pass
具体实现 。
来实现一下 。
这里是简化版,所以一些参数直接写死了,如果需要可以适当修改 。
import consulclass ConsulServiceDiscovery(DiscoveryClient): _consul = None def __init__(self, host: str, port: int, token: str = None): self.host = host self.port = port self.token = token self._consul = consul.Consul(host, port, token=token) def get_services(self) -> list: return self._consul.catalog.services()[1].keys() def get_instances(self, service_id: str) -> list: origin_instances = self._consul.catalog.service(service_id)[1] result = [] for oi in origin_instances: result.append(ServiceInstance( oi.get('ServiceName'), oi.get('ServiceAddress'), oi.get('ServicePort'), oi.get('ServiceTags'), oi.get('ServiceMeta'), oi.get('ServiceID'), )) return result
import unittestfrom random import randomclass MyTestCase(unittest.TestCase): def test_consul_register(self): instance = ServiceInstance("abc", "127.0.0.1", 8000, instance_id=f'abc_{random()}') registry = ConsulServiceRegistry("127.0.0.1", 8500) discovery = ConsulServiceDiscovery("127.0.0.1", 8500) registry.register(instance) print(discovery.get_services()) print(discovery.get_instances("abc")) self.assertEqual(True, True)if __name__ == '__main__': unittest.main()
通过使用 consul api 我们可以简单的实现基于 consul 的服务发现,在通过结合 http rpc 就可简单的实现服务的调用,下面一章来简单讲下 go 如何发起 http 请求,为我们做 rpc 做个铺垫 。
具体代码见 https://github.com/zhangyunan1994/gimini 。
参考 。
https://www.consul.io/api-docs 。
https://github.com/hashicorp/consul/tree/master/api 。
到此这篇关于Python 使用 consul 做服务发现的文章就介绍到这了,更多相关Python 使用 consul 服务内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/zyndev/article/details/113888620 。
最后此篇关于Python 使用 consul 做服务发现示例详解的文章就讲到这里了,如果你想了解更多关于Python 使用 consul 做服务发现示例详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
Consul服务定义json如下 { "Address": "192.168.10.10", "TaggedAddresses": { "lan": "192.168.10
在 Consul 中,您可以拥有许多代理作为服务器或客户端。在所有服务器中,一个被选为领导者。从代理的角度来看,它怎么知道自己是领导者? 最佳答案 consul operator raft list-
对于 Eureka,服务可以直接将自己注册到 Eureka 服务器。为什么我们应该向 Consul 客户端而不是 Consul 服务器发送请求?让服务直接与Consul服务器通信有什么问题吗? 感谢您
我四处搜索,这似乎不可能,但我想确认我没有遗漏任何东西。我们正在向 consul 注册一个应用程序。当服务终止时,它会在一定时间后自动删除。如果可能的话,这个选项是可配置的吗? 最佳答案 我认为检查注
我正在使用 Traefik 在 Consul 中注册的不同服务之间进行负载平衡。 我正在使用consul-catalog配置并通过在 consul 中定义服务时添加标签来覆盖其中一项服务的前端路由规则
我正在尝试提出 consul用于生产目的的集群。我没有找到太多关于部署 consul 集群的最佳实践的信息。假设我想要一个包含 3 个节点的集群。我想知道以下场景之间有什么区别以及首选哪种场景。 正在
我发现在几种情况下,存储有关特定服务的附加元数据会很方便,但services API中似乎不支持自定义字段。 (只有基本的id、姓名、地址、端口)。例如,数据库名称或负载均衡器权重。 我对设计决策很好
所以我构建了一个 3 节点的 Consul 集群。现在它们仅由 IP 地址表示。在阅读文档时,我不清楚如何将他们的位置暴露给其他想要查询的人。 我可以将当前领导者的 IP 地址硬编码到其他代理中,
我目前是一名实习生,必须托管一个微服务应用程序。我选择将 AWS ECS 与 Fargate 任务结合使用来托管 Consul Connect Service Mesh,为应用程序提供服务发现、意图、
我将 Ocelot 和 API 网关与 Consul 和服务发现一起使用。我正在 Consul 中使用动态名称注册服务,例如:service.name.1234 和 service.name.5678
实现这个需要什么配置? 可以使用此处提到的“开发模式”- https://learn.hashicorp.com/consul/getting-started/agent (但不推荐用于生产)。 我试
我的 Prometheus 服务器从 Consul 获取它的目标列表(或“服务”,在 Consul 的行话中)。我只想监控这些目标的一个子集。这应该可以通过 Prometheus 的 regex 机制
我们使用 Consul 并且我们愿意强制开发人员只能使用 git2consul 方法来更改它,以保留属性更改的历史并维护备份。 为了确保这一点,我们希望使 Consul Key-Value 浏览器 U
当 Prometheus 使用 Consul 的自动发现功能来获取要监控的目标列表时,它也会自己获取 Consul 服务器。这很棒 - 我们想用 Prometheus 监控这些人。问题是Consul用
我有一个使用以下服务定义向 Consul 注册的测试服务: { "name": "web", "tags": ["web1"], "address": "example.com", "
我需要更改默认的 http 端口,因为另一个应用程序已经在使用 8500。 此命令有效: 领事信息-http-addr=http://127.0.0.1:18500 我无法弄清楚这等于配置文件中的什么
我试图在更新 consul 的 KV 对时获取它的锁,所以没有其他人可以更新它。 最初我有 curl -XGET http://localhost:8500/v1/kv/hosts?raw {"k1"
我想知道是否有办法过滤服务领事使用标签返回我。 终点: /v1/catalog/services 将服务映射返回到标签列表,并要求我在返回后解析服务。 我想知道是否有某种方法可以将我想要的标签(或多个
我正在尝试了解如何使用 Consul 进行应用程序领导者选举。我正在使用来自 java consul-client 的 LeaderElectionUtil。 我可以选举一个领导者,并且所有节点都同意
我决定关注this指导,我遇到了很多问题。 首先需要在 command 中指定 traefik 命令,否则我会遇到 entrypoint.sh 找不到的错误命令 storedata,是 > yaml
我是一名优秀的程序员,十分优秀!