下面直接就是集群版的Eureka服务搭建

项目名称:eureka-server7001

鼠标点击父工程项目右击 —> New —> Module
image-20200312115848949.png
image-20200312115959353.png
image-20200312120100893.png

1.项目结构

image-20200312120344815.png

2.eureka-server7001项目的完整pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>cloudDemo</artifactId>
  7. <groupId>com.cloud</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>eureka-server7001</artifactId>
  12. <dependencies>
  13. <!--eureka-server-->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17. </dependency>
  18. <!--boot web actuator-->
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-web</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <!--一般通用配置-->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <scope>runtime</scope>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.projectlombok</groupId>
  36. <artifactId>lombok</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>junit</groupId>
  45. <artifactId>junit</artifactId>
  46. </dependency>
  47. </dependencies>
  48. </project>

3.在window系统中修改hosts文件

路径:C:\Windows\System32\drivers\etc
image-20200313121138556.png

添加如下配置

127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com

4.添加application.yml配置文件

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com   #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false   #false表示自己端就是注册中心
    service-url:
      # defaultZone: http://eureka7001.com:7001/eureka/   #单机
      defaultZone: http://eureka7002.com:7002/eureka/  #集群
  #server:
    # 关闭自我保护机制,保证不可用服务被及时剔除
    #enable-self-preservation: false
    #eviction-interval-timer-in-ms: 2000

5.添加主启动类

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

6.搭建eureka-server7002项目作为Eureka第二个注册中心

参考步骤 【搭建EurekaServer端服务注册中心】创建一个eureka-server7002项目,主要修改如下内容:

eureka-server7002项目中的application.yml配置文件

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com   #eureka服务端的实例名称
  client:
    register-with-eureka: false   #false表示不向注册中心注册自己
    fetch-registry: false   #false表示自己端就是注册中心
    service-url:
      #单机就是7001自己
      #defaultZone: http://eureka7001.com:7001/eureka/   #单机
      #集群指向其它eureka
      defaultZone: http://eureka7002.com:7002/eureka/  #集群
  #server:
    # 关闭自我保护机制,默认是开启的,保证不可用服务被及时剔除
    #enable-self-preservation: false
    #eviction-interval-timer-in-ms: 2000

主启动类:

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