本節(jié)講解基于Eureka的服務(wù)發(fā)現(xiàn)。
Eureka簡(jiǎn)介
Eureka是Netflix開源的服務(wù)發(fā)現(xiàn)組件,本身是一個(gè)基于REST的服務(wù),包含Server和Client兩部分,Spring Cloud將它集成在子項(xiàng)目Spring Cloud Netflix中。
拓展閱讀
Eureka的GitHub:https://github.com/Netflix/Eureka
Netflix是一家在線影片租賃提供商。
Eureka的典故:阿基米德發(fā)現(xiàn)浮力時(shí),非常開心,于是說(shuō):“Eureka!”意思是“我找到了!”。Netflix將它們的服務(wù)發(fā)現(xiàn)組件命名為Eureka實(shí)在是非常形象。
理解跟我學(xué)Spring Cloud(Finchley版)-04-服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)-原理剖析 所講的服務(wù)發(fā)現(xiàn)原理后,我們來(lái)編寫基于Eureka的服務(wù)發(fā)現(xiàn)——首先編寫一個(gè)Eureka Server,然后將前文的微服務(wù)都注冊(cè)到Eureka Server上。
編寫Eureka Server
- 加依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- 加注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
- 寫配置
server:
port: 8761
eureka:
client:
# 是否要注冊(cè)到其他Eureka Server實(shí)例
register-with-eureka: false
# 是否要從其他Eureka Server實(shí)例獲取數(shù)據(jù)
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
TIPS
這里,大家可先不去探究registerWithEureka 以及fetchRegistry 究竟是什么鬼,筆者將在下一節(jié)為大家揭曉。
將應(yīng)用注冊(cè)到Eureka Server上
- 加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 加注解
@SpringBootApplication
public class ProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderUserApplication.class, args);
}
}
**注意**:早期的版本(Dalston及更早版本)還需在啟動(dòng)類上添加注解 `@EnableDiscoveryClient`或`@EnableEurekaClient` ,從Edgware開始,該注解可省略。
- 添加配置:
spring:
application:
# 指定注冊(cè)到eureka server上的服務(wù)名稱,對(duì)于電影微服務(wù),本系列將名稱設(shè)為microservice-consumer-movie
name: microservice-provider-user
eureka:
client:
service-url:
# 指定eureka server通信地址,注意/eureka/小尾巴不能少
defaultZone: http://localhost:8761/eureka/
instance:
# 是否注冊(cè)IP到eureka server,如不指定或設(shè)為false,那就會(huì)注冊(cè)主機(jī)名到eureka server
prefer-ip-address: true
測(cè)試
依次啟動(dòng)Eureka Server以及用戶微服務(wù)、電影微服務(wù);
-
訪問
http://localhost:8761可觀察到類似如下界面:Eureka Server-01 -
將用戶微服務(wù)停止,可看到Eureka Server首頁(yè)變成類似如下界面:
Eureka Server-02
配套代碼
-
GitHub:
- Eureka Server:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-discovery-eureka
- microservice-provider-user:https://www.github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-provider-user
- microservice-consumer-movie:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie
-
Gitee:
- Eureka Server:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-discovery-eureka
- microservice-provider-user:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-provider-user
- microservice-consumer-movie:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie
相關(guān)文章
- 跟我學(xué)Spring Cloud(Finchley版)番外-01-Eureka安全詳解
- 跟我學(xué)Spring Cloud(Finchley版)-06-服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)-Eureka深入
- 跟我學(xué)Spring Cloud(Finchley版)-04-服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)-原理剖析
- 關(guān)于Eureka 2.x,別再人云亦云了!
- Spring Cloud Alibaba遷移指南1:零代碼從Eureka遷移到Nacos
本文鏈接:跟我學(xué)Spring Cloud(Finchley版)-05-服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)-Eureka入門
轉(zhuǎn)載聲明:本博客由周立創(chuàng)作,采用 CC BY 3.0 CN 許可協(xié)議??勺杂赊D(zhuǎn)載、引用,但需署名作者且注明文章出處。

