服务注册中心-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
相关推荐
1candobetter6 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
一次旅行6 天前
Docker安全总结
安全·docker·eureka
老葱头蒸鸡6 天前
(3)Docker搭建Network网络
云原生·eureka
weixin_395448917 天前
cursor日志0224
eureka·flink·etcd
hopsky7 天前
Docker Compose 启动的容器内存 监控
docker·容器·eureka
海兰7 天前
Docker部署OpenClaw及常见问题解决(win11)
docker·容器·eureka
曹天骄7 天前
在 Docker 容器中控制宿主机 Docker:DoD 与 DinD 的技术原理解析
docker·容器·eureka
sunshinebo7 天前
一次 GitLab 无法启动的排查:Docker 日志把 500G 磁盘打满
docker·eureka·gitlab
weixin_395448917 天前
dataset.py_0224_cursor
eureka·flink·etcd