Spring Cloud Eureka Server

單實例Eureka Server配置

1. 依賴pom.xml:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zws.cloud</groupId>
    <artifactId>Eurka-Server1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eurka-Server1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2. 配置application.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    # 下面兩行表明自己是一個EurekaServer服務
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: Eureka-Server

3. 啓動類

package com.zws.cloud.server;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

4. 訪問:

http://localhost:8761/

Eureka Server集羣配置

集羣配置只在配置文件上有區別

1.Server1

server:
  port: 8761

eureka:
  instance:
    hostname: peer1
  client:
    # 下面兩行表明自己是一個EurekaServer服務
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/

spring:
  application:
    name: Eureka-Server1

2.Server2

server:
  port: 8762

eureka:
  instance:
    hostname: peer2
  client:
    # 下面兩行表明自己是一個EurekaServer服務
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/

spring:
  application:
    name: Eureka-Server2

3.Server3

server:
  port: 8763

eureka:
  instance:
    hostname: peer3
  client:
    # 下面兩行表明自己是一個EurekaServer服務
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

spring:
  application:
    name: Eureka-Server3

4./etc/hosts配置

127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3

Eureka工作原理

1. 服務註冊

當Eureka Client向Eureka Server註冊時,Eureka Client提供自身的元數據,比如 IP地址、 端口、運行狀況指標的Url、主頁地址等信息。

2. 服務續約

Eureka Client在默認的情況下會每隔30秒發送一次心跳來進行服務續約。通過服務續約來告知Eureka Serve :該 Eureka Client 仍然可用,沒有出現故障。正常情況下,如果Eureka Server在90秒內沒有收到Eureka Client的心跳,Eureka Server會將Eureka Client 實例從註冊列表中刪除。注意:官網建議不要更改服務續約的間隔時間。

3. 獲取服務註冊列表信息

Eureka Client從Eureka Server獲取服務註冊表信息,並將其緩存在本地。Eureka Client 會使用服務註冊列表信息查找其他服務的信息,從而進行遠程調用。該註冊列表信息定時(每30秒)更新一次,每次返回註冊列表信息可能與Eureka Client 的緩存信息不同,Eureka Client會自己處理這些信息。如果由於某種原因導致註冊列表信.息不能及時匹配,Eureka Client 會重新獲取整個註冊表信息。 Eureka server 緩存了所有的服務註冊列表信息,並將整個註冊列表以及每個應用程序的信息進行了壓縮,壓縮內容和沒有壓縮的內容完全相同。Eureka Client和Eureka server可以使用Json和XML數據格式進行通信。在默認的情況下,Eureka client使用Json格式的方式來獲取服務註冊列表的信息。

4. 服務下線

Eureka client在程序關閉時可以向Eureka Server發送下線請求。發送請求後,實例信息將從 Eureka server的服務註冊列表中刪除。該下線請求不會自動完成,關閉時調用以下代碼:該客戶端的需要在程序 DiscoveryManager.getInstance().shutdowncomponent();

5. 服務剔除

在默認情況下,當 Eureka client 連續 90 秒沒有向 Eureka server 發送服務續約(即心跳),EurekaServer會將該服務實例從服務註冊列表刪除,即服務剔除。

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