spring cloud(學習筆記)高可用註冊中心(Eureka)的實現(一)

最近在學習的時候,發現微服務架構中,假如只有一個註冊中心,那這個註冊中心掛了可怎麼辦,這樣的系統,既不安全,穩定性也很差,網上和書上找了一會,發現這個spring cloud早就想到了,並幫咱們解決了!java

構建高可用的註冊中心

1.建立spring boot項目

建立的網站http://start.spring.io/,界面以下,建立兩個spring工程,一個做爲註冊中心,一個做爲測試客戶端,注意要導入(eureka-server),建立的界面以下web

導入的依賴的配置以下spring

 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>com.example</groupId>
 7     <artifactId>enreka-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>enreka-server</name>
12     <description>Demo project for Spring Boot</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         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
36         </dependency>
37 
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-test</artifactId>
41             <scope>test</scope>
42         </dependency>
43     </dependencies>
44 
45     <dependencyManagement>
46         <dependencies>
47             <dependency>
48                 <groupId>org.springframework.cloud</groupId>
49                 <artifactId>spring-cloud-dependencies</artifactId>
50                 <version>${spring-cloud.version}</version>
51                 <type>pom</type>
52                 <scope>import</scope>
53             </dependency>
54         </dependencies>
55     </dependencyManagement>
56 
57     <build>
58         <plugins>
59             <plugin>
60                 <groupId>org.springframework.boot</groupId>
61                 <artifactId>spring-boot-maven-plugin</artifactId>
62             </plugin>
63         </plugins>
64     </build>
65 
66 
67 </project>
View Code

2.在IEDA中用maven導入工程:

3.在spring boot工程的入口類中加入@EnableEurekaServer,標註好這是註冊中心;

4.在工程的resources文件夾中複製出兩個配置文件

配置文件application-peer1.properties中的內容爲apache

spring.application.name=eureka-server server.port=1112 eureka.instance.hostname=peer2 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
View Code

配置文件application-peer2.properties中的內容爲安全

spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=peer1 eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
View Code

5.點擊圖中位置配置啓動器

6.導入咱們的測試工程

7.配置客戶端

在入口類中加入@EnableDiscoveryClient註解,標註他爲客戶端,在配置文件中加入註冊中心的地址,內容以下架構

1 server.port=9001
2 spring.application.name=demoOne-service 3 eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

8.修改hosts文件

文件地址爲C:\Windows\System32\drivers\etc,若是直接打開修改回提示如圖app

咱們是不被容許修改這類型的文件的,注意它沒有後綴名,怎麼辦麼。教你們一個小技巧,這類型的系統文件,咱們能夠複製一份用記事本修改maven

在後面加入ide

127.0.0.1 peer1 127.0.0.1 peer2

而後保存,咱們能夠保存成.txt文件,而後刪除後綴名,替換原來的文件,就修改okspring-boot

9.分別啓動兩個註冊中心

均可以正常顯示;

10.咱們啓動客戶端驗證,咱們能夠在註冊中看到服務啓動成功,

11.假設出現故障,有一個註冊中心掛了,咱們來驗證一下

 

咱們關閉了一個註冊中心,咱們仍然能夠在第二個註冊中心看到,咱們的客戶端;

高可用註冊中心,實現成功。紅色部分爲spring cloud的心跳檢測。詳情可見下一篇博客。

 

歡迎你們關注公衆號,不定時乾貨,只作有價值的輸出

做者:Dawnzhang 
出處:https://www.cnblogs.com/clwydjgs/ 版權:本文版權歸做者轉載:歡迎轉載,但未經做者贊成,必須保留此段聲明;必須在文章中給出原文鏈接;不然必究法律責任

相關文章
相關標籤/搜索