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

[Spring Cloud][6] Eureka Server 搭建详解,与 Zookeeper 的区别

文章目录

  • 搭建 Eureka Server
    • 创建 eureka-server 子模块
    • 引入依赖
    • 项目构建插件
    • 完善启动类
    • 编写配置文件
    • 启动服务
  • 服务注册
    • 引入 eureka-client 依赖
    • 完善配置文件
    • 启动服务
  • 服务发现
    • 引入依赖
    • 完善配置文件
    • 远程调用
    • 启动服务
  • Eureka 和 Zookeeper 区别

搭建 Eureka Server

Eureka Server 是一个独立的微服务

创建 eureka-server 子模块

image.png|311

引入依赖

<dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>  
</dependency>

项目构建插件

<build>  <plugins>        <plugin>           <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-maven-plugin</artifactId>  </plugin>    </plugins>
</build>

完善启动类

package org.example.eureka;  import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;  @EnableEurekaServer  
@SpringBootApplication  
public class EurekaServerApplication {  public static void main(String[] args) {  SpringApplication.run(EurekaServerApplication.class, args);  }  
}

编写配置文件

server:  port: 10010  
spring:  application:  name: eureka-server  
eureka:  instance:  hostname: localhost  client:  fetch-registry: false  register-with-eureka: false  service-url:  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • fetch-registry:表示是否从 Eureka Server 获取注册信息,默认为 true。因为这是一个单点的 Eureka Server,不需要同步其他的 Eureka Server 节点的数据,这里设置为 false
  • register-with-eureka:表示是否将自己注册到 Eureka Server,默认为 true。由于当前应用就是 Eureka Server,故而设置为 false

启动服务

启动服务,访问注册中心: http://127.0.0.1:10010/
image.png

  • 可以看到,eureka-server 已经启动成功了

服务注册

接下来我们把 product_service 注册到 eureka—server

引入 eureka-client 依赖

<dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
</dependency>

完善配置文件

spring: application: name: product-service
eureka:  client:  service-url:  defaultZone: http://127.0.0.1:10010/eureka

启动服务

刷新注册中心: http://127.0.0.1:10010/
image.png|442

  • 可以看到 product-service 已经注册到 eureka 上了

服务发现

接下来我们修改 order-service,在远程调用时,从 eureka-server 拉取 product-service 的服务信息,实现服务发现

引入依赖

<dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
</dependency>

完善配置文件

服务发现也需要知道 eureka 地址,因此配置内容依然与服务注册一致,都是配置 eureka 信息

spring: application: name: order-service
eureka:  client:  service-url:  defaultZone: http://127.0.0.1:10010/eureka

远程调用

远程调用时,我们需要从 eureka-server 中获取 product-service 的列表(可能存在多个服务),并选择其中一个进行调用

package org.example.order.service;  import jakarta.annotation.Resource;  
import lombok.extern.slf4j.Slf4j;  
import org.example.order.mapper.OrderMapper;  
import org.example.order.model.OrderInfo;  
import org.example.order.model.ProductInfo;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.cloud.client.ServiceInstance;  
import org.springframework.cloud.client.discovery.DiscoveryClient;  
import org.springframework.cloud.netflix.eureka.EurekaServiceInstance;  
import org.springframework.stereotype.Service;  
import org.springframework.web.client.RestTemplate;  import java.util.List;  @Slf4j  
@Service  
public class OrderService {  @Autowired  private OrderMapper orderMapper;  @Resource  private DiscoveryClient discoveryClient;  @Autowired  private RestTemplate restTemplate;  public OrderInfo selectOrderById(Integer orderId) {  OrderInfo orderInfo = orderMapper.selectOrderById(orderId);  //String url = "http://127.0.0.1:9090/product/" + orderInfo.getProductId();  // 根据应用名称获取服务列表  List<ServiceInstance> instances = discoveryClient.getInstances("product-service");  // 服务可能有多个,获取第一个  EurekaServiceInstance instance = (EurekaServiceInstance) instances.get(0);  log.info(instance.getInstanceId());  // 拼接 URL        String url = instance.getUri() + "/product/" + orderInfo.getProductId();  ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);  orderInfo.setProductInfo(productInfo);  return orderInfo;  }  
}

启动服务

刷新注册中心: http://127.0.0.1:10010/
image.png|420

  • 可以看到 order-service 已经注册到 eureka 上了

访问接口: http://127.0.0.1:8080/order/1
可以看到,远程调用也成功了:image.png|399

Eureka 和 Zookeeper 区别

EurekaZookeeper 都是用于服务注册和发现的工具,区别如下:

  1. EurekaNetFlix 开源的项目,而 ZookeeperApache 开源的项目
  2. Eureka 基于 AP 原则,保证高可用,Zookeeper 基于 CP 原则,保证数据一致性
  3. Eureka 每个节点都是均等的,Zookeeper 的节点区分 LeaderFollowerObserver,也正因为这个原因,如果 ZookeeperLeader 发生故障时,需要重新选举,选举过程集群会有短暂时间的不可用
http://www.dtcms.com/a/389954.html

相关文章:

  • 前端性能优化完全指南:从入门到实战
  • 国产组态软件对工控行业的影响及作用
  • Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
  • 从把python里的一个函数变成接口到自动化测试平台的实现
  • LibreTV+cpolar:打造私人云影院的智能方案
  • 软考高级系统架构设计师之架构设计扩展篇(一)
  • 宝德PR1710P服务器安装Anolis8.6系统
  • ABAP读写SAP服务器文件
  • 无人机操控核心:智能飞行的技术引擎
  • H5页面在真机移动端1px边框处理方案总结
  • 本地大模型部署与应用: Dify 与 Ollama 集成
  • 从 “盲调” 到 “精准优化”:SQL Server 表统计信息实战指南
  • ffmpeg.dll是什么?4步彻底解决ffmpeg.dll丢失报错问题
  • ROS2C++核心基础
  • 第二篇:搭建现代C++开发环境:VS2022 / CLion / VSCode实战
  • 【群晖NAS】一键脚本搭建frp内网穿透,在外轻松远程访问内网设备|远程桌面
  • 【HTML】 第一章:HTML 基础
  • 【RAG】知识库问答不是只有 RAG
  • 前端缓存深度解析:localStorage 到底是同步还是异步?
  • Vue2 基础知识点二:事件绑定 (Event Binding)
  • ​​[硬件电路-250]:LDO电源核心指标、典型问题与工程实践指南
  • 论文笔记(九十二)RLVR-World: Training World Models with Reinforcement Learning
  • 驾校培训办公管理系统 专属驾校的OA系统 驾培管理行业
  • 绿色纺织品的国际通行证:GRS认证的深度解析
  • 如何解决 pip install 安装报错 ModuleNotFoundError: No module named ‘cryptography’ 问题
  • Linux网络:应用层http
  • 基于GeoDa与R语言的空间数据回归实践技术应用
  • 硅基计划3.0 学习总结 反射枚举Lambada表达式
  • 创作一个简单的编程语言2 ,开始增加中文关键字的功能
  • AI之EBT:《Energy-Based Transformers are Scalable Learners and Thinkers》的翻译与解读