使用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 , 加入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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-center</artifactId>
        <groupId>com.my.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>e-commerce-eureka-server-9002</artifactId>

    <!--引入相关的依赖: 如果有需要,可以调整-->
    <dependencies>
        <!--引入eureka-server 场景启动器starter: 使用版本仲裁-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--引入web-starter 说明我们使用版本仲裁(从父项目继承了版本)
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--说明:starter-actuator 是springboot程序的监控系统, 可以实现系统的健康检测
        可以通过http://localhost:9002/actuator 看到相关的连接,和信息
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--引入test-starter-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--引入e_commerce_center-common-api-->
        <dependency>
            <groupId>com.my.springcloud</groupId>
            <artifactId>e_commerce_center-common-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>
创建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/
创建主启动类EurekaApplication9002.java
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication9002 {
    public static void main(String[] args) {
    	SpringApplication.run(EurekaApplication9002.class, args);
    }
}
修改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/
修改主启动类名为EurekaApplication9001.java
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication9001 {
    public static void main(String[] args) {
   		SpringApplication.run(EurekaApplication9001.class, args);
    }
}
修改hosts 文件
  1. 文件: C:\Windows\System32\drivers\etc\host
  2. 文件可以先拷贝到桌面,修改后,再拷贝会去
  3. 加入内容:
#eureka 主机名和ip 映射
127.0.0.1 eureka9001.com
127.0.0.1 eureka9002.com
完成测试

启动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. 启动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. 启动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连支持一下,创造不易您们的支持是我的动力🤞

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨犀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值