SpringCloud2.0 Eureka Server 服務中心 基礎教程(二)

一、建立【服務中心】,即 Eureka Server

1.一、新建 Spring Boot 工程,工程名稱: springcloud-eureka-server

1.二、工程 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.三、在工程啓動類中,添加註解 @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.四、新建工程配置文件 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.1 # 默認是0.85

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

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

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

爲了保證服務的高可用,咱們須要把單點應用改爲集羣應用。spring

接下來,咱們對服務中心工程作些配置修改,來完成集羣部署。瀏覽器

二、搭建【服務中心】集羣

2.一、建立工程配置文件 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 爲 9528bash

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

2.二、在 IntelliJ IDEA 集成開發環境中,添加 spring boot 啓動配置 EurekaServerS1,修改 Active profiles 爲 server1

參照2.2, spring boot 啓動配置 EurekaServerS2,修改 Active profiles 爲 server2spring-boot

參照2.2, spring boot 啓動配置 EurekaServerS3,修改 Active profiles 爲 server3測試

2.三、按照啓動配置 EurekaServerS一、EurekaServerS二、EurekaServerS3 依次啓動服務中心

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

若是三個服務都啓動成功的話,三個頁面都應該看到如上圖所示。fetch

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

三、本章小結

本章主要講解了 Eureka Server 服務中心的搭建,並講解若是擴展爲集羣應用。但服務中心的使用以及集羣的優勢,本章並未講解,由於這須要 Eureka Client 服務提供者和 Eureka Discovery Client 服務發現者配合使用,後續的章節咱們會講解後二者的使用。

下一章,咱們將講解,如何建立 Eureka Client 服務提供者,並把咱們的服務註冊到服務中心,以及如何搭建服務提供者集羣。

相關文章
相關標籤/搜索