SpringCloud2.0 Eureka Server 服務中心 基礎教程(二)
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.1 # 預設是0.85
1.5、啟動 服務中心 工程,打開浏覽器,通路 服務中心 前台頁面:http://localhost:9527
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL2EDO5AzNxYDMy0iMzMjMwYTMxETNxETM4EDMy0CN2QjNwUTMvwVMxgTMwIzLcRjN0YDM1EzLcd2bsJ2Lc12bj5ycn9Gbi52YugTMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
紅框處 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 啟動配置 EurekaServerS1,修改 Active profiles 為 server1
參照2.2, spring boot 啟動配置 EurekaServerS2,修改 Active profiles 為 server2
參照2.2, spring boot 啟動配置 EurekaServerS3,修改 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 服務發現者配合使用,後續的章節我們會講解後兩者的使用。
下一章,我們将講解,如何建立 Eureka Client 服務提供者,并把我們的服務注冊到服務中心,以及如何搭建服務提供者叢集。