Spring Cloud服務發現與服務註冊Eureka + Eureka Server的搭建
文章目錄
服務發現組件概述
Eureka概述
Eureka原理
Maven父子工程的搭建
Eureka Server的搭建
新建 Maven Module
添加spring-cloud-starter-eureka-server依賴
啟動類增加@EnableEurekaServer註解
配置文件配置Eureka等信息
啟動Eureka Server測試
Github 代碼
服務發現組件概述
上篇文章 Spring Cloud-01服務提供者與服務消費者 中,我們通過配置文件的方式來配置服務提供者的地址,有兩個缺點
如果服務提供者的ip和埠發生變化,將會影響服務消費者
通常情況下下,每個微服務都會部署多個實例,從而實現負載均衡和容災,可以動態增減節點
顯然通過硬編碼的方式無法滿足上述的需求。
在微服務中,服務發現組件的位置如下
關係如上圖
各個微服務在啟動時,將自己的網路地址等信息註冊到服務發現組件中
服務消費者可以從服務發現組件中查詢服務提供者的地址,並使用該地址調用服務提供者的提供的介面
各個微服務與服務發現組件在使用一定的機制(比如心跳機制)來通信。當服務發現組件長時間無法與某微服務實例通信,將註銷該實例
微服務網路地址發生變更(比如實例增減或者ip埠發生變化)時,會重新註冊到服務發現組件。 使用這種方式,服務消費者就無需人工修改提供者的網路地址了。
Spring Cloud支持Eureka 、Consul 、Zookeeper 等。
這裡我們主要介紹的Eureka
Eureka概述
Eureka是Netflix開源的服務發現組件,一個基於REST的服務。 包含Server和Client兩部分。 Spring Cloud將它集成在子項目Spring Cloud Netflix中,從而實現微服務的註冊與發現
https://github.com/Netflix/eureka
Eureka原理
官網上的圖 https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance
由上圖可知,Eureka包含 Eureka Server和 Eureka Client
Eureka Server: 提供服務發現的能力,各個微服務啟動時,會向Eureka Server註冊信息,比如ip、埠、微服務名稱等。 Eureka Server會存儲這些信息
Eureka Client: Java 客戶端,用於簡化與Eureka Server的交互
微服務啟動後,會周期性(默認30S)向Eureka Server發送心跳以續約自己的「租期」
如果Eureka Server在一定時間內(默認90S)沒有接收到某個微服務實例的心跳,Eureka Server將註銷該實例。
默認情況下,Eureka Server 同時也是 Eureka Client . 多個Eureka Server之間通過複製的方式來實現服務註冊表中數據的同步
Eureka Client會緩存服務註冊表中的信息,兩個好處 第一,微服務無需每次都請求查詢Eureka Server ,降低Server的壓力。 第二,即使Eureka Server所有節點都宕掉,服務消費者依然可以使用緩存中的信息找到服務提供者並完成調用。
Maven父子工程的搭建
這一步不是必須的,這裡我們為了方案管理依賴
首先我們將上篇博客中用到的provider和consumer中也放到父工程中去管理,簡單說下操作步驟
新建個maven 工程作為父工程,刪除多餘的文件和目錄,僅保留pom.xml即可。
在maven父工程上右鍵,選擇maven module,新建子模塊即可。
更多的父子工程信息, 請參考 http://www.cnblogs.com/telwanggs/p/7016561.html
如何搭建也可以參考 https://blog.csdn.net/NancyWu_LuckyGirl/article/details/80244589
父工程的pom.xml 如下
<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>
<!-- http://www.cnblogs.com/telwanggs/p/7016561.html -->
<groupId>com.artisan</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>microservice-spring-cloud</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<modules>
<module>micorservice-provider-user</module>
<module>micorservice-consumer-movie</module>
<module>microservice-discovery-eureka</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</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>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
http://spring.io/projects/spring-cloud#overview
注意Spring boot 和 Spring Cloud的對應關係,否則啟動可能會出錯。
Eureka Server的搭建
Finchley版本的官方指導手冊: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html
新建 Maven Module
在父工程 microservice-spring-cloud 上右鍵 New ,選擇
添加spring-cloud-starter-eureka-server依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
1
2
3
4
5
啟動類增加@EnableEurekaServer註解
package com.artisan.microservice.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String args[]) {
SpringApplication.run(EurekaApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
配置文件配置Eureka等信息
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
1
2
3
4
5
6
7
8
eureka.client.register-with-eureka: 是否將自己註冊到Eureka Server ,默認為true.因為當前應用是作為Eureka Server用,因此設置為false
eureka.client.fetch-registry:是否從Eureka Server獲取註冊信息,默認為true, 因為我們這裡是個單節點的Eureka Server ,不需要與其他的Eureka Server節點的數據,因此設為false
eureka.client.service-url.defaultZone : 設置與Eureka Server交互的地址,查詢服務和註冊服務都依賴這個地址,默認為 http://localhost:8761/eureka ,多個地址可使用 , 分隔。
啟動Eureka Server測試
訪問 http://localhost:8761/
Eureka Server 首頁展示的信息包括 當前實例的系統狀態、註冊到Eureka Server的服務實例、常用信息、實例信息等 。
我們這裡看到是空的
因為沒有微服務註冊上來,下面我們來看下如何將我們改造的用戶微服務和電影微服務註冊到Eureka Server上來吧。
Github 代碼
https://github.com/yangshangwei/SpringCloudMaster
原文:https://blog.csdn.net/yangshangwei/article/details/84716830
![](https://pic.pimg.tw/zzuyanan/1488615166-1259157397.png)
![](https://pic.pimg.tw/zzuyanan/1482887990-2595557020.jpg)
※重構大型業務型寫介面——並行處理注意點
※帶你看懂Spark2.x源碼之stage劃分
TAG:程序員小新人學習 |