服务注册中心-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
相关推荐
n1121212 小时前
Ollama 接 Open WebUI 中文界面:我踩的 4 个坑
云原生·eureka·ollama·本地大模型·open webui·私有部署·中文界面
水无痕simon2 天前
4、Eureka服务端安装
云原生·eureka
逃逸线LOF2 天前
为什么需要Actuator进行静态检查
spring cloud·eureka
csdn_aspnet6 天前
构建、运行与管理容器的 18 个常用 Docker 命令
docker·容器·eureka·command
一级新生12 天前
Dify 日常使用手册(小白版)
人工智能·docker·云原生·eureka·dify·ai工具
xixiaoyunya13 天前
Docker 常用命令与实战:从入门到部署一个简单应用
docker·容器·eureka
pirateeee13 天前
在ubuntu中通过 docker 搭建 yocto 开发环境
ubuntu·docker·eureka
江湖有缘15 天前
Docker实战 | 使用Docker部署Imagor Studio图片编辑工具
java·docker·eureka
林森i16 天前
ida 安装教程
eureka
江湖有缘17 天前
Docker实战 | 使用Docker部署Reubah文件转换器和图像处理器
docker·容器·eureka