[toc]

作者:雨中散步撒哈拉
来源:https://liudongdong.top
公众号:雨中散步撒哈拉
备注:欢迎关注公众号,学习技术,一起成长!
吃水不忘挖井人:https://www.bilibili.com/video/BV18E411x7eT

零、问题

如果项目中,有且仅有一个Eureka注册中心,那当Eureka中心挂了,那整个项目环境就不可用!!
回答:高可用,搭建Eureka注册中心集群,实现负载均衡+故障容错
image.png

一、创建Eureka集群

1. 创建cloud_learn_eureka9002模块

image.png

2. 修改pom文件

<?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>cloud_learn</artifactId>
        <groupId>org.cloud.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud_learn_eureka9002</artifactId>


    <dependencies>
        <!-- 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>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>


        <dependency>
            <groupId>org.cloud.learn</groupId>
            <artifactId>cloud_learn_common</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

3. 创建application文件

server:
  port: 9002

eureka:
  instance:
    hostname: eureka9002.com  #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka9001.com:9001/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址

4. 修改集群cloud_learn_eureka9001模块配置

server:
  port: 9001

eureka:
  instance:
    hostname: eureka9001.com  #eureka服务端的实例名字
  client:
    register-with-eureka: false    #表识不向注册中心注册自己
    fetch-registry: false   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka9002.com:9002/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址

5. 创建主启动类

package org.learn.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain9002 {
  
  public static void main(String[] args){
    SpringApplication.run(EurekaMain9002.class, args);
  } 
}

6. 修改主机映射

image.png

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

image.png

7. 启动测试

访问:http://eureka9001.com:9001/
image.png

访问:http://eureka9002.com:9002/
image.png

获取源码

搜索并关注公众号:雨中散步撒哈拉
回复关键词:010

Q.E.D.


只有创造,才是真正的享受,只有拚搏,才是充实的生活。