1. 程式人生 > >SpringCloud教程一:eureka註冊中心(Finchley版)

SpringCloud教程一:eureka註冊中心(Finchley版)

一、spring cloud簡介

本階段學習教程Spring Boot版本2.0.3.RELEASE,Spring Cloud版本為Finchley.RELEASE。

Finchley版本的官方文件如下: http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

spring cloud 為開發人員提供了快速構建分散式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件匯流排、全域性鎖、決策競選、分散式會話等等。它執行環境簡單,可以在開發人員的電腦上跑。另外說明spring cloud是基於springboot的,所以需要開發中對springboot有一定的瞭解,如果不瞭解的話可以看這篇文章:2小時學會springboot。另外對於“微服務架構” 不瞭解的話,可以通過搜尋引擎搜尋“微服務架構”瞭解下。

二、建立服務註冊中心

在這裡,我還是採用Eureka作為服務註冊與發現的元件,至於Consul 之後會出文章詳細介紹。

2.1 首先建立一個maven主工程。

首先建立一個主Maven工程,在其pom檔案引入依賴,spring Boot版本為2.0.3.RELEASE,Spring Cloud版本為Finchley.RELEASE。這個pom檔案作為父pom檔案,起到依賴版本控制的作用,其他module工程繼承該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     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.liumy</groupId>
 8     <artifactId>eureka-f-1</artifactId>
 9     <version>0.0.1-SNAPSHOT</version>
10     <packaging>pom</packaging>
11 
12     <name>eureka-f-1</name>
13     <description>Demo project for Spring Boot</description>
14 
15     <parent>
16         <groupId>org.springframework.boot</groupId>
17         <artifactId>spring-boot-starter-parent</artifactId>
18         <version>2.0.3.RELEASE</version>
19         <relativePath/>
20     </parent>
21 
22     <modules>
23         <module>eureka-server</module>
24         <module>eureka-client</module>
25     </modules>
26 
27     <properties>
28         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
30         <java.version>1.8</java.version>
31         <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
32     </properties>
33 
34     <dependencies>
35         <dependency>
36             <groupId>org.springframework.boot</groupId>
37             <artifactId>spring-boot-starter-test</artifactId>
38             <scope>test</scope>
39         </dependency>
40 
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-autoconfigure</artifactId>
44             <version>2.0.3.RELEASE</version>
45             <scope>compile</scope>
46         </dependency>
47     </dependencies>
48 
49     <dependencyManagement>
50         <dependencies>
51             <dependency>
52                 <groupId>org.springframework.cloud</groupId>
53                 <artifactId>spring-cloud-dependencies</artifactId>
54                 <version>${spring-cloud.version}</version>
55                 <type>pom</type>
56                 <scope>import</scope>
57             </dependency>
58         </dependencies>
59     </dependencyManagement>
60 
61     <build>
62         <plugins>
63             <plugin>
64                 <groupId>org.springframework.boot</groupId>
65                 <artifactId>spring-boot-maven-plugin</artifactId>
66             </plugin>
67         </plugins>
68     </build>
69 
70 </project>

2.2 然後建立2個model工程:一個model工程作為服務註冊中心,即Eureka Server,另一個作為Eureka Client。

下面以建立server為例子,詳細說明建立過程:

右鍵工程->建立model-> 選擇spring initialir 如下圖:

 

 

 下一步->選擇cloud discovery->eureka server ,然後一直下一步就行了。

 

 

 建立完後的工程,其pom.xml繼承了父pom檔案,並引入spring-cloud-starter-netflix-eureka-server的依賴,程式碼如下:

 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     <parent>
 6         <groupId>com.liumy</groupId>
 7         <artifactId>eureka-f-1</artifactId>
 8         <version>0.0.1-SNAPSHOT</version>
 9     </parent>
10 
11     <groupId>com.liumy</groupId>
12     <artifactId>eureka-server</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <packaging>jar</packaging>
15 
16     <name>eureka-server</name>
17     <description>Demo project for Spring Boot</description>
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.cloud</groupId>
22             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
23         </dependency>
24     </dependencies>
25 
26 </project>

2.3 啟動一個服務註冊中心,只需要一個註解@EnableEurekaServer,這個註解需要在springboot工程的啟動application類上加:

 1 package com.liumy.eurekaserver;
 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 @EnableEurekaServer
 8 @SpringBootApplication
 9 public class EurekaServerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(EurekaServerApplication.class, args);
13     }
14 
15 }

2.4 yml配置檔案的配置 

eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳(因此可以在記憶體中完成),在預設情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置檔案appication.yml:

 1 server:
 2   port: 8881
 3 
 4 eureka:
 5   instance:
 6     hostname: localhost
 7   client:
 8     registerWithEureka: false
 9     fetchRegistry: false
10     serviceUrl:
11       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
12 
13 spring:
14   application:
15     name: eurka-server

通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server.

2.5 eureka server 是有介面的,啟動工程,開啟瀏覽器訪問: http://localhost:8761 ,介面如下:

 

 

 

 

 

 PS:No application available 沒有服務被發現 ……^_^ 因為沒有註冊服務當然不可能有服務被發現了。

三、建立一個服務提供者 (eureka client)

當client向server註冊時,它會提供一些元資料,例如主機和埠,URL,主頁等。Eureka server 從每個client例項接收心跳訊息。 如果心跳超時,則通常將該例項從註冊server中刪除。

建立過程同server類似,建立完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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.liumy</groupId>
 7         <artifactId>eureka-f-1</artifactId>
 8         <version>0.0.1-SNAPSHOT</version>
 9     </parent>
10 
11     <groupId>com.liumy</groupId>
12     <artifactId>eureka-client</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <packaging>jar</packaging>
15 
16     <name>eureka-client</name>
17     <description>Demo project for Spring Boot</description>
18 
19     <dependencies>
20         <dependency>
21             <groupId>org.springframework.cloud</groupId>
22             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
23         </dependency>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28     </dependencies>
29 
30     <build>
31         <plugins>
32             <plugin>
33                 <groupId>org.springframework.boot</groupId>
34                 <artifactId>spring-boot-maven-plugin</artifactId>
35             </plugin>
36         </plugins>
37     </build>
38 
39 </project>

通過註解@EnableEurekaClient 表明自己是一個eurekaclient.

 1 package com.liumy.eurekaclient;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 import org.springframework.web.bind.annotation.RequestParam;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 @EnableEurekaClient
11 @SpringBootApplication
12 @RestController
13 public class EurekaClientApplication {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(EurekaClientApplication.class, args);
17     }
18 
19     @Value("${server.port}")
20     String port;
21     public String Hello(@RequestParam(value = "name",defaultValue = "liumingyu")String name){
22         return "Hello ~"+name+",My Port is:"+port;
23     }
24 }

僅僅@EnableEurekaClient是不夠的,還需要在配置檔案中註明自己的服務註冊中心的地址,application.yml配置檔案如下:

 1 server:
 2   port: 8882
 3 
 4 spring:
 5   application:
 6     name: eureka-client
 7 
 8 
 9 eureka:
10   client:
11     serviceUrl:
12       defaultZone: http://localhost:8881/eureka/

需要指明spring.application.name,這個很重要,這在以後的服務與服務之間相互呼叫一般都是根據這個name 。 啟動工程,開啟http://localhost:8881,即eureka server 的網址:

 

 你會發現一個服務已經註冊在服務中了,這樣就成功註冊,服務名為EUREKA-CLIENT ,埠為8882

這時開啟 http://localhost:8882/hello?name=lmy,你會在瀏覽器上看到 :

四、參考資料

http://blog.csdn.net/forezp/article/details/69696915

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

 

注:本篇為原創文章,其中的過程與實現是參考某BAT頂級架構師的教學完成