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

😀前言

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

🏠个人主页:晨犀主页

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

💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰

如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦😊

文章目录

SpringCloud Eureka 服务注册与发现

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

为什么需要集群Eureka Server

示意图

说明

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

需求分析/图解

示意图

搭建Eureka Server 集群

创建e-commerce-eureka-server-9002 微服务模块作为注册中心

创建步骤参考e-commerce-eureka-server-9001

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

修改pom.xml , 加入依赖
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
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
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
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
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. 加入内容:
tex 复制代码
#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

将member-service-provider-10000 注册到EurekaServer 集群(目前2 台)

修改resources/application.yml
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/

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

将member-service-consumer-80 注册到EurekaServer 集群(目前2 台)

修改resources/application.yml
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/

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

文章到这里就结束了,如果有什么疑问的地方请指出,诸大佬们一起来评论区一起讨论😁

希望能和诸大佬们一起努力,今后我们一起观看感谢您的阅读🍻

如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞

相关推荐
Rain的Java大神之路7 天前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
需要8267 天前
MySQL 索引 B+ 树与“查询为什么变慢了
java·spring boot·spring·spring cloud·tomcat
需要8267 天前
Arthas 一个命令排查线上问题
java·jvm·spring·spring cloud
苏渡苇8 天前
Spring Insight 里如何把 Span 画成瀑布时间线
后端·spring·spring cloud·springboot·监控·apm
叶总没有会8 天前
Nacos—注册中心
spring cloud·微服务
苏渡苇9 天前
Spring Insight 里如何把 Span 收成一条链路
spring boot·spring·spring cloud·系统监控·apm
叶总没有会9 天前
微服务--分布式基础
java·spring·spring cloud·微服务
孙启超10 天前
【AI开发之Rust】第 7 课:错误处理 —— panic、Result 与 `?`
人工智能·分布式·后端·爬虫·spring cloud·架构·rust
行百里er11 天前
Spring Insight 里如何把 Span 收成一条链路
spring boot·spring cloud·监控
砍材农夫11 天前
spring|spring web|拦截器、过滤器、aop
java·spring boot·spring·spring cloud