搭建高可用的Eureka注册中心
搭建高可用的Eureka注册中心
一、搭建高可用的Eureka的作用
当服务器因种种原因导致Eureka注册中心(后面简称Eureka)服务当机(服务器跪了,异常关闭停止服务)。这样就会影响到整个业务的流程,因为你把所有的业务都注册到了Eureka中,当Eureka所依赖的docker(容器)当机了,这就会影响到所有在Eureka中注册的服务全部error。因为Eureka的请求流程在上一篇中说过,当A服务向B服务发送一个请求的时候,他是不会直接请求B服务,首先A服务先会到Eureka,Eureka拿到A服务请求的api,Eureka会通过这个api会去找在Eureka中注册的所有服务(这个过程叫做服务发现),找到服务之后,Eureka会返回给A一个服务列表(因为B服务可能会做负载均衡,就是会向Eureka注册两个B服务,这个过程叫做服务消费),当A服务拿到Eureka返回来的服务列表,根据自身的一些机制比如feign,ribbon等(这个后面都会讲到)进行处理。
二、第一个Eureka搭建
- pom.xml文件的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>cn.ds</groupId> 7 <artifactId>eureka-server-001</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>eureka-server-001</name> 12 <description>Eureka服务-001</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.3.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <!-- 引入Eureka服务 --> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-devtools</artifactId> 41 <scope>runtime</scope> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-test</artifactId> 46 <scope>test</scope> 47 </dependency> 48 </dependencies> 49 50 <dependencyManagement> 51 <dependencies> 52 <dependency> 53 <groupId>org.springframework.cloud</groupId> 54 <artifactId>spring-cloud-dependencies</artifactId> 55 <version>${spring-cloud.version}</version> 56 <type>pom</type> 57 <scope>import</scope> 58 </dependency> 59 </dependencies> 60 </dependencyManagement> 61 62 <build> 63 <plugins> 64 <plugin> 65 <groupId>org.springframework.boot</groupId> 66 <artifactId>spring-boot-maven-plugin</artifactId> 67 </plugin> 68 </plugins> 69 </build> 70 71 72 </project>
- application.properties配置:这里需要注意的是,IP地址不能写localhost或者127.0.0.1,后面会说到这个IP地址该怎么配置
1 # Eureka注册中心配置 2 # server-port:项目端口号 3 # spring-application-name:项目注册到Eureka显示的调用名称,类似于域名 4 # eureka.client.register-with-erueka:是否将自己注册到Eureka,默认为true 5 # eureka.client.fetch-registry:是否向Eureka获取注册信息,默认为true 6 # spring.jmx.default-domain:区分spring-boot项目 7 # eureka.instance.hostname:作为eureka-server-001服务配置中心 8 # eureka.client.service-url.defaultZone:双节点注册 9 server.port = 8080 10 spring.application.name = eureka-server 11 eureka.client.register-with-eureka = true 12 eureka.client.fetch-registry = true 13 spring.jmx.default-domain = erueka-server-001 14 eureka.instance.hostname = eureka-server-001 15 eureka.client.service-url.defaultZone = http://eureka-server-001:8080/eureka/,http://eureka-server-002:8081/eureka/
- springboot启动类配置
1 package cn.yuzhenzi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 /** 8 * @author 玉眞子 9 * @name springboot启动类 10 * */ 11 @SpringBootApplication 12 @EnableEurekaServer //启动注册中心 13 public class Application { 14 15 public static void main(String[] args) { 16 SpringApplication.run(Application.class, args); 17 } 18 }
三、第二个Eureka搭建
- pom.xml文件的配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>cn.ds</groupId> 7 <artifactId>eureka-server-002</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>eureka-server-002</name> 12 <description>Eureka服务-002</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.3.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <!-- 引入Eureka服务 --> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-devtools</artifactId> 41 <scope>runtime</scope> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-test</artifactId> 46 <scope>test</scope> 47 </dependency> 48 </dependencies> 49 50 <dependencyManagement> 51 <dependencies> 52 <dependency> 53 <groupId>org.springframework.cloud</groupId> 54 <artifactId>spring-cloud-dependencies</artifactId> 55 <version>${spring-cloud.version}</version> 56 <type>pom</type> 57 <scope>import</scope> 58 </dependency> 59 </dependencies> 60 </dependencyManagement> 61 62 <build> 63 <plugins> 64 <plugin> 65 <groupId>org.springframework.boot</groupId> 66 <artifactId>spring-boot-maven-plugin</artifactId> 67 </plugin> 68 </plugins> 69 </build> 70 71 72 </project>
- application.properties文件的配置:这里需要注意的是,IP地址不能写localhost或者127.0.0.1,后面会说到这个IP地址该怎么配置
1 # Eureka注册中心配置 2 # server-port:项目端口号 3 # spring-application-name:项目注册到Eureka显示的调用名称,类似于域名 4 # eureka.client.register-with-erueka:是否将自己注册到Eureka,默认为true 5 # eureka.client.fetch-registry:是否向Eureka获取注册信息,默认为true 6 # spring.jmx.default-domain:区分spring-boot项目 7 # eureka.instance.hostname:作为eureka-server-001服务配置中心 8 # eureka.client.service-url.defaultZone:双节点注册 9 server.port = 8081 10 spring.application.name = eureka-server 11 eureka.client.register-with-eureka = true 12 eureka.client.fetch-registry = true 13 spring.jmx.default-domain = eureka-server-002 14 eureka.instance.hostname = eureka-server-002 15 eureka.client.service-url.defaultZone = http://eureka-server-002:8081/eureka/,http://eureka-server-001:8080/eureka/
- springboot启动类的配置
1 package cn.yuzhenzi; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 /** 8 * @author 玉眞子 9 * @name springboot启动类 10 * */ 11 @SpringBootApplication 12 @EnableEurekaServer //启动注册中心 13 public class Application { 14 15 public static void main(String[] args) { 16 SpringApplication.run(Application.class, args); 17 } 18 }
四、IP配置以及注意事项
- IP配置
- windows配置:/windows/system32/drivers/etc/hosts
- Linux配置:/etc/hosts
- 如图所示:
1 # Copyright (c) 1993-2009 Microsoft Corp. 2 # 3 # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. 4 # 5 # This file contains the mappings of IP addresses to host names. Each 6 # entry should be kept on an individual line. The IP address should 7 # be placed in the first column followed by the corresponding host name. 8 # The IP address and the host name should be separated by at least one 9 # space. 10 # 11 # Additionally, comments (such as these) may be inserted on individual 12 # lines or following the machine name denoted by a '#' symbol. 13 # 14 # For example: 15 # 16 # 102.54.94.97 rhino.acme.com # source server 17 # 38.25.63.10 x.acme.com # x client host 18 19 # localhost name resolution is handled within DNS itself. 20 # 127.0.0.1 localhost 21 # ::1 localhost 22 127.0.0.1 eureka-server-001 23 127.0.0.1 eureka-server-002
- windows配置:/windows/system32/drivers/etc/hosts
- 注意事项:当项目启动时会报错,这是正常现象,因为eureka-server-001要注册到eureka-server-002上面,但是eureka-server-002项目还没有启动起来,所以会报错
五、测试分别访问http://localhost:8080/eureka/和http://localhost:8081/eureka/