搭建高可用的Eureka註冊中心

搭建高可用的Eureka註冊中心

1、搭建高可用的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等(這個後面都會講到)進行處理。java

2、第一個Eureka搭建

  1. 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>
  2. 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/
  3. 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 }  

 

3、第二個Eureka搭建

  1. 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>
  2. 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/
  3. 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 }

4、IP配置以及注意事項

  1. IP配置
    1. windows配置:/windows/system32/drivers/etc/hosts
    2. Linux配置:/etc/hosts
    3. 如圖所示:
       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
  2. 注意事項:當項目啓動時會報錯,這是正常現象,由於eureka-server-001要註冊到eureka-server-002上面,可是eureka-server-002項目尚未啓動起來,因此會報錯

 

5、測試分別訪問http://localhost:8080/eureka/和http://localhost:8081/eureka/web

相關文章
相關標籤/搜索