服务注册中心-Eureka

  1. 版本:Spring Cloud 2021.0.x开始被弃用

  2. 需要添加依赖:Eureka Server Spring Web

  3. 作用:服务注册与发现(使服务调用方能够通过服务名找到服务提供方)

  4. 包含俩个组件:Eureka Server和Eureka Client

    1. Eureka Server:提供服务注册服务(各个节点启动后,会在Eureka Server中进行注册,将吧所有可用的服务节点的信息存储到Eureka的服服务注册表中
    2. Eureka Client:java客户端,用于简化与Eureka Server的交互,同时也是一个内置的、使用轮询负载算法的负载均衡器
  5. 运行机制:当应用启动后,Eureka Client将会向Eureka Server定期发送心跳,默认周期为30s,如果Eureka在多个心跳周期没有接收到某个节点的心跳,Eureka Server将会吧这个服务节点从服务注册表中移除(默认三个周期)

  6. Eureka Server直接通过复制的方式完成数据的同步,Eureka还提供客户缓存机制,当所有的Eureka Server都挂掉后,客户端依然可以利用缓存中的信息消费其他服务的API

  7. 服务注册:

    1. 服务实例向服务注册中心(Eureka Server)注册自身的过程。注册时,服务实例会向Eureka Server提供服务地址、端口、健康检查路径等信息,被Eureka Server存储起来用来后续的服务发现。
  8. 服务发现:

    1. 服务调用方根据服务名称查找服务实例的过程。Eureka Client从Eureka Server中获取服务实例列表,并从中选取实例进行调研,服务发现的过程是透明的,开发者只需要关注业务逻辑。
  9. 搭建Eureka服务模块:

    1. 1.创建Eureka Server模块
  10. 在pom.xml中添加依赖:

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

12.appication.yml中配置:

复制代码
eureka:
  instance:# Eureka实例的主机名,这里设置为localhost,通常用于本地开发环境,在实际部署到生产环境时,应根据具体的服务器配置修改为对应的主机名或IP地址
    hostname: localhost
  client:
    registerWithEureka: false   #服务实例不会将自身信息注册到Eureka服务器上
    fetchRegistry: false  #当前服务实例是否从Eureka注册中心拉取服务注册信息
    serviceUrl:  #Eureka客户端用来与Eureka Server进行通信的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  1. 创建启动类中添加@EnableEurekaServer注解

    package com.example.eurekaserver;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {

    复制代码
     public static void main(String[] args) {
         SpringApplication.run(EurekaServerApplication.class, args);
     }

    }

在服务提供者模块添加依赖

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

给application.yml文件中添加配置:

复制代码
eureka:
  client:
    service-url:
      defaultZone: http://eureka-server:8761/eureka/

服务提供这的启动类中添加@EnableEurekaClient注解

复制代码
@SpringBootApplication
@EnableEurekaClient
public class ItemServiceApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ItemServiceApplication.class, args);
    }
}
  1. 启动服务并查看服务注册情况
    1. 启动Eureka Server和服务提供这的启动类
    2. 打开浏览器访问 :
    3. http://localhost:8761/
  2. 查看服务实例列表,确认服务提供者是否被成功注册
    我的服务是 BUSINESS-SERVICE
相关推荐
HillVue2 小时前
中国未来 AI 路径的百度样本
大数据·eureka·dubbo
檀越剑指大厂19 小时前
查看 Docker 镜像详情的几种常用方法
docker·容器·eureka
轩轩Aminent1 天前
WSL 中的 Ubuntu 系统中使用 Docker
ubuntu·docker·eureka
斯普信专业组1 天前
Docker Registry 镜像缓存与客户端无感加速(以 Docker Hub 为例)
缓存·docker·eureka
颜淡慕潇2 天前
容器生态双核心:Podman与Docker深度对比及实战指南
docker·eureka·podman
周杰伦_Jay3 天前
【大模型数据标注】核心技术与优秀开源框架
人工智能·机器学习·eureka·开源·github
凯新生物3 天前
mPEG-SS-PLGA-DTX:智能药物递送系统
eureka·flink·ffmpeg·etcd
周杰伦_Jay4 天前
【BGE-M3与主流RAG嵌入模型】知识库嵌入模型对比
人工智能·机器学习·eureka·开源·github
qq_5470261794 天前
Docker 常用命令解析
docker·容器·eureka
周杰伦_Jay4 天前
【微服务注册与管理开源框架】从选型到实战(Nacos/Eureka/Consul/etcd/Zookeeper)
微服务·eureka·开源