跟我學(xué)Spring Cloud(Finchley版)-05-服務(wù)注冊(cè)與服務(wù)發(fā)現(xiàn)-Eureka入門

本節(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

配套代碼

相關(guān)文章

本文鏈接跟我學(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)載、引用,但需署名作者且注明文章出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容