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
紅框處 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 服務提供者,並把我們的服務注冊到服務中心,以及如何搭建服務提供者集群。