• 使用SpringCloud Eureka 搭建EurekaServer 集群- 实现负载均衡&故障容错【上】


    😀前言
    本篇博文是关于使用SpringCloud Eureka 搭建EurekaServer 集群- 实现负载均衡&故障容错,希望你能够喜欢

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

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

    SpringCloud Eureka 服务注册与发现

    搭建EurekaServer 集群- 实现负载均衡&故障容错

    为什么需要集群Eureka Server

    示意图

    image-20230827092956080

    说明
    1. 微服务RPC 远程服务调用最核心的是实现高可用
    2. 如果注册中心只有1 个,它出故障,会导致整个服务环境不可用
    3. 解决办法∶搭建Eureka 注册中心集群,实现负载均衡+故障容错

    需求分析/图解

    示意图

    image-20230827093048263

    搭建Eureka Server 集群

    创建e-commerce-eureka-server-9002 微服务模块[作为注册中心]
    创建步骤参考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-9002artifactId>
    
        
        <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: 9002
    
    #配置eureka-server
    eureka:
      instance:
        hostname: eureka9002.com #服务实例名
      client:
        #配置不向注册中心注册自己
        register-with-eureka: false
        #表示自己就是注册中心,作用就是维护注册服务实例, 不需要去检索服务
        fetch-registry: false
        service-url:
          #这里注册到eureka9001 server
          defaultZone: http://eureka9001.com:9001/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    创建主启动类EurekaApplication9002.java
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaApplication9002 {
        public static void main(String[] args) {
        	SpringApplication.run(EurekaApplication9002.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    修改e-commerce-eureka-server-9001 微服务模块
    修改resources/application.yml
    server:
      port: 9001
    
    #配置eureka-server
    eureka:
      instance:
        hostname: eureka9001.com #服务实例名
      client:
        #配置不向注册中心注册自己
        register-with-eureka: false
        #表示自己就是注册中心,作用就是维护注册服务实例, 不需要去检索服务
        fetch-registry: false
        service-url:
          #设置与eureka server 交互模块, 查询服务和注册服务都需要依赖这个地址
          #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          #相互注册,这里应该注册到eureka server9002
          defaultZone: http://eureka9002.com:9002/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    修改主启动类名为EurekaApplication9001.java
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaApplication9001 {
        public static void main(String[] args) {
       		SpringApplication.run(EurekaApplication9001.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    修改hosts 文件
    1. 文件: C:\Windows\System32\drivers\etc\host
    2. 文件可以先拷贝到桌面,修改后,再拷贝会去
    3. 加入内容:
    #eureka 主机名和ip 映射
    127.0.0.1 eureka9001.com
    127.0.0.1 eureka9002.com
    
    • 1
    • 2
    • 3
    完成测试

    启动e-commerce-eureka-server-9001

    启动e-commerce-eureka-server-9002

    浏览器: http://eureka9001.com:9001 浏览器: http://eureka9002.com:9002

    image-20230827093648980

    image-20230827093700041

    将member-service-provider-10000 注册到EurekaServer 集群(目前2 台)
    修改resources/application.yml
        #说明: 将defaultZone: http://localhost:9001/eureka 注销改成红色内容
    	service-url:
          # defaultZone: http://localhost:9001/eureka #表示将自己注册到哪个eurekaServer
          # 将本微服务注册到多个eurekaServer, 使用逗号隔开
          defaultZone: http://eureka9001.com:9001/eureka,http://eureka9002.com:9002/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    完成测试
    1. 启动e-commerce-eureka-server-9001 和e-commerce-eureka-server-9002
    2. 启动member-service-provider-10000
    3. 观察member-service-provider-10000 是否注册到Eureka 集群(目前2 台)

    浏览器输入: http://eureka9001.com:9001/

    image-20230827093918065

    浏览器输入: http://eureka9002.com:9002/

    image-20230827093948644

    将member-service-consumer-80 注册到EurekaServer 集群(目前2 台)
    修改resources/application.yml
        #说明: 将defaultZone: http://localhost:9001/eureka 注销改成红色内容
        service-url:
            # defaultZone: http://localhost:9001/eureka #表示将自己注册到哪个eurekaServer
            # 将本微服务注册到多个eurekaServer, 使用逗号隔开
            defaultZone: http://eureka9001.com:9001/eureka,
            http://eureka9002.com:9002/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    完成测试
    1. 启动e-commerce-eureka-server-9001 和e-commerce-eureka-server-9002
    2. 启动member-service-consumer-80
    3. 观察member-service-consumer-80 是否注册到Eureka 集群(目前2 台)

    浏览器输入: http://eureka9001.com:9001/

    image-20230827094326145

    浏览器输入: http://eureka9002.com:9002/

    image-20230827094342093

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

  • 相关阅读:
    Day699.Tomcat的Session管理机制 -深入拆解 Tomcat & Jetty
    为什么不是Github Copilot,不是 Devin 而是 AutoCoder
    柔性数组的使用及注意事项
    SpringBoot + kotlin 协程小记
    【NVIDIA CUDA】2023 CUDA夏令营编程模型(四)
    Langchain-Chatchat项目:5.1-ChatGLM3-6B工具调用
    码蹄集 - MT3029 - 新月轩就餐
    01-go基础-07-map(声明map、初始化map、map赋值、遍历map、判断key是否在map中、删除map成员)
    苹果iPhone手机发抖音短视频如何挂载词令抖音小程序?
    企业管理 - 现代管理學之父
  • 原文地址:https://blog.csdn.net/m0_73557631/article/details/132729388