<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>
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); } }
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
紅框處 No instances available :沒有可用的實例。目前還任何服務註冊到服務中心。java
至此,一個簡單的單點服務中心搭建完成。web
爲了保證服務的高可用,咱們須要把單點應用改爲集羣應用。spring
接下來,咱們對服務中心工程作些配置修改,來完成集羣部署。瀏覽器
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.2, spring boot 啓動配置 EurekaServerS2,修改 Active profiles 爲 server2spring-boot
參照2.2, spring boot 啓動配置 EurekaServerS3,修改 Active profiles 爲 server3測試
若是三個服務都啓動成功的話,三個頁面都應該看到如上圖所示。fetch
至此,一個簡單的服務中心集羣搭建完成。.net
本章主要講解了 Eureka Server 服務中心的搭建,並講解若是擴展爲集羣應用。但服務中心的使用以及集羣的優勢,本章並未講解,由於這須要 Eureka Client 服務提供者和 Eureka Discovery Client 服務發現者配合使用,後續的章節咱們會講解後二者的使用。
下一章,咱們將講解,如何建立 Eureka Client 服務提供者,並把咱們的服務註冊到服務中心,以及如何搭建服務提供者集羣。