Springcloud 配置Eureka Server 服務端

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>
          <!--www.1b23.com-->
          <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啓動代碼    

       //www.1b23.com
       @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服務端狀態

發佈了15 篇原創文章 · 獲贊 0 · 訪問量 1212
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章