Eureka作為服務注冊與發現的組件,Eureka2.0已經閉源了,但是本教程還是以Eureka為核心進行展開,
1、三個模塊
Spring Cloud Eureka是Spring Cloud Netflix微服務套件之一,基于Netflix Eureka做了二次封裝,主要負責完成微服務架構中的服務治理功能,
eueka的3個重要模塊,eureka-server,service-provider,service-consumer
eureka-server:服務端,提供服務注冊和發現;
eureka-client-service-provider:服務端,服務提供者,通過http rest告知服務端注冊,更新,取消服務;
eureka-client-service-consumer:客戶端,服務消費者,通過http rest從服務端獲取需要服務的地址串列,然后配合一些負載均衡策略(ribbon)來呼叫服務端服務,
2、eureka-server
Eureka Server 的服務注冊資料存盤層是雙層的 ConcurrentHashMap(執行緒安全高效的 Map 集合),
第一層的key=spring.application.name 也就是客戶端實體注冊的應用名;value 為嵌套的 ConcurrentHashMap,
第二層的key=instanceId 也就是服務的唯一實體 ID,value 為 Lease 物件,Lease 物件存盤著這個實體的所有注冊資訊,包括 ip 、埠、屬性等,
申明陳述句如下:
private final ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>> registry= new ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>();
服務注冊表沒有持久化到資料庫,我想應該是出于性能的考慮吧,畢竟,注冊表資訊是需要定時獲取、更新的,
3、創建服務注冊中心——demo
3.1、引入依賴pom.xml
<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>
3.2、eureka server啟動代碼
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
@EnableEurekaServer的主要作用是啟動EurekaServer運行環境和背景關系,
3.3、application組態檔
application組態檔可以是xml或yml結構,我比較喜歡xml結構,yml是縮進格式,我覺得比較容易寫錯,
server.port=8080
spring.application.name: eureka-server
#服務注冊中心實體的主機名
eureka.instance.hostname: localhost
#表示是否將自己注冊在EurekaServer上,默認為true,由于當前應用就是EurekaServer,所以置為false
eureka.client.register-with-eureka: false
#表示表示是否從EurekaServer獲取注冊資訊,默認為true,單節點不需要同步其他的EurekaServer節點的資料
eureka.client.fetch-registry: false
#設定Eureka的地址
eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.4、查看eureka server
訪問http://localhost:8080/地址,如圖上部分

Environment: 環境,默認為test,生產環境建議改下,看著順眼
Data center: 資料中心,生產環境建議改下
Current time:當前的系統時間
Uptime:已經運行了多少時間
Lease expiration enabled:是否啟用租約過期 ,自我保護機制關閉時,該值默認是true, 自我保護機制開啟之后為false,
Renews threshold: 每分鐘最少續約數,Eureka Server 期望每分鐘收到客戶端實體續約的總數,
Renews (last min): 最后一分鐘的續約數量(不含當前,1分鐘更新一次),Eureka Server 最后 1 分鐘收到客戶端實體續約的總數,
頁面下部分:

total-avail-memory : 總共可用的記憶體
environment : 環境名稱,默認test
num-of-cpus : CPU的個數
current-memory-usage : 當前已經使用記憶體的百分比
server-uptime : 服務啟動時間
registered-replicas : 相鄰集群復制節點
unavailable-replicas :不可用的集群復制節點,
available-replicas :可用的相鄰集群復制節點
ipAddr:eureka服務端IP
status:eureka服務端狀態
3.5、源代碼鏈接:https://files-cdn.cnblogs.com/files/wreading/eureka-server.rar
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/39902.html
標籤:架構設計
