Eureka -- 入門案例(3)

父 pom

<packaging>pom</packaging>

<!-- 父工程是 2.1.0.RELEASE 版本的 boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- 設置全局 SpringCloud 依賴版本爲 F.RELEASE -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 設置全局依賴 SpringBoot、Web -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

建立 Eureka Server 子項目

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-server-simple

父pom中建立 Eureka Server 子項目 module:spring-cloud-eureka-server

pom 依賴

<!-- 父工程 -->
<parent>
    <groupId>com.laiyy.gitee</groupId>
    <artifactId>spring-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
    <!-- 引入 eureka server 依賴,注意不能少了 starter -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

啓動類

@SpringBootApplication
@EnableEurekaServer     // 開啓 Eureka Server 
public class SpringCloudEurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
    }
}

配置文件

spring:
  application:
    name: eureka-server # 服務名稱
server:
  port: 8761 # 服務端口

eureka:
  instance:
    hostname: localhost # host name
  client:
    fetch-registry: false # 是否獲取註冊表
    service-url:
      defaultZone: http://localhost:${server.port:8761}/eureka/ # 默認 Zone
    register-with-eureka: false # 是否註冊自己
  server:
    enable-self-preservation: false # 是否開啓自我保護,默認 true。在本機測試可以使用 false,但是在生產環境下必須爲 true

驗證 Eureka Server 啓動結果

在瀏覽器輸入: http://localhost:8761 ,進入 Eureka Server 可視化頁面

Eureka Server UI 提示信息

在 Eureka Server 檢測到異常時,會在中間以紅色加粗字體提示信息。

在沒有 Eureka Client 或 Eureka Server 檢測心跳的閾值小於指定閾值,且關閉自我保護時:

RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
提示說明了:1、eureka client 的心跳發送次數小於閾值(沒有client,肯定小於);2、自我保護被關閉了

在有 Eureka Client,且關閉了自我保護時:

THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

在沒有 Eureka Client 或 Eureka Server 檢測心跳的閾值小於指定閾值,且開啓了自我保護時:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

在有 Eureka Client 且 Eureka Server 檢測心跳的閾值大於指定閾值,且開啓了自我保護時,Eureka Server 會認爲整個服務正常,不會有任何信息提示。

建立 Eureka Client 項目

源碼:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-client-simple

在 在父pom中建立 Eureka Server 子項目 module:spring-cloud-eureka-client-simple,建立一個簡單的 eureka client 工程

pom

<!-- parent 都和 Eureka Server 一致,不再贅述 -->

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

啓動類

// EurekaClient、DiscoveryClient 本質上都是註冊到服務中心的實現,EurekaClient 只針對 Eureka 使用,
// DiscoveryClient 針對不同的註冊中心都可以使用。可以說 DiscoveryClient 的 EurekaClient 的一個抽象
@SpringBootApplication
@EnableDiscoveryClient //@EnableEurekaClient 
public class SpringCloudEurekaClientSimpleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaClientSimpleApplication.class, args);
    }
}

配置文件

spring:
  application:
    name: spring-cloud-eureka-client-simple # 工程名稱,也是註冊到 server 後的實例名稱

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka      # 指定 Zone,需要與 Eureka Server 一樣
  instance:
    prefer-ip-address: true # 使用 ip 註冊,默認爲 false。爲 false 時是 機器名
    instance-id: ${spring.application.name}:${server.port} # 註冊到 server 後顯示的名字,默認是 機器名:name:端口

server:
  port: 8081 # 端口

在 Eureka Server 驗證服務註冊

可以看到,eureka client 已經註冊成功。 status 大致有 5 個狀態:UP(正常運行)、DOWN(停機)、STATING(正在啓動)、OUT_OF_SERVICE(下線)、UNKNOWN(爲止)


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