Spring Cloud之服務發現

Eureka是Netflix開發的一個服務發現框架,本身是一個基於REST的服務,主要用於定位運行在AWS(Amazon Web Services)域中的中間層服務,以達到負載均衡和中間層服務故障轉移的目的。

Eureka包括兩大組件:

             服務端發現組件(Eureka Server):也被稱爲服務註冊中心,提供服務註冊功能。

             客戶端發現組件(Eureka Client)主要用於處理服務的註冊於發現。

使用Eureka註冊服務步驟:

1.創建服務端代碼

  a. 創建maven項目

  b.向pom.xml文件中添加eureka server依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>

c.在application.yml文件中 添加對應配置

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    #是否向服務註冊中心註冊自己
    register-with-eureka: false
    #是否檢索服務
    fetch-registry: false
    #服務註冊中心的配置內容,指定服務註冊中心的位置
    service-url:
        default Zone: http://${eureka.instance.hostname}:${server.port}/eureka/
  #關閉保護機制,確保註冊中心可以將不可用的實例正確刪除
  server:
    enable-self-preservation: true

  d.在啓動類中添加註解@EnableEurekaServer

2.創建客戶端服務代碼

   a. 創建maven項目

   b.向pom.xml文件中添加eureka server依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

   c.在application.yml文件中 添加對應配置

eureka:
  instance:
    #是否顯示服務ip地址
    prefer-ip-address: true
    #ip地址顯示格式
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  client:
     #註冊中心地址,注意default Zone需要手動輸出,沒有提醒
    service-url:
      default Zone: http://localhost:8761/eureka/

  d.在啓動類中添加註解@EnableEurekaClient

至此Eureka 服務端與客戶端服務發現已完成,先啓動服務端項目,再啓動客戶端項目,瀏覽器訪問http://localhost:8761/eureka/,即可看到Eureka的信息面板

    

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章