方志朋 2019-12-21
上一篇文章讲解了如何搭建单个节点的eureka,这篇讲解如何搭建eureka集群,这里的集群还是本地不同的端口执行三个eureka,因为条件不要允许,没有三台电脑,所以大家将就一下吧,eureka集群的目的就是防止一个点故障导致,整个服务瘫痪的问题,成为单点故障,因为一个点出问题,还有另一个点顶上去,代替这个点工作,所以集群也实现了高可用、高性能
每一个注册点都配置有其它点的url,能够与其他server点进行数据的同步,当服务向一个点注册时,该店就会把该服务同步到其它注册点,当消费者需要消费时就会向注册中心询问具体的服务,注册中心就会向消费者提供具体的提供该服务的服务提供者的访问地址,最后实现服务的调用
这里方便就直接把所有的服务都泡在本地的win10环境中,本来真正模拟环境是要在linux的虚拟机上跑的,大家可以自行百度,原理都一样的,只不过跑在linux上需要打包发布,我就直接在win上操作了。首先本地跑多个服务配置一下,域名映射,在C:\Windows\System32\drivers\etc路径下的hosts文件中加入:
127.0.0.1 eureka3000.com
127.0.0.1 eureka3001.com
127.0.0.1 eureka3002.com
这里需要提醒的是,修改hosts文件是有权限的,具体设置权限的方法参考win10修改配置文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.org.ldc</groupId> <artifactId>eureka3000</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka3000</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 继承本项目的父工程 --> <parent> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> </parent> <!--引入eurekaserver 依赖--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
server: port: 3000 eureka: server: enable-self-preservation: false #关闭自我保护机制 eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000) instance: hostname: eureka3000.com client: serviceUrl: defaultZone: http://eureka3001.com:3001/eureka,http://eureka3002.com:3002/eureka #将自己当成服务注册到其他注册中心 spring: application: name: eureka-server-cluster #设置服务名
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.org.ldc</groupId> <artifactId>eureka3001</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka3001</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 继承本项目的父工程 --> <parent> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> </parent> <!--引入eurekaserver 依赖--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
server: port: 3001 eureka: server: enable-self-preservation: false #关闭自我保护机制 eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000) instance: hostname: eureka3001.com client: serviceUrl: defaultZone: http://eureka3000.com:3000/eureka,http://eureka3002.com:3002/eureka #将自己当成服务注册到其他注册中心 spring: application: name: eureka-server-cluster #设置服务名
启动类同上
eureka3002的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.org.ldc</groupId> <artifactId>eureka3002</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka3002</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 继承本项目的父工程 --> <parent> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> </parent> <!--引入eurekaserver 依赖--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
- eureka3002的application文件:
server:
port: 3002
eureka:
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 4000 #设置清理间隔(单位:毫秒 默认是60*1000)
instance:
hostname: eureka3002.com
client:
serviceUrl:
defaultZone: http://eureka3001.com:3001/eureka,http://eureka3000.com:3000/eureka #将自己当成服务注册到其他注册中心
spring:
application:
name: eureka-server-cluster #设置服务名
- 最后是工程的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--基本信息-->
<description>SpringBoot-Eureka环境搭建多模块构建示例</description>
<modelVersion>4.0.0</modelVersion>
<name>eureka-father</name>
<packaging>pom</packaging>
<!-- 项目说明:这里作为聚合工程的父工程 --> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> <!-- 继承说明:这里继承SpringBoot提供的父工程 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <modules> <module>eureka3000</module> <module>eureka3001</module> <module>eureka3002</module> <module>user5000</module> </modules> <dependencyManagement> <dependencies> <dependency> <groupId>com.org.ldc</groupId> <artifactId>eureka3000</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.org.ldc</groupId> <artifactId>eureka3001</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.org.ldc</groupId> <artifactId>eureka3002</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.org.ldc</groupId> <artifactId>user5000</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement>
</project>
在这里说明一下,这里的搭建教程是基于maven的聚合工程搭建的,需要有能搭建maven聚合工程的前提,这里就默认大家会搭建,所以就一顿输出了,如果还不懂的可以自行去百度。 最后测试就是启动各个注册中心,然后最后启动服务在浏览器分别输入各个注册中心的访问地址,出现如下图,说明搭建成功 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191215215951525.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzMjU1MDE3,size_16,color_FFFFFF,t_70) >更多的教程请关注非科班的科班,一起努力,加油干,路过的小伙伴帮我点个赞,谢谢大家 最后分享一波java的资源,资源包括java从入门到开发的全套视频,以及java的26个项目,资源比较大,大小大概是290g左右,链接容易失效,获取的方式是关注公众号:非科班的科班,让后回复:java项目即可获得,祝大家学习愉快