创建eureka集群

springcloud-eureka-7002

image.png
配置pom.xml文件

  1. <?xml version="1.0"?>
  2. <project
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <modelVersion>4.0.0</modelVersion>
  7. <parent>
  8. <groupId>com.junjay</groupId>
  9. <artifactId>SpringCloud</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <groupId>com.junjay</groupId>
  13. <artifactId>springcloud-eureka-7002</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>springcloud-eureka-7002</name>
  16. <url>http://maven.apache.org</url>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>3.8.1</version>
  25. <scope>test</scope>
  26. </dependency>
  27. <!-- 导包eureka -->
  28. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  32. <version>1.4.6.RELEASE</version>
  33. </dependency>
  34. <!-- 热部署工具 -->
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-devtools</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.mybatis.spring.boot</groupId>
  41. <artifactId>mybatis-spring-boot-starter</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-web</artifactId>
  46. </dependency>
  47. </dependencies>
  48. </project>

添加yml配置文件

  1. server:
  2. port: 7002
  3. #Eureka 配置
  4. eureka:
  5. instance:
  6. hostname: localhost #首先声明eureka服务端名字
  7. client:
  8. register-with-eureka: false #是否向eureka中心注册自己
  9. fetch-registry: false #如果fetch-registry为false,则表示自己是注册中心
  10. service-url: #监控页面
  11. #使用${}动态获取yml配置文件中值
  12. #defaultZone 默认http://localhost:8761/eureka
  13. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

添加springboot启动类

  1. package com.junjay.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  6. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  7. @SpringBootApplication
  8. //开启启动eureka,注解肯定是enable+eureka。。。
  9. @EnableEurekaServer // @EnableEurekaServer服务的启动类,可以接收别人注册进来
  10. //在Spring
  11. // boot的启动引导类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}),阻止Spring
  12. // boot自动注入dataSource
  13. @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
  14. public class EurekaService_7002 {
  15. public static void main(String[] args) {
  16. SpringApplication.run(EurekaService_7002.class, args);
  17. }
  18. }

springcloud-eureka-7003

image.png
配置pom.xml文件

  1. <?xml version="1.0"?>
  2. <project
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <modelVersion>4.0.0</modelVersion>
  7. <parent>
  8. <groupId>com.junjay</groupId>
  9. <artifactId>SpringCloud</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <groupId>com.junjay</groupId>
  13. <artifactId>springcloud-eureka-7002</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>springcloud-eureka-7002</name>
  16. <url>http://maven.apache.org</url>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <version>3.8.1</version>
  25. <scope>test</scope>
  26. </dependency>
  27. <!-- 导包eureka -->
  28. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-eureka-server</artifactId>
  32. <version>1.4.6.RELEASE</version>
  33. </dependency>
  34. <!-- 热部署工具 -->
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-devtools</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.mybatis.spring.boot</groupId>
  41. <artifactId>mybatis-spring-boot-starter</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-starter-web</artifactId>
  46. </dependency>
  47. </dependencies>
  48. </project>

添加yml配置文件

  1. server:
  2. port: 7003
  3. #Eureka 配置
  4. eureka:
  5. instance:
  6. hostname: localhost #首先声明eureka服务端名字
  7. client:
  8. register-with-eureka: false #是否向eureka中心注册自己
  9. fetch-registry: false #如果fetch-registry为false,则表示自己是注册中心
  10. service-url: #监控页面
  11. #使用${}动态获取yml配置文件中值
  12. #defaultZone 默认http://localhost:8761/eureka
  13. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

添加springboot启动类

  1. package com.junjay.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  6. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  7. @SpringBootApplication
  8. //开启启动eureka,注解肯定是enable+eureka。。。
  9. @EnableEurekaServer // @EnableEurekaServer服务的启动类,可以接收别人注册进来
  10. //在Spring
  11. // boot的启动引导类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}),阻止Spring
  12. // boot自动注入dataSource
  13. @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
  14. public class EurekaService_7003 {
  15. public static void main(String[] args) {
  16. SpringApplication.run(EurekaService_7003.class, args);
  17. }
  18. }

eureka集群服务

image.png
7:Eureka集群环境配置 - 图4

eureka单机服务

  1. # 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  2. # 集群联机:defaultZone: 单机defaultZone地址1, 单机defaultZone地址2
  3. defaultZone: http://${eureka.instance.hostname}:7001/eureka/, http://${eureka.instance.hostname}:7002/eureka/

eureka集群联机

eureka服务:springcloud-eureka-7001

  1. defaultZone: http://${eureka.instance.hostname}:7002/eureka/,http://${eureka.instance.hostname}:7003/eureka/

eureka服务:springcloud-eureka-7002

  1. defaultZone: http://${eureka.instance.hostname}:7001/eureka/,http://${eureka.instance.hostname}:7003/eureka/

eureka服务:springcloud-eureka-7003

  1. defaultZone: http://${eureka.instance.hostname}:7002/eureka/,http://${eureka.instance.hostname}:7001/eureka/

不配服务本身,是因为本身服务配置在其他服务中了

服务提供者发布集群

  1. #eureka 的配置
  2. eureka:
  3. client:
  4. service-url:
  5. # 服务注册到哪里 打开eureka服务配置以配置中的地址为准
  6. # eureka配置好之后,开启eureka功能注解
  7. defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka

启动发现并没有集群
image.png
单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
修改配置后再启动服务,集群搭建完毕
image.png
image.png
启动服务提供者,使其注册在集群中
image.png
image.png
比如当其中一个eureka无法挂了,服务注册不影响可以切换其他端口上的eureka服务