微服务ETCD服务注册和发现

1.什么是注册中心

注册中心主要有三种角色:

  • 服务提供者(RPC Server):在启动时,向 Registry 注册自身服务,并向 Registry 定期发送心跳汇报存活状态。

  • 服务消费者(RPC Client):在启动时,向 Registry 订阅服务,把 Registry 返回的服务节点列表缓存在本地内存中,并与 RPC Sever 建立连接。

  • 服务注册中心(Registry):用于保存 RPC Server 的注册信息,当 RPC Server 节点发生变更时,Registry 会同步变更,RPC Client 感知后会刷新本地 内存中缓存的服务节点列表。

2.框架版本

spring boot:2.7.13

spring cloud:2021.0.1

3.xxx-discovery-etcd

支持etcd作为服务的注册中心,在微服务中使用

3.1.使用

pom.xml中引入依赖

<dependency><groupId>x.xx.xxx</groupId><artifactId>xxx-discovery-etcd</artifactId>
</dependency>

application.yml中配置

spring:application:name: etcd-provider-examplexxx:discovery:etcd:server-addr: http://192.168.184.133:2379instance-name: provider1                

启动主类增加注解@EnableDiscoveryClient

4.Spring Cloud和xxx-etcd-discovery的结合

4.1.etcd-provider-demo

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
​<groupId>org.example</groupId><artifactId>etcd-provider-demo</artifactId><version>1.0-SNAPSHOT</version>
​<dependencies><dependency><groupId>x.xx.xxx</groupId><artifactId>xxx-discovery-etcd</artifactId></dependency>
​<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
​<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>
​</dependencies>
​
</project>

bootstrap.yml

server:port: 18082
spring:application:name: etcd-provider-example
​
xxx:discovery:etcd:server-addr: http://192.168.184.133:2379instance-name: provider1
​
logging:level:root: DEBUGio:grpc:netty:NettyClientHandler: INFO
​org:springframework:boot:autoconfigure: INFO

EtcdProviderExampleApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
​
@SpringBootApplication
@EnableDiscoveryClient
public class EtcdProviderExampleApplication {
​public static void main(String[] args) {SpringApplication.run(EtcdProviderExampleApplication.class, args);}
}

EchoController

package x.xx.xxx.etcd.demo;
​
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
public class EchoController {@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)public String echo(@PathVariable String string) {return System.currentTimeMillis() + "--Hello Etcd Discovery " + string;}
}

4.2.etcd-consumer-demo

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>
​<groupId>org.example</groupId><artifactId>etcd-consumer-demo</artifactId><version>1.0-SNAPSHOT</version>
​<dependencies><dependency><groupId>x.xx.xxx</groupId><artifactId>xxx-discovery-etcd</artifactId></dependency>
​<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
​<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency>
​<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></dependency>
​</dependencies>
​
</project>

bootstrap.yml

server:port: 18081
​
spring:application:name: etcd-consumer-example
​
xxx:discovery:etcd:server-addr: http://192.168.184.133:2379instance-name: consumer1logging:level:root: DEBUGio:grpc:netty:NettyClientHandler: INFO
​org:springframework:boot:autoconfigure: INFO

EtcdConsumerExampleApplication

package x.xx.xxx.etcd.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;@SpringBootApplication
@EnableDiscoveryClient
public class EtcdConsumerExampleApplication {public static void main(String[] args) {SpringApplication.run(EtcdConsumerExampleApplication.class, args);}@LoadBalanced@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}@Bean@LoadBalancedpublic WebClient.Builder loadBalancedWebClientBuilder() {return WebClient.builder();}
}

TestController

package x.xx.xxx.etcd.demo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;@RestController
public class TestController {private final RestTemplate restTemplate;@Autowiredpublic TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)public String echo(@PathVariable String str) {return restTemplate.getForObject("http://etcd-provider-example/echo/" + str, String.class);}@Autowiredprivate WebClient.Builder webClientBuilder;@RequestMapping(value = "/echo/webClient/{str}", method = RequestMethod.GET)public String echoWebClient(@PathVariable String str) {WebClient webClient = webClientBuilder.build();webClient.get().uri("http://etcd-provider-example/echo/" + str).retrieve().bodyToMono(String.class).subscribe(response -> {System.err.println("控制台响应结果:" + response);});return System.currentTimeMillis() + "--echoWebClient--" + str + "--请查看控制台是否执行成功!";}}

4.3.启动主程序

EtcdProviderExampleApplication

EtcdConsumerExampleApplication

通过ETCD Manager查看,如下表示注册成功

请求etcd-consumer-demo中接口,查看服务发现功能,更改测试接口,可查看TestController

http://localhost:18081/echo/6666

浏览器返回如下信息表示服务发现成功

1705978409084--Hello Etcd Discovery 6666

5.xxx-etcd-discovery详解

5.1.可配置属性

5.2.存储到etcd的格式

5.3.监控ectd服务端事件,并发送事件

5.4.启动全量查所有服务列表并缓存

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/92765.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/92765.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

计算机网络---默认网关(Default Gateway)

一、默认网关的定义 默认网关&#xff08;Default Gateway&#xff09;是一个网络设备&#xff08;通常是路由器、防火墙或三层交换机&#xff09;的IP地址&#xff0c;它是本地网络中的设备访问其他网络&#xff08;如外网、其他子网&#xff09;时&#xff0c;数据报文的“第…

OpenBMC中libgpio架构与驱动交互全解析:从硬件映射到应用控制

1. libgpio概述与核心定位 libgpio作为OpenBMC中GPIO管理的核心库&#xff0c;扮演着连接硬件驱动与上层应用的桥梁角色。它通过标准化的接口抽象了不同硬件平台的GPIO操作细节&#xff0c;使得电源控制、传感器监控等关键功能能够以统一的方式访问GPIO资源。 1.1 libgpio在Ope…

开放原子开源生态大会:麒麟信安加入openEuler社区AI联合工作组,聚焦操作系统开源实践与行业赋能

7月23日&#xff0c;由开放原子开源基金会主办的2025开放原子开源生态大会在京开幕&#xff0c;大会以“开源赋能产业&#xff0c;生态共筑未来”为主题。工业和信息化部副部长熊继军、北京市人民政府副秘书长许心超出席大会并致辞。作为开放原子开源基金会黄金捐赠人和开源重要…

Lyapunov与SAC算法的数学结构对比:从二次漂移到TD损失

一、李雅普诺夫优化中二次漂移函数的推导 李雅普诺夫优化的核心是通过设计 “李雅普诺夫函数” 和 “漂移项”&#xff0c;保证系统状态收敛到稳定点。以下以线性时不变系统为例&#xff08;非线性系统推导逻辑类似&#xff0c;仅动力学方程更复杂&#xff09;&#xff0c;推导…

WireShark:非常好用的网络抓包工具

文章目录一、写在前面二、安装三、使用1、入门使用&#xff08;1&#xff09;打开软件&#xff08;2&#xff09;右键网卡&#xff0c;Start Capture(开始捕获)2、界面详细介绍3、过滤器设置一、写在前面 Wireshark是使用最广泛的一款「开源抓包软件」&#xff0c;常用来检测网…

WEB技术演进史:从C/S到微服务架构

WEB技术 HTTP协议和B/S 结构 操作系统有进程子系统&#xff0c;使用多进程就可以充分利用硬件资源。进程中可以多个线程&#xff0c;每一个线程可以被CPU调度执行&#xff0c;这样就可以让程序并行的执行。这样一台主机就可以作为一个服务器为多个客户端提供计算服务。 客户端…

win11中Qt5.14.0+msvc2019+opencv4.9配置

本文主要研究由msvc编译的opencv在QT中的配置&#xff0c;opencv可以是官网直接下载的版本&#xff0c;也可以是msvc(例如vs2019)通过cmake编译 contrib功能的opencv版本&#xff0c;这2种版本对qt版本没有严格要求&#xff0c;但是若在cmake中选择了with_qt功能&#xff0c;那…

【listlist模拟】

list&list模拟1.list使用2、list模拟附录1.list使用 list常见接口不做介绍&#xff0c;跟前面vector有相似之处&#xff0c;跟数据结构list基本一样。  因为list使用带头的双向循环链表实现的&#xff0c;不能用小标访问&#xff0c;只能用迭代器或范围for访问 list有成…

在CentOS 7上将PostgreSQL数据库从默认路径迁移到自定义目录

在CentOS 7上将PostgreSQL数据库从默认路径迁移到自定义目录&#xff0c;需遵循以下步骤。假设原数据目录为“/var/lib/pgsql/12/data”&#xff0c;目标目录为“/new/path/pgdata”。 1、步骤概览 停止PostgreSQL服务创建新目录并设置权限复制数据文件&#xff08;保留权限&am…

C语言基础06——结构体(struct)

一、结构体的概念结构体&#xff08;struct&#xff09;是 C 语言中一种自定义数据类型&#xff0c;它允许你将不同类型的数据项组合在一起&#xff0c;形成一个新的复合数据类型。想象一下&#xff1a;如果要表示一个 "学生"&#xff0c;需要包含姓名&#xff08;字…

小白入门指南:Edge SCDN 轻松上手

在互联网飞速发展的当下&#xff0c;网站性能与安全至关重要。对于小白而言&#xff0c;Edge SCDN 可能是个陌生概念&#xff0c;但它却能极大助力网站运营。本文将用简单易懂的语言&#xff0c;带大家了解 Edge SCDN&#xff0c;探讨其运用方法。​一、Edge SCDN 是什么&#…

探秘酵母单杂交技术:解锁基因调控的密码

在生命科学研究领域&#xff0c;基因的表达调控机制一直是科学家们关注的焦点。为了深入探究这一复杂过程&#xff0c;众多先进技术应运而生&#xff0c;酵母单杂交技术便是其中极具价值的一项&#xff0c;它为研究 DNA 与蛋白质之间的相互作用提供了独特视角与有效手段。酵母单…

大模型备案要点一次过【附材料清单详解】

最近&#xff0c;广东省公布了最新一批的大模型备案&#xff08;登记&#xff09;名单&#xff0c;很多准备要做大模型备案的企业都在纷纷咨询&#xff1a;“大模型备案的周期是多久&#xff1f;”“做大模型备案有什么要求&#xff1f;”“做大模型备案一共需要准备多少材料&a…

启保停-----------单相照明灯的接法

一.单相照明灯-K21使用的器材,单相电能表,空开,插座,开关,灯泡二.启 保 停1.需要用到的器材1.空开2.三相电机3.接触器4.熔断器5.按钮2.电路的作用按按钮 运转 在按按钮 停止运转3.电动4.加上辅助触点 控制电路5.在加上按钮 停止电路

TF-IDF:信息检索与文本挖掘的统计权重基石

本文由「大千AI助手」原创发布&#xff0c;专注用真话讲AI&#xff0c;回归技术本质。拒绝神话或妖魔化。搜索「大千AI助手」关注我&#xff0c;一起撕掉过度包装&#xff0c;学习真实的AI技术&#xff01; 1. 背景与定义 TF-IDF 是一种统计加权方法&#xff0c;用于衡量词语在…

[论文阅读] (41)JISA24 物联网环境下基于少样本学习的攻击流量分类

《娜璋带你读论文》系列主要是督促自己阅读优秀论文及听取学术讲座&#xff0c;并分享给大家&#xff0c;希望您喜欢。由于作者的英文水平和学术能力不高&#xff0c;需要不断提升&#xff0c;所以还请大家批评指正&#xff0c;非常欢迎大家给我留言评论&#xff0c;学术路上期…

react中父子数据流动和事件互相调用(和vue做比较)

前言&#xff1a;react中父子数据流动和事件互相调用&#xff0c;父组件给子组件数据&#xff0c;父组件调用子组件的事件&#xff0c;同理&#xff0c;子也可以调用父的数据和传值。react是单向数据流&#xff0c;具体使用跟vue是不同的。1、父组件的数据传给子组件&#xff0…

杰理手表-增加提示音-提示音音量调整--使用提示音

本章节非常详细的介绍这个提示音的增加-调整-使用&#xff0c;其余耳机包之类的也是差不多的&#xff01;&#xff01; 目录 1.添加自己需要用的提示音 2.根据添加的提示音-代码中配置 1.在tone_player.h中枚举里添加本次提示音的名称 2.把定义好的提示音放到tone_player.…

数据库的基本操作(视图,存储,触发器)

1、视图&#xff08;1&#xff09;什么是视图视图是虚拟表&#xff0c;是基于查询结果的可视化表&#xff0c;视图的作用有&#xff1a;①简化复杂查询 ②限制数据访问 ③提供数据独立性 ④汇总数据&#xff08;2&#xff09;怎么创建视图创建视图 CREATE OR REPLACE VIEW 视图…

Pytest项目_day13(usefixture方法、params、ids)

usefixture 我们还可以使用mark.usefixtures来调用fixture 这样相比在传入参数处调用fixture&#xff0c;会更加直接 但是如果我们在一个测试用例中使用了多个usefixtures&#xff0c;那么测试用例会先调用离他最近的那个fixtureparams fixture中还可以带参数 当我们用request.…