springcloud~服務注冊與發現Eureka的使用
服(fu)務(wu)(wu)(wu)(wu)注冊(ce)與發現(xian)(xian)(xian)是微服(fu)務(wu)(wu)(wu)(wu)里的概念,也是一個基本的組(zu)件(jian),負責服(fu)務(wu)(wu)(wu)(wu)組(zu)件(jian)的認證,即實現(xian)(xian)(xian)『你(ni)是誰(shui)』的功能,在(zai)服(fu)務(wu)(wu)(wu)(wu)注冊(ce)與發現(xian)(xian)(xian)里,存在(zai)兩種模式,即服(fu)務(wu)(wu)(wu)(wu)端發現(xian)(xian)(xian)和客(ke)(ke)戶端發現(xian)(xian)(xian),咱們今天說(shuo)的eureka屬于客(ke)(ke)戶端發現(xian)(xian)(xian)!
下面概念來(lai)自文(wen)章://blog.csdn.net/forezp/article/details/73017664
下面看幾個服務注冊與發現的基本概念:
-
Register:服務注冊
當Eureka客戶端(duan)向Eureka Server注冊時,它提供(gong)自身的元數據,比如IP地址(zhi)、端(duan)口(kou),運行狀況指示符URL,主(zhu)頁等。 -
Renew:服務續約
Eureka客(ke)戶會(hui)每隔30秒(miao)發(fa)送一次(ci)心跳來續(xu)約(yue)。 通過續(xu)約(yue)來告知Eureka Server該(gai)Eureka客(ke)戶仍然存在(zai),沒(mei)有(you)出現(xian)問(wen)題。 正(zheng)常(chang)情況下,如果(guo)Eureka Server在(zai)90秒(miao)沒(mei)有(you)收到Eureka客(ke)戶的續(xu)約(yue),它會(hui)將實例從(cong)其注(zhu)冊表中刪(shan)除。 建議不(bu)要更改(gai)續(xu)約(yue)間隔。 -
Fetch Registries:獲取注冊列表信息
Eureka客(ke)(ke)(ke)戶(hu)(hu)端(duan)從(cong)服(fu)務(wu)器獲取注(zhu)(zhu)(zhu)冊(ce)表信(xin)(xin)(xin)息(xi),并將其緩存(cun)在本地(di)。客(ke)(ke)(ke)戶(hu)(hu)端(duan)會(hui)使用該信(xin)(xin)(xin)息(xi)查找其他服(fu)務(wu),從(cong)而(er)進(jin)(jin)行(xing)(xing)遠程(cheng)調用。該注(zhu)(zhu)(zhu)冊(ce)列(lie)表信(xin)(xin)(xin)息(xi)定(ding)期(每30秒(miao)鐘)更新(xin)一次。每次返回注(zhu)(zhu)(zhu)冊(ce)列(lie)表信(xin)(xin)(xin)息(xi)可(ke)能與Eureka客(ke)(ke)(ke)戶(hu)(hu)端(duan)的緩存(cun)信(xin)(xin)(xin)息(xi)不同(tong), Eureka客(ke)(ke)(ke)戶(hu)(hu)端(duan)自動處(chu)理。如果(guo)由于(yu)某種原(yuan)因(yin)導致注(zhu)(zhu)(zhu)冊(ce)列(lie)表信(xin)(xin)(xin)息(xi)不能及時匹配,Eureka客(ke)(ke)(ke)戶(hu)(hu)端(duan)則會(hui)重(zhong)新(xin)獲取整(zheng)個(ge)注(zhu)(zhu)(zhu)冊(ce)表信(xin)(xin)(xin)息(xi)。 Eureka服(fu)務(wu)器緩存(cun)注(zhu)(zhu)(zhu)冊(ce)列(lie)表信(xin)(xin)(xin)息(xi),整(zheng)個(ge)注(zhu)(zhu)(zhu)冊(ce)表以及每個(ge)應用程(cheng)序的信(xin)(xin)(xin)息(xi)進(jin)(jin)行(xing)(xing)了壓(ya)縮,壓(ya)縮內(nei)容(rong)和沒有壓(ya)縮的內(nei)容(rong)完全相同(tong)。Eureka客(ke)(ke)(ke)戶(hu)(hu)端(duan)和Eureka 服(fu)務(wu)器可(ke)以使用JSON / XML格(ge)式進(jin)(jin)行(xing)(xing)通訊。在默認的情(qing)況下(xia)Eureka客(ke)(ke)(ke)戶(hu)(hu)端(duan)使用壓(ya)縮JSON格(ge)式來獲取注(zhu)(zhu)(zhu)冊(ce)列(lie)表的信(xin)(xin)(xin)息(xi)。 -
Cancel:服務下線
Eureka客戶端在程序關閉時向Eureka服務器發送取消請求。 發送請求后,該客戶端實例信息將從服務器的實例注冊表中刪除。該下線請求不會自動完成,它需要調用以下內容:
DiscoveryManager.getInstance().shutdownComponent(); -
Eviction 服務剔除
在默認的情(qing)況下,當Eureka客戶端連(lian)續90秒(miao)沒有向Eureka服(fu)務(wu)(wu)(wu)器發(fa)送(song)服(fu)務(wu)(wu)(wu)續約,即心跳,Eureka服(fu)務(wu)(wu)(wu)器會將該服(fu)務(wu)(wu)(wu)實例從服(fu)務(wu)(wu)(wu)注(zhu)冊列表刪除,即服(fu)務(wu)(wu)(wu)剔除。
大叔(shu)演示服務(wu)發現(xian)與(yu)注冊的環境:
- intellij idea開發工具
- gradle4.6構建工具
- java1.8運行時
- springboot腳手架
Eureka Server端的實(shi)現:
依靠包
dependencies { compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') }
配置項
server.port: 8761 management.port: 8762 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8761/eureka/
啟動代碼
@EnableEurekaServer @EnableDiscoveryClient @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
訪問(wen)://localhost:8761/
Eureka Client端的加入(ru):
服務端啟動后,它里面的應用是空的,這時我們添加一個客戶端,注意,這里的客戶端是指Eureka的,它可能對于其它服務來說,是個服務端,為其它接口提供數據的,這是有可能的。(我這個(ge)(ge)客戶端同時引用了配(pei)置(zhi)(zhi)中(zhong)心的(de)概念,它(ta)也是配(pei)置(zhi)(zhi)中(zhong)心的(de)使用端,這個(ge)(ge)項目的(de)配(pei)置(zhi)(zhi)文件通(tong)過(guo)配(pei)置(zhi)(zhi)中(zhong)心來統(tong)一進(jin)行分布式管(guan)理)
依賴包
dependencies { compile('org.springframework.boot:spring-boot-starter-web', 'org.springframework.cloud:spring-cloud-starter-config', 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') }
配置項
spring: application: name: email-client cloud: config: uri: http://localhost:8200 server: port: 8300 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
啟(qi)動代碼
@EnableEurekaClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
我們啟動它之后,去(qu)eureka server界(jie)面去(qu)查看, //localhost:8761,你會發(fa)現,我們的服務出(chu)現在了服務列表(biao)里!

感謝各位的閱讀!
謝謝!