服务注册中心-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
相关推荐
IT小辉同学1 天前
# Milvus v3.0-beta docker-compose 启动失败完整排错教程
docker·eureka·milvus
tryxr2 天前
Spring Cloud eureka
spring·spring cloud·eureka
BaiduPHP4 天前
docker异常断电,出现docker服务,启动失败
docker·容器·eureka
会飞的土拨鼠呀8 天前
Docker 容器维护手册
docker·容器·eureka
慕伏白8 天前
【慕伏白】Docker 常用指令
docker·容器·eureka
bin91538 天前
使用Docker安装github项目 源码中含有 docker-compose.yml
docker·eureka·github
吠品11 天前
PHP里取月份最后一天的几种方式
java·开发语言·eureka
十年磨一剑~12 天前
docker 为何每次 docker tag才能 docker push
docker·容器·eureka
Tian_Hang16 天前
Eclipse Ditto 物模型相关代码
java·运维·服务器·ide·eureka·eclipse
江畔柳前堤17 天前
第16章:docker企业级实战综合项目
运维·git·安全·docker·容器·eureka