当前位置: 首页 > ops >正文

微服务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.xdnf.cn/news/17619.html

相关文章:

  • LeetCode 2787.将一个数字表示成幂的和的方案数:经典01背包
  • Airtable 入门指南:从创建项目到基础数据分析与可视化
  • 渗透测试现已成为 CISO 战略的核心
  • 开疆智能Ethernet转ModbusTCP网关连接PAC3200电能表配置案例
  • 企业高性能web服务器(4)
  • 【运维进阶】Ansible 自动化
  • AI重构Java开发:飞算JavaAI如何实现效率与质量的双重突破?
  • 计算机网络摘星题库800题笔记 第6章 应用层
  • [Robotics_py] 机器人运动模型 | `update`函数 | 微积分矩阵
  • Visual Studio中VC++目录、C/C++和链接器配置的区别与最佳实践
  • 北京JAVA基础面试30天打卡08
  • 【问题解决】从Anaconda环境迁移到miniforge并在IDEA中完成环境配置
  • K8S学习---- Kubernetes 架构:从控制平面到工作节点的协作逻辑
  • Vue接口平台十三——测试记录
  • Git 撤回已推送到远程的最近push
  • 【数据结构入门】堆
  • NLP—词向量转换评论学习项目分析真实案例
  • 4.运算符
  • Docker命令及操作
  • imx6ull-驱动开发篇20——linux互斥体实验
  • 图解软件系统组成
  • 什么是iOS超级签名?为何它能解决企业签名的“掉签”难题?
  • 云原生高级---TOMCAT
  • [激光原理与应用-250]:理论 - 几何光学 - 透镜成像的优缺点,以及如克服缺点
  • 机器学习-集成学习(EnsembleLearning)
  • ETCD的简介和使用
  • 17、CryptoMamba论文笔记
  • 导入文件到iPhone实现
  • C++11-下
  • QT6 如何在Linux Wayland 桌面系统抓屏和分享屏幕