1. 程式人生 > >Spring Cloud 入門教程(一)服務註冊

Spring Cloud 入門教程(一)服務註冊

1.  什麼是Spring Cloud?

Spring提供了一系列工具,可以幫助開發人員迅速搭建分散式系統中的公共元件(比如:配置管理,服務發現,斷路器,智慧路由,微代理,控制匯流排,一次性令牌,全域性鎖,主節點選舉, 分散式session, 叢集狀態)。協調分散式環境中各個系統,為各類服務提供模板性配置。使用Spring Cloud, 開發人員可以搭建實現了這些樣板的應用,並且在任何分散式環境下都能工作得非常好,小到膝上型電腦, 大到資料中心和雲平臺。

Spring Cloud官網的定義比較抽象,我們可以從簡單的東西開始。Spring Cloud是基於Spring Boot的, 最適合用於管理Spring Boot建立的各個微服務應用。要管理分散式環境下的各個Spring Boot微服務,必然存在服務的註冊問題。所以我們先從服務的註冊談起。既然是註冊,必然有個管理註冊中心的伺服器,各個在Spring Cloud管理下的Spring Boot應用就是需要註冊的client

Spring Cloud使用erureka server,  然後所有需要訪問配置檔案的應用都作為一個erureka client註冊上去。eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳,在預設情況下erureka server也是一個eureka client ,必須要指定一個 server。

2.  建立Eureka Server

1).建立一個Maven工程helloworld.eureka.server, pom.xml內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lvpeng</groupId>
<artifactId>springcloud.helloworld.eureka.server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud.helloworld.Eureka.server</name>
<description>Demo Spring Eureka Server</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring boot test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>

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   <groupId>com.chry</groupId>
 6   <artifactId>springcloud.helloworld.eureka.server</artifactId>
 7   <version>0.0.1-SNAPSHOT</version>
 8   <packaging>jar</packaging>
 9   <name>springcloud.helloworld.Eureka.server</name>
10   <description>Demo Spring Eureka Server</description>
11 
12     <parent>
13         <groupId>org.springframework.boot</groupId>
14         <artifactId>spring-boot-starter-parent</artifactId>
15         <version>1.5.3.RELEASE</version>
16         <relativePath/> <!-- lookup parent from repository -->
17     </parent>
18 
19     <properties>
20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22         <java.version>1.8</java.version>
23     </properties>
24 
25     <dependencies>
26         <!--eureka server -->
27         <dependency>
28             <groupId>org.springframework.cloud</groupId>
29             <artifactId>spring-cloud-starter-eureka</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.cloud</groupId>
33             <artifactId>spring-cloud-starter-eureka-server</artifactId>
34         </dependency>
35         <dependency>
36             <groupId>org.springframework.cloud</groupId>
37             <artifactId>spring-cloud-starter-config</artifactId>
38         </dependency>
39         <!-- spring boot test-->
40         <dependency>
41             <groupId>org.springframework.boot</groupId>
42             <artifactId>spring-boot-starter-test</artifactId>
43             <scope>test</scope>
44         </dependency>
45     </dependencies>
46 
47     <dependencyManagement>
48         <dependencies>
49             <dependency>
50                 <groupId>org.springframework.cloud</groupId>
51                 <artifactId>spring-cloud-dependencies</artifactId>
52                 <version>Dalston.RC1</version>
53                 <type>pom</type>
54                 <scope>import</scope>
55             </dependency>
56         </dependencies>
57     </dependencyManagement>
58 
59     <build>
60         <plugins>
61             <plugin>
62                 <groupId>org.springframework.boot</groupId>
63                 <artifactId>spring-boot-maven-plugin</artifactId>
64             </plugin>
65         </plugins>
66     </build>
67 
68     <repositories>
69         <repository>
70             <id>spring-milestones</id>
71             <name>Spring Milestones</name>
72             <url>https://repo.spring.io/milestone</url>
73             <snapshots>
74                 <enabled>false</enabled>
75             </snapshots>
76         </repository>
77     </repositories>
78 
79 </project>
複製程式碼

 2). 用Spring Boot建立一個服務類EurekaServerApplication,需要一個註解@EnableEurekaServer加在springboot工程的啟動類上

複製程式碼
 1 package springcloud.helloworld.eureka.server;
 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 }
複製程式碼

3).eureka server的配置檔案appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一個eureka server

複製程式碼
 1 server:
 2    port: 8761
 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/
複製程式碼

4) eureka server的工程結構如下

 

5)啟動eureka server,然後訪問http://localhost:8761, 介面如下, "No instances available" 表示無client註冊

 

3.  建立Eureka Client

1). 建立一個Maven工程helloworld.eureka.client, pom.xml內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lvpeng</groupId>
<artifactId>springcloud.helloworld.eureka.client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud.helloworld.eureka.client</name>
<packaging>jar</packaging>
<description>Demo Spring Boot Client</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>


</project>

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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>com.chry</groupId>
 5     <artifactId>springcloud.helloworld.eureka.client</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <name>springcloud.helloworld.eureka.client</name>
 8     <packaging>jar</packaging>
 9     <description>Demo Spring Boot Client</description>
10 
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.5.3.RELEASE</version>
15         <relativePath/> <!-- lookup parent from repository -->
16     </parent>
17 
18     <properties>
19         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.cloud</groupId>
27             <artifactId>spring-cloud-starter-eureka</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>org.springframework.boot</groupId>
31             <artifactId>spring-boot-starter-web</artifactId>
32         </dependency>
33 
34         <dependency>
35             <groupId>org.springframework.boot</groupId>
36             <artifactId>spring-boot-starter-test</artifactId>
37             <scope>test</scope>
38         </dependency>
39     </dependencies>
40 
41     <dependencyManagement>
42         <dependencies>
43             <dependency>
44                 <groupId>org.springframework.cloud</groupId>
45                 <artifactId>spring-cloud-dependencies</artifactId>
46                 <version>Dalston.RC1</version>
47                 <type>pom</type>
48                 <scope>import</scope>
49             </dependency>
50         </dependencies>
51     </dependencyManagement>
52 
53     <build>
54         <plugins>
55             <plugin>
56                 <groupId>org.springframework.boot</groupId>
57                 <artifactId>spring-boot-maven-plugin</artifactId>
58             </plugin>
59         </plugins>
60     </build>
61 
62     <repositories>
63         <repository>
64             <id>spring-milestones</id>
65             <name>Spring Milestones</name>
66             <url>https://repo.spring.io/milestone</url>
67             <snapshots>
68                 <enabled>false</enabled>
69             </snapshots>
70         </repository>
71     </repositories>
72 
73 
74 </project>
複製程式碼

2).  建立主類EurekaClientApplication

使用@EnableEurekaClient註解表明是client

複製程式碼
 1 package springcloud.helloworld.eureka.client;
 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.RequestMapping;
 8 import org.springframework.web.bind.annotation.RequestParam;
 9 import org.springframework.web.bind.annotation.RestController;
10 
11 @SpringBootApplication
12 @EnableEurekaClient
13 @RestController
14 
15 public class EurekaClientApplication {
16 
17     public static void main(String[] args) {
18         SpringApplication.run(EurekaClientApplication.class, args);
19     }
20 
21     @Value("${server.port}")
22     String port;
23     @RequestMapping("/")
24     public String home() {
25         return "hello world from port " + port;26     }
27 
28 }
複製程式碼

3) eureka client的配置檔案application.yml

複製程式碼
1 eureka:
2     client:
3         serviceUrl:
4             defaultZone: http://localhost:8761/eureka/
5 server:
6     port: 8762
7 spring:
8     application:
9         name: service-helloworld
複製程式碼

4). Client啟動後, 可以訪問http://localhost:8762

 

5). 再次訪問伺服器埠, 可以看到Service Helloworld已經自動註冊到之前的server中

 

  • 參考資料:
    • Spring Cloud 官網
    • 部落格: http://blog.csdn.net/forezp/article/details/70148833