十五:Spring Cloud 之Eureka服務註冊中心(HA版)

1. Eureka簡介

二:Spring Cloud 之Eureka服務註冊中心(單機版) 記錄了eureka的單機版,這裏通過eureka自帶的特性實現HA版本。

2. 代碼實現

2.1涉及的模塊

  • eureka-server-ha:通過profiles指定不同的端口來模擬多服務實例。
  • eureka-service:服務提供者

2.2 源代碼

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.3 eureka-server-ha

2.3.1整體步驟

  1. 利用eureka-server相互註冊功能來實現eureka的HA,所以代碼很簡單
  2. 使用8781、8782、8783三個端口來啓動eureka-server使用8781、8782、8783三個端口來啓動eureka-server
  3. 每個profile指定當前eureka服務的eureka-server地址是另外兩個eureka服務
  4. 啓動三個服務,觀察eureka信息界面

2.3.2 pom文件添加依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server-ha</artifactId>

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

2.3.3 application-peer8781.yml

指定eureka-server相關設置,並且把自己註冊另外兩個服務中。
spring:
  application:
    name: eureka-server-ha

server:
  port: 8781

eureka:
  instance:
    hostname: peer8781
  client:
    serviceUrl:
      defaultZone: http://peer8782:8782/eureka/,http://peer8783:8783/eureka/ #Register self to another two eureka server

2.3.4 application-peer8782.yml

指定eureka-server相關設置,並且把自己註冊另外兩個服務中。
spring:
  application:
    name: eureka-server-ha

server:
  port: 8782

eureka:
  instance:
    hostname: peer8782
  client:
    serviceUrl:
      defaultZone: http://peer8781:8781/eureka/,http://peer8783:8783/eureka/ #Register self to another two eureka server

2.3.5 application-peer8783.yml

指定eureka-server相關設置,並且把自己註冊另外兩個服務中。
spring:
  application:
    name: eureka-server-ha

server:
  port: 8783

eureka:
  instance:
    hostname: peer8783
  client:
    serviceUrl:
      defaultZone: http://peer8782:8782/eureka/,http://peer8781:8781/eureka/ #Register self to another two eureka server

2.3.6 EurekaServerHaApplication

package org.oscar.scd.eureka.server.ha;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerHaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerHaApplication.class, args);
    }
}

3. 驗證

3.1 創建SpringBoot啓動類

3.1.1 EurekaServerHaApplication-peer8783

3.1.2 EurekaServerHaApplication-peer8782

與上一步基本相同,只是將Active profiles修改成:peer8782

3.1.3 EurekaServerHaApplication-peer8781

與上一步基本相同,只是將Active profiles修改成:peer8781

3.2 將一下信息添加到hosts文件中

  • windows系統文件位置:C:\Windows\System32\drivers\etc\hosts
  • linux系統文件位置:`/etc/hosts
127.0.0.1 peer8781
127.0.0.1 peer8782
127.0.0.1 peer8783

3.3 啓動SpringBoot啓動類

  • EurekaServerHaApplication-peer8781
  • EurekaServerHaApplication-peer8782
  • EurekaServerHaApplication-peer8783

3.4 訪問註冊中心

訪問任一eureka-server地址,如:http://localhost:8781/  可以查看eureka-server已經啓動,並且其他兩個eureka-server已經註冊:

在這裏插入圖片描述

4. 思考

  • 是否支持zookeeper
  • 服務的自動註冊與註銷由哪些配置可控,如檢測時間
  • 如何保持心跳連接

5. 補充

5.1 資料

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-eureka-server.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-eureka-server.html#spring-cloud-eureka-server-zones-and-regions

二:Spring Cloud 之Eureka服務註冊中心(單機版)

四:對微服務所需的服務發現的理解

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