1. 程式人生 > >SpringCloud 基礎教程(一) 服務中心(Eureka Server)

SpringCloud 基礎教程(一) 服務中心(Eureka Server)

client -c enter 以及 清理 啟動服務 one ram 一個

1、創建【服務中心】,即 Eureka Server

1.1、新建 Spring Boot 工程,工程名稱: springcloud-eureka-server

1.2、工程 pom.xml 文件添加如下依賴:

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

1.3、在工程啟動類中,添加註解 @EnableEurekaServer

package com.miniooc.eurekaserver;

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

/**
 * EurekaServerApplication
 *
 * @author 宋陸
 * @version 1.0.0
 */
@EnableEurekaServer // 啟用 eureka server 相關默認配置
@SpringBootApplication // 等價於 @Configuration、@EnableAutoConfiguration、@ComponentScan
public class EurekaServerApplication {

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

}

1.4、新建工程配置文件 application.yml ,配置內容:

server:
  port: 9527

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    register-with-eureka: false #  默認為 true。設為 false,僅作為服務中心,不作為服務客戶端。
    fetch-registry: false # 默認為true。設為false,不從服務中心檢索註冊的服務。
  server:
    eviction-interval-timer-in-ms: 5000 #清理間隔(單位毫秒,默認是60*1000)
    enable-self-preservation: true # 默認為true。設為false,關閉自我保護。
    # Eureka Server 在運行期間會去統計心跳失敗比例在 15 分鐘之內是否低於 85%
    renewal-percent-threshold: 0.49 # 默認是0.85,本地單機測試設為0.49

1.5、啟動 服務中心 工程,打開瀏覽器,訪問 服務中心 前臺頁面:http://localhost:9527

技術分享圖片

紅框處 No instances available :沒有可用的實例。目前還任何服務註冊到服務中心。

至此,一個簡單的單點服務中心搭建完成。

為了保證服務的高可用,我們需要把單點應用改成集群應用。

接下來,我們對工程做些修改,來完成集群搭建。

2、搭建【服務中心】集群

2.1、創建工程配置文件 application-server1.yml ,配置內容:

server:
  port: 9527

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
    register-with-eureka: false #  默認為 true。設為 false,僅作為服務中心,不作為服務客戶端。
    fetch-registry: false # 默認為true。設為false,不從服務中心檢索註冊的服務。
  server:
    eviction-interval-timer-in-ms: 5000 #清理間隔(單位毫秒,默認是60*1000)
    enable-self-preservation: true # 默認為true,設為false,關閉自我保護
    # Eureka Server 在運行期間會去統計心跳失敗比例在 15 分鐘之內是否低於 85%
    renewal-percent-threshold: 0.49 # 默認是0.85,本地單機測試設為0.49

參照2.1,創建工程配置文件 application-server2.yml,並修改 server.port 為 9528

參照2.1,創建工程配置文件 application-server3.yml,並修改 server.port 為 9529

2.2、在 IntelliJ IDEA 集成開發環境中,添加 spring boot 啟動配置 EurekaServer1,修改 Active profiles 為 server1

技術分享圖片

技術分享圖片

技術分享圖片

參照2.2, spring boot 啟動配置 EurekaServer2,修改 Active profiles 為 server2

參照2.2, spring boot 啟動配置 EurekaServer3,修改 Active profiles 為 server3

2.3、按照啟動配置 EurekaServerS1、EurekaServerS2、EurekaServerS3 依次啟動服務中心

技術分享圖片

2.4、打開瀏覽器,訪問 服務中心 前臺頁面:http://localhost:9527,http://localhost:9528,http://localhost:9529

技術分享圖片

如果三個服務都啟動成功的話,三個頁面都應該看到如上圖所示。

至此,一個簡單的服務中心集群搭建完成。

3、本章小結

本章主要講解了 Eureka Server 服務中心的搭建,並講解如果擴展為集群應用。但服務中心的使用以及集群的優點,本章並未講解,因為這需要 Eureka Client 服務註冊和 Eureka Discovery Client 服務發現配合使用,後續的章節我們會陸續講解後兩者的使用。

下一章,我們將講解,如何創建服務,並把我們的服務註冊到服務中心,以及服務中心的集群是如何保證服務的高可用。


SpringCloud 教程 課程目錄

SpringCloud 教程 之 SpringCloud概述

SpringCloud 基礎教程(一) 服務中心(Eureka Server)

SpringCloud 教程 之二 服務消費(Ribbon)

SpringCloud 教程 之三 服務消費(Feign)

SpringCloud 基礎教程(一) 服務中心(Eureka Server)