• SpringCloud的服务发现框架 — Eureka — 单机Eureka构建流程,以及服务注册功能实现


    目标:

    了解 单机Eureka构建流程,以及服务注册功能实现


    学习步骤:

    1、新建一个model,导入指定依赖
    2、 编写yaml文件 指定Eureka信息
    3、在主启动类上加 EnableEurekaServer 注解
    4、启动项目,访问你配置文件指定的路径
    5、注册一个服务模块到注册中心
    6、注册另一个服务模块到注册中心

    一、新建一个model,导入指定依赖

    		 <!-- 服务注册中心的服务端 eureka-server -->
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web  -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    二、编写yaml文件 指定Eureka信息

    server:
      port: 7001
    
    # 单机版本
    eureka:
      instance:
        hostname: localhost # eureka 服务端的实例名称
      client:
        register-with-eureka: false # 表示不向注册中心注册自己
        fetch-registry: false # 表示自己就是注册中心,职责是维护服务实例,并不需要去探索服务
        service-url:
          # 设置与 eureka server 交互的地址查询服务和注册服务都需要依赖这个地址
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    三、在主启动类上加EnableEurekaServer注解

    @EnableEurekaServer //设置为服务注册中心 EurekaServer
    
    • 1

    四、启动项目,访问你配置文件指定的路径

    在这里插入图片描述此时还没有实例注册进来

    五、注册一个服务模块到注册中心

    5-1、引入pom依赖 服务注册中心的客户端 eureka-client

     		<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4

    5-2、配置eureka需要的信息(application.yml)

    eureka:
      client:
        register-with-eureka: true  #是否向注册中心注册自己 默认为 true
        #是否从注册中心抓取已有的注册信息 默认true,单节点无所谓,集群必须设置为 true 才能配合ribbon使用负载均衡
        fetchRegistry: true
        service-url:
          # 设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
          defaultZone: http://localhost:7001/eureka   #单机版
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5-3、将该功能模块 注册到Eureka中 (启动类加注解)

    @EnableEurekaClient
    
    • 1

    运行 两个模块,结果如下

    在这里插入图片描述

    六、注册另一个服务模块到注册中心

    步骤和第五点相同,下面直接展示结果

    在这里插入图片描述

    总结:至此,我们的单体Eureka架构就算搭建完毕了~

    在这里插入图片描述

  • 相关阅读:
    java递归将list转换为树形结构
    32、rocketMq应用
    浏览器消息通知代码
    论文解读:为了数据中心的未来,存算这对CP竟然又离了
    2023-10-5一些笔试题(1)
    任意文件上传漏洞
    Go函数全景:从基础到高阶的深度探索
    [含毕业设计论文+PPT+源码等]springboot巧匠家装小程序+Java后台管理系统|前后分离VUE
    【JUC系列-04】精通Synchronized底层的实现原理
    Ubuntu 20.04 安装 flameshot
  • 原文地址:https://blog.csdn.net/csnz123123/article/details/125460215