spring cloud eureka 是 spring cloud 中的一個(gè)組件,它是基于 netflix eureka 做了二次封裝,主要是負(fù)責(zé)完成微服務(wù)框架中服務(wù)治理的功能。spring cloud通過為 eureka 增加了 spring boot 風(fēng)格的自動(dòng)化配置,我們只需要通過簡(jiǎn)單的引用依賴和注解就能讓 spring boot 夠?qū)⒌奈⒎?wù)應(yīng)用輕松的與 eureka 服務(wù)治理體系進(jìn)行整合。
服務(wù)治理
服務(wù)治理是微服務(wù)框架中最為核心和基礎(chǔ)的模塊,它主要是用來實(shí)現(xiàn)各個(gè)微服務(wù)實(shí)例的自動(dòng)化注冊(cè)與發(fā)現(xiàn)。
最初開始,可能構(gòu)建的微服務(wù)系統(tǒng)服務(wù)并不是很多,我們可以通過一些靜態(tài)配置來完成服務(wù)的調(diào)用。比如有兩個(gè)服務(wù) a 和 b,其中 a 服務(wù)需要調(diào)用 b 服務(wù)來完成一個(gè)業(yè)務(wù)操作時(shí),為了實(shí)現(xiàn) b 服務(wù)的高可用,無論我們采用服務(wù)端的復(fù)雜均衡,還是客戶端的負(fù)載均衡,我們都需要手工的來維護(hù)一份 b 的實(shí)例列表。但是隨著業(yè)務(wù)的發(fā)展,系統(tǒng)功能越來越復(fù)雜,相應(yīng)的微服務(wù)也是越來越多,這種靜態(tài)手工維護(hù)的難度會(huì)越來越高。
為了解決這種問題,產(chǎn)生了大量的服務(wù)治理的框架和產(chǎn)品。這些框架其實(shí)都是圍繞著服務(wù)注冊(cè)和服務(wù)發(fā)現(xiàn)機(jī)制來完成對(duì)微服務(wù)應(yīng)用實(shí)例的自動(dòng)化管理。
服務(wù)注冊(cè)
在服務(wù)治理框架中,通常都會(huì)構(gòu)建一個(gè)服務(wù)注冊(cè)中心,每個(gè)服務(wù)實(shí)例單元向注冊(cè)中心登記自己的服務(wù),將實(shí)例主機(jī)位置、端口號(hào)、版本號(hào)、通信協(xié)議等一系列附加信息告訴注冊(cè)中心,注冊(cè)中心按服務(wù)名分類組織服務(wù)清單。
服務(wù)發(fā)現(xiàn)
由于在服務(wù)治理框架下操作,服務(wù)間的通信與調(diào)用不再是通過指定具體的實(shí)例地址來實(shí)現(xiàn),而是通過向服務(wù)名發(fā)起請(qǐng)求調(diào)用實(shí)現(xiàn)。所以,服務(wù)調(diào)用方在調(diào)用服務(wù)提供方的接口時(shí),并不知道具體的服務(wù)實(shí)例位置。因此,需要先向注冊(cè)中心發(fā)起查詢請(qǐng)求,獲取實(shí)例清單,以實(shí)現(xiàn)對(duì)具體服務(wù)實(shí)例的訪問。
搭建 eureka 注冊(cè)中心服務(wù)
搭建單中心eureka
- spring cloud 版本:finchley.build-snapshot
- spring boot 版本:2.0.2.release
我們之后的所有開發(fā)都會(huì)基于以上版本進(jìn)行操作,需要注意的是:sring cloud 使用的是 snapshot 版,所以需要在 pom 文件中指定倉庫的地址。
首先,我們新建一個(gè) spring boot 工程,命名為:spring-cloud-eureka,并在 pom 中添加必要的依賴,具體 pom 文件如下:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
<?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.sagesource</groupid> <artifactid>spring-cloud-eureka</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>spring-cloud-eureka</name> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 2.0 . 2 .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> <spring-cloud.version>finchley.build-snapshot</spring-cloud.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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-snapshots</id> <name>spring snapshots</name> <url>https: //repo.spring.io/snapshot</url> <snapshots> <enabled> true </enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> </project> |
通過 @enableeurekaserver 注解啟動(dòng)一個(gè)服務(wù)注冊(cè)中心提供給其他應(yīng)用進(jìn)行對(duì)話。這一步非常簡(jiǎn)單,只需在一個(gè)普通的 spring boot 應(yīng)用中添加這個(gè)注解就能開啟此功能:
1
2
3
4
5
6
7
8
9
|
@springbootapplication @enableeurekaserver public class applicationeurekaserver { public static void main(string[] args) { springapplication.run(applicationeurekaserver. class , args); } } |
在默認(rèn)的配置下,注冊(cè)中心服務(wù)端也會(huì)將自己作為一個(gè)客戶端來注冊(cè)自身,一般情況下,我們需要禁用這個(gè)功能,修改后的 application.yml 如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# spring config spring: application: name: spring-boot-eureka # server config server: port: 9871 # eureka config eureka: instance: hostname: localhost client: register-with-eureka: false #不向注冊(cè)中心注冊(cè)自己 fetch-registry: false #不檢索服務(wù) service-url: defaultzone: http: //${eureka.instance.hostname}:${server.port}/eureka/ |
由于我們都是在本地運(yùn)行,為了后續(xù)的服務(wù)區(qū)分,我們將服務(wù)注冊(cè)中心的端口通過 server.port 設(shè)置運(yùn)行端口為:9871
在完成上述配置后,啟動(dòng)應(yīng)用并訪問 http://localhost:9871。可以看到如下頁面,其中 instance currently registered with eureka 的列表是空的,說明還沒有服務(wù)注冊(cè)到該注冊(cè)中心。
我想大家都注意到上面那兩行醒目的紅字:
emergency! eureka may be incorrectly claiming instances are up when they're not. renewals are lesser than threshold and hence the instances are not being expired just to be safe.
原因:自我保護(hù)機(jī)制。eureka server在運(yùn)行期間,會(huì)統(tǒng)計(jì)心跳失敗的比例在15分鐘之內(nèi)是否低于85%,如果出現(xiàn)低于的情況(在單機(jī)調(diào)試的時(shí)候很容易滿足,實(shí)際在生產(chǎn)環(huán)境上通常是由于網(wǎng)絡(luò)不穩(wěn)定導(dǎo)致),eureka server會(huì)將當(dāng)前的實(shí)例注冊(cè)信息保護(hù)起來,同時(shí)提示這個(gè)警告。
由于在單機(jī)情況下很容易出現(xiàn)該問題,按照網(wǎng)上的配置關(guān)閉掉自我保護(hù)后,eureka 仍會(huì)報(bào)警,提示安全模式關(guān)閉,無法保證實(shí)例正確性。所以,我們暫時(shí)忽略該問題,后期集群部署時(shí)即可解決。
注冊(cè)服務(wù)提供者
在完成了注冊(cè)中心服務(wù)的搭建后,接下來我們可以嘗試將一個(gè)既有的 spring boot 應(yīng)用加入到eureka 的服務(wù)治理體系中去。
我們?nèi)匀灰灾暗?spring-cloud-server 為例,我們只需要修改 application.yml以下配置:
1
2
3
4
5
6
7
8
9
10
|
# 應(yīng)用名稱 spring: application: name: spring-cloud-server # eureka 注冊(cè)中心位置 eureka: client: service-url: defaultzone: http: //localhost:9871/eureka/ |
修改完成后,即可啟動(dòng)服務(wù),這時(shí)我們?cè)谒⑿?eureka 的管理頁面,看到instance currently registered with eureka的列表信息如下:
這表明,我們的服務(wù)已經(jīng)成功注冊(cè)在注冊(cè)中心。
相關(guān)實(shí)例代碼:https://github.com/sagesource/spring-cloud-set
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000015243627