创建eureka集群
springcloud-eureka-7002

配置pom.xml文件
<?xml version="1.0"?><projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.junjay</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.junjay</groupId><artifactId>springcloud-eureka-7002</artifactId><version>0.0.1-SNAPSHOT</version><name>springcloud-eureka-7002</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 导包eureka --><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.6.RELEASE</version></dependency><!-- 热部署工具 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>
添加yml配置文件
server:port: 7002#Eureka 配置eureka:instance:hostname: localhost #首先声明eureka服务端名字client:register-with-eureka: false #是否向eureka中心注册自己fetch-registry: false #如果fetch-registry为false,则表示自己是注册中心service-url: #监控页面#使用${}动态获取yml配置文件中值#defaultZone 默认http://localhost:8761/eurekadefaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
添加springboot启动类
package com.junjay.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication//开启启动eureka,注解肯定是enable+eureka。。。@EnableEurekaServer // @EnableEurekaServer服务的启动类,可以接收别人注册进来//在Spring// boot的启动引导类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}),阻止Spring// boot自动注入dataSource@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })public class EurekaService_7002 {public static void main(String[] args) {SpringApplication.run(EurekaService_7002.class, args);}}
springcloud-eureka-7003

配置pom.xml文件
<?xml version="1.0"?><projectxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.junjay</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.junjay</groupId><artifactId>springcloud-eureka-7002</artifactId><version>0.0.1-SNAPSHOT</version><name>springcloud-eureka-7002</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 导包eureka --><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.4.6.RELEASE</version></dependency><!-- 热部署工具 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>
添加yml配置文件
server:port: 7003#Eureka 配置eureka:instance:hostname: localhost #首先声明eureka服务端名字client:register-with-eureka: false #是否向eureka中心注册自己fetch-registry: false #如果fetch-registry为false,则表示自己是注册中心service-url: #监控页面#使用${}动态获取yml配置文件中值#defaultZone 默认http://localhost:8761/eurekadefaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
添加springboot启动类
package com.junjay.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication//开启启动eureka,注解肯定是enable+eureka。。。@EnableEurekaServer // @EnableEurekaServer服务的启动类,可以接收别人注册进来//在Spring// boot的启动引导类上增加@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}),阻止Spring// boot自动注入dataSource@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })public class EurekaService_7003 {public static void main(String[] args) {SpringApplication.run(EurekaService_7003.class, args);}}
eureka集群服务
eureka单机服务
# 单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/# 集群联机:defaultZone: 单机defaultZone地址1, 单机defaultZone地址2defaultZone: http://${eureka.instance.hostname}:7001/eureka/, http://${eureka.instance.hostname}:7002/eureka/
eureka集群联机
eureka服务:springcloud-eureka-7001
defaultZone: http://${eureka.instance.hostname}:7002/eureka/,http://${eureka.instance.hostname}:7003/eureka/
eureka服务:springcloud-eureka-7002
defaultZone: http://${eureka.instance.hostname}:7001/eureka/,http://${eureka.instance.hostname}:7003/eureka/
eureka服务:springcloud-eureka-7003
defaultZone: http://${eureka.instance.hostname}:7002/eureka/,http://${eureka.instance.hostname}:7001/eureka/
服务提供者发布集群
#eureka 的配置eureka:client:service-url:# 服务注册到哪里 打开eureka服务配置以配置中的地址为准# eureka配置好之后,开启eureka功能注解defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
启动发现并没有集群
单机的时候 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
修改配置后再启动服务,集群搭建完毕

启动服务提供者,使其注册在集群中

比如当其中一个eureka无法挂了,服务注册不影响可以切换其他端口上的eureka服务

