• 如何使用SpringCloud Eureka 创建单机Eureka Server-注册中心


    😀前言
    本篇博文是关于使用SpringCloud Eureka 创建单机Eureka Server-注册中心,希望你能够喜欢

    🏠个人主页晨犀主页
    🧑个人简介:大家好,我是晨犀,希望我的文章可以帮助到大家,您的满意是我的动力😉😉

    💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰
    如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦😊

    SpringCloud Eureka 服务注册与发现

    创建单机Eureka Server-注册中心

    需求说明/图解

    image-20230827073816506

    实现步骤

    创建Moduel & 完成配置
    创建e-commerce-eureka-server-9001 微服务模块[作为注册中心]

    模块创建步骤前面说过,这里不再说明。

    父工程的pom.xml-会做相应变化,管理e-commerce-eureka-server-9001 微服务子模块。

    image-20230827084802439

    修改e-commerce-eureka-server-9001 的pom.xml , 加入依赖
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>e-commerce-centerartifactId>
            <groupId>com.my.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>e-commerce-eureka-server-9001artifactId>
    
        
        
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
    
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            
            <dependency>
                <groupId>com.my.springcloudgroupId>
                <artifactId>e_commerce_center-common-apiartifactId>
                <version>${project.version}version>
            dependency>
        dependencies>
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    创建resources/application.yml
    server:
      port: 9001
    
    #配置eureka-server
    eureka:
      instance:
        hostname:localhost #服务实例名
      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
    • 14
    • 15
    创建主启动类com/my/springcloud/EurekaApplication.java
    //@EnableEurekaServer 表示该程序,作为Eureka Server
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaApplication9001 {
        public static void main(String[] args) {
            SpringApplication.run(EurekaApplication9001.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    完成测试
    浏览器: http://localhost:9001

    image-20230827085348484

    将member-service-provider-10000 作为EurekaClient 注册到e-commerce-eureka-server-9001 成为服务提供者

    架构示意图

    image-20230827085504610

    修改member-service-provider-10000 的pom.xml
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        
        <dependency>
            <groupId>com.my.springcloudgroupId>
            <artifactId>e_commerce_center-common-apiartifactId>
            <version>${project.version}version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    修改member-service-provider-10000 的resources/application.yml
    server:
      port: 10000
    
    spring:
      application:
        name: member-service-provider #配置应用的名称
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/e_commerce_center_db?useSSL=true&useUnicode=true&characterEncoding=UTF-8
        username: root
        password: 123456
        
    #配置eureka-client
    eureka:
      client:
        register-with-eureka: true #将自己注册到Eureka-Server
        #表示从Eureka-Server 抓取注册信息
        #如果是单节点,是可以不配置的,但是如果是一个集群,则必须配置true,
        #才能配合Ribbon使用负载均衡
        fetch-registry: true
        service-url:
          #表示将自己注册到哪个eureka-server
          defaultZone: http://localhost:9001/eureka   
          
    #配置mybatis
    mybatis:
      mapper-locations: classpath:mapper/*.xml #指定mapper.xml文件位置
      type-aliases-package: com.my.springcloud.entity # 实例类所在的包,这样通过类名就可以引用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    修改member-service-provider-10000 的com/my/springcloud/MemberApplication.java
    @SpringBootApplication
    //@EnableEurekaClient 将该程序标识为EurekaClient
    @EnableEurekaClient
    public class MemberApplication {
        public static void main(String[] args) {
        	SpringApplication.run(MemberApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    完成测试

    启动e-commerce-eureka-server-9001

    启动member-service-provider-10000

    浏览器: http://localhost:9001

    image-20230827090113415

    微服务注册名配置说明

    image-20230827090335835

    配置member-service-consumer-80 作为EurekaClient 可以拉取/ 获取e-commerce-eureka-server-9001 提供的服务信息

    架构示意图

    image-20230827090411064

    修改pom.xml
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>e-commerce-centerartifactId>
            <groupId>com.my.springcloudgroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>member-service-consumer-80artifactId>
    
    
        
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-zipkinartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
            dependency>
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
    
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
    
    
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
    
            
            <dependency>
                <groupId>com.my.springcloudgroupId>
                <artifactId>e_commerce_center-common-apiartifactId>
                <version>${project.version}version>
            dependency>
    
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    修改application.yml
    server:
      port: 80
    
    spring:
      application:
        name: member-service-consumer-80
    
    #配置eureka-client
    eureka:
      client:
        register-with-eureka: true #将自己注册到Eureka-Server
        fetch-registry: true  #配置从EurekaServer 抓取其它服务注册信息
        service-url:
          #表示将自己注册到哪个eureka-server
          defaultZone: http://localhost:9001/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    修改MemberConsumerApplication.java
    //排除DataSourceAutoConfiguration 自动配置
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    //@EnableEurekaClient 将该程序标识为EurekaClient
    @EnableEurekaClient
    public class MemberConsumerApplication {
        public static void main(String[] args) {
        	SpringApplication.run(MemberConsumerApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    完成测试

    启动e-commerce-eureka-server-9001

    启动member-service-consumer-80

    浏览器: http://localhost:9001

    image-20230827091058819

    Service Consumer 、Service Provider 、EurekaServer 的维护机制

    示意图

    image-20230827091156802

    Eureka 自我保护模式

    自我保护模式理论
    1. 在默认情况下, Eureka 启动了自我保护模式(如图红字, 需要刷新页面, 可以看到)

    image-20230827091859978

    2.自我保证机制/模式说明

    ​ 1)默认情况下EurekaClient定时向EurekaServer端发送心跳包.

    ​ 2)如果Eureka在server端在一定时间内(默认90秒)没有收到EurekaClient发送心跳包,便会直接从服务注册列表中剔除该服务.

    ​ 3)如果Eureka 开启了自我保护模式/机制, 那么在短时间(90秒中)内丢失了大量的服务实例心跳,这时候EurekaServer会开启自我保护机制,不会剔除该服务(该现象可能出现在如果网络不通或者阻塞) 因为客户端还能正常发送心跳,只是网络延迟问题,而保护机制是为了解决此问题而产生的.

    3.自我保护是属于CAP 里面的AP 分支, 保证高可用和分区容错性

    4.自我保护模式是—种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式, 可以让Eureka 集群更加的健壮、稳定。

    参考:https://blog.csdn.net/wangliangluang/article/details/120626014

    5.测试

    启动member-service-provider-10000 和e-commerce-eureka-server-9001,让member-service-provider-10000 正确的注册,然后关闭member-service-provider-10000,观察注册的member-service-provider-10000 服务是否还在.

    image-20230827092817220

    禁用自我保护模式(生产环境中, 一般不禁用)
    1. 说修改e-commerce-eureka-server-9001 的application.yml

    image-20230827092334125

    1. 修改member-service-provider-10000 的application.yml

    image-20230827092543783

    1. 启动e-commerce-eureka-server-9001 和member-service-provider-10000
    2. 在member-service-provider-10000 注册成功后,再关闭, 看看eureka server服务注册信息的变化

    image-20230827092608724

    image-20230827092837679

    提醒:测试完毕后,别忘了恢复原状,启用自我保护

    文章到这里就结束了,如果有什么疑问的地方请指出,诸大佬们一起来评论区一起讨论😁
    希望能和诸大佬们一起努力,今后我们一起观看感谢您的阅读🍻
    如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞

  • 相关阅读:
    恢复Windows 11经典右键菜单:一条命令解决显示更多选项问题
    rem 实现自应用屏幕大小
    vue中调用高德地图
    kafka学习-生产者
    猫罐头买什么牌子的?宠物店最受欢迎的5款猫罐头推荐!
    6.824 lab2
    阿里云服务器登录、安装MySql、配置Python、GO环境
    解决 clickhouse jdbc 偶现 failed to respond 问题
    MFC保存窗口客户区为图片
    WPF分享一个登录界面设计
  • 原文地址:https://blog.csdn.net/m0_73557631/article/details/132725625