在互联网应用中,服务实例很少有单个的。
即使微服务消费者会缓存服务列表,但是如果EurekaServer只有一个实例,该实例挂掉,正好微服务消费者本地缓存列表中服务实例也不可用,那么这个时候整个系统都受影响。
在⽣产环境中,我们会配置Eureka Server集群实现⾼可⽤。Eureka Server集群之中的节点通过点对点(P2P)通信的⽅式共享服务注册表。我们开启两台 Eureka Server 以搭建集群。
在本地模拟多个电脑步骤:
1.在host文件中的添加配置
2.在文件中添加2个服务的名称:FrankCloudEurekaServerB、FrankCloudEurekaServerA
3.代码结构
4.2个服务端中的一个代码结构
5.FrankCloudEurekaServerApplication8761 中的代码结构
1 package com.frank.edu; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @SpringBootApplication 8 @EnableEurekaServer 9 public class FrankCloudEurekaServerApplication8761 { 10 public static void main(String[] args) { 11 SpringApplication.run(FrankCloudEurekaServerApplication8761.class,args); 12 } 13 }
6.配置项application.yml
1 server: 2 port: 8761 3 spring: 4 application: 5 name: frank-cloud-eureka-server 6 7 eureka: 8 instance: 9 hostname: FrankCloudEurekaServerA 10 client: 11 fetch-registry: true 12 register-with-eureka: true 13 serviceUrl: 14 defaultZone: http://FrankCloudEurekaServerB:8762/eureka
7.FrankCloudEurekaServerApplication8762中代码
1 package com.frank.edu; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @SpringBootApplication 8 @EnableEurekaServer 9 public class FrankCloudEurekaServerApplication8762 { 10 public static void main(String[] args) { 11 SpringApplication.run(FrankCloudEurekaServerApplication8762.class,args); 12 } 13 }
8.配置项application.yml
spring: application: name: frank-cloud-eureka-server server: port: 8762 eureka: instance: hostname: FrankCloudEurekaServerB client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://FrankCloudEurekaServerA:8761/eureka
9.分别启动2个服务,在浏览器中分别查看http://localhost:8761/;http://localhost:8762
10.父模块的pom导入
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <packaging>pom</packaging> 6 <modules> 7 <module>frank-cloud-eureka-server8761</module> 8 <module>frank-cloud-eureka-server8762</module> 9 <module>frank-cloud-oauth-server-9999</module> 10 <module>frank-service-resume-8080</module> 11 <module>frank-service-common</module> 12 <module>frank-service-autodeliver-9090</module> 13 </modules> 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.1.6.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 <groupId>com.frank.edu</groupId> 21 <artifactId>frank-parent</artifactId> 22 <version>0.0.1-SNAPSHOT</version> 23 <name>frank-parent</name> 24 <description>Demo project for Spring Boot</description> 25 <properties> 26 <java.version>1.8</java.version> 27 </properties> 28 <dependencyManagement> 29 <dependencies> 30 <dependency> 31 <groupId>org.springframework.cloud</groupId> 32 <artifactId>spring-cloud-dependencies</artifactId> 33 <version>Greenwich.RELEASE</version> 34 <type>pom</type> 35 <scope>import</scope> 36 </dependency> 37 </dependencies> 38 </dependencyManagement> 39 <dependencies> 40 <!--导入springCloud依赖--> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter</artifactId> 44 </dependency> 45 46 <dependency> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-starter-test</artifactId> 49 <scope>test</scope> 50 </dependency> 51 <!--引⼊Jaxb,开始--> 52 <dependency> 53 <groupId>com.sun.xml.bind</groupId> 54 <artifactId>jaxb-core</artifactId> 55 <version>2.2.11</version> 56 </dependency> 57 <dependency> 58 <groupId>javax.xml.bind</groupId> 59 <artifactId>jaxb-api</artifactId> 60 </dependency> 61 <dependency> 62 <groupId>com.sun.xml.bind</groupId> 63 <artifactId>jaxb-impl</artifactId> 64 <version>2.2.11</version> 65 </dependency> 66 <dependency> 67 <groupId>org.glassfish.jaxb</groupId> 68 <artifactId>jaxb-runtime</artifactId> 69 <version>2.2.10-b140310.1920</version> 70 </dependency> 71 <dependency> 72 <groupId>javax.activation</groupId> 73 <artifactId>activation</artifactId> 74 <version>1.1.1</version> 75 </dependency> 76 <!--引⼊Jaxb,结束--> 77 <dependency> 78 <groupId>org.springframework.boot</groupId> 79 <artifactId>spring-boot-starter-web</artifactId> 80 </dependency> 81 <dependency> 82 <groupId>org.springframework.boot</groupId> 83 <artifactId>spring-boot-starter-logging</artifactId> 84 </dependency> 85 <dependency> 86 <groupId>org.springframework.boot</groupId> 87 <artifactId>spring-boot-starter-test</artifactId> 88 <scope>test</scope> 89 </dependency> 90 <dependency> 91 <groupId>org.projectlombok</groupId> 92 <artifactId>lombok</artifactId> 93 <version>1.18.4</version> 94 <scope>provided</scope> 95 </dependency> 96 <dependency> 97 <groupId>org.springframework.boot</groupId> 98 <artifactId>spring-boot-starter-actuator</artifactId> 99 </dependency> 100 101 <dependency> 102 <groupId>org.springframework.boot</groupId> 103 <artifactId>spring-boot-devtools</artifactId> 104 <optional>true</optional> 105 </dependency> 106 107 108 </dependencies> 109 110 <build> 111 <plugins> 112 <!--编译插件--> 113 <plugin> 114 <groupId>org.apache.maven.plugins</groupId> 115 <artifactId>maven-compiler-plugin</artifactId> 116 <configuration> 117 <source>8</source> 118 <target>8</target> 119 <encoding>utf-8</encoding> 120 </configuration> 121 </plugin> 122 <!--打包插件--> 123 <plugin> 124 <groupId>org.springframework.boot</groupId> 125 <artifactId>spring-boot-maven-plugin</artifactId> 126 </plugin> 127 </plugins> 128 </build> 129 130 131 </project>
2个服务模块中的一个pom文件
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>frank-parent</artifactId> 7 <groupId>com.frank.edu</groupId> 8 <version>0.0.1-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>frank-cloud-eureka-server8761</artifactId> 13 <dependencies> 14 <!--Eureka server依赖--> 15 <dependency> 16 <groupId>org.springframework.cloud</groupId> 17 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 18 </dependency> 19 </dependencies> 20 21 22 </project>
11.代码随后附上git地址。。。。