Eureka是Spring Cloud Netflix微服務套件中的一部分,可以與Springboot構建的微服務很容易的整合起來。
Eureka包含了服務器端和客戶端組件。服務器端,也被稱作是服務注冊中心,用於提供服務的注冊與發現。Eureka支持高可用的配置,當集群中有分片出現故障時,Eureka就會轉入自動保護模式,它允許分片故障期間繼續提供服務的發現和注冊,當故障分片恢復正常時,集群中其他分片會把他們的狀態再次同步回來。
客戶端組件包含服務消費者與服務生產者。在應用程序運行時,Eureka客戶端向注冊中心注冊自身提供的服務並周期性的發送心跳來更新它的服務租約。同時也可以從服務端查詢當前注冊的服務信息並把他們緩存到本地並周期性的刷新服務狀態。
搭建空的maven工程,以后把其它模塊都放入下面
創建完成后界面如下:
可以刪除src目錄,因為這個工程只是為了放置springcloud的其它模塊。
搭建服務注冊中心服務注冊中心 (eureka-server)
Eureka Server是基於springboot的,只要啟動一個springboot就可以了。start.spring.io提供了一系列啟動模板。創建module,選擇Spring initializer.:
設置artifactId值為eureka-server
加入Eureka Server組件
創建完成后目錄如下
自動生成的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xuan</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
修改啟動類EurekaServerApplication.java,添加@EnableEurekaServer
package com.xuan.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
在默認情況下,服務注冊中心也會把自己當做是一個服務,將自己注冊進服務注冊中心,所以我們可以通過配置來禁用他的客戶端注冊行為,修改application.properties文件
spring.application.name=eureka-server
#服務注冊中心端口號
server.port=8080
#服務注冊中心實例的主機名
eureka.instance.hostname=localhost
#是否向服務注冊中心注冊自己
eureka.client.register-with-eureka=false
#是否檢索服務
eureka.client.fetch-registry=false
#服務注冊中心的配置內容,指定服務注冊中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
啟動應用,並訪問http://localhost:8080/即可看到Eureka信息面板,如下:
從上圖看到,在"Instances currently registered with Eureka"信息中,沒有一個實例,說明目前還沒有服務注冊。接下來創建一個服務提供者eureka-client進行注冊測試。
創建並注冊服務提供者 Eureka Client
創建方式如eureka-server模塊類似;在選擇組件的時候需要選擇對應的組件
注意要選擇Web組件或者其它能夠持久運行的。不然會注冊失敗
創建后目錄如下
生成的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xuan</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <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-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
啟動類EurekaClientApplication.java添加@EnableDiscoveryClient注解以實現Eureka中的DiscoveryClient實現。
package com.xuan.eurekaclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
@EnableEurekaClient和@EnableDiscoveryClient的區別
spring cloud中discovery service有許多種實現(eureka、consul、zookeeper等等),@EnableDiscoveryClient基於spring-cloud-commons,@EnableEurekaClient基於spring-cloud-netflix。
就是如果選用的注冊中心是eureka,那么就推薦@EnableEurekaClient,如果是其他的注冊中心,那么推薦使用@EnableDiscoveryClient。
修改配置文件application.properties
spring.application.name=eureka-client
server.port=8090
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
測試是否能夠注冊
先啟動:eureka-server
然后啟動eureka-client
發現多了一個EUREKA-CLIENT,說明注冊成功了
其它問題:
運行一段時間后,在http://localhost:8080/出現
停止eureka-client后也沒有刪除節點。
原因參考:
https://www.cnblogs.com/breath-taking/articles/7940364.html
http://jadyer.cn/2017/01/17/springcloud-eureka-advance
自我保護模式打開時,已關停節點是會一直顯示在 Eureka 首頁的
關閉自我保護模式后,由於其默認的心跳周期比較長等原因,要過一會兒才會發現已關停節點被自動踢出了
若想盡快的及時踢出,那就只有修改默認的心跳周期參數了
注冊中心eureka-server的配置文件application.properties中修改為
spring.application.name=eureka-server
#服務注冊中心端口號
server.port=8080
#服務注冊中心實例的主機名
eureka.instance.hostname=localhost
#關閉自我保護
eureka.server.enableSelfPreservation=false
# 續期時間,即掃描失效服務的間隔時間(缺省為60*1000ms)
eureka.server.eviction-interval-timer-in-ms: 1000
#是否向服務注冊中心注冊自己
eureka.client.register-with-eureka=false
#是否檢索服務
eureka.client.fetch-registry=false
#服務注冊中心的配置內容,指定服務注冊中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
Eureka客戶端eureka-client的配置文件application.properties中修改為
spring.application.name=eureka-client
server.port=8090
# 心跳時間,即服務續約間隔時間(缺省為30s)
eureka.instance.lease-renewal-interval-in-seconds: 5
# 發呆時間,即服務續約到期時間(缺省為90s)
eureka.instance.lease-expiration-duration-in-seconds: 15
# 開啟健康檢查(依賴spring-boot-starter-actuator)
eureka.client.healthcheck.enabled:true
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/
eureka-client的pom.xml增加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
修改后,關閉eureka-client,注冊中心就會很快刪除節點
源碼下載地址:https://gitee.com/xuantest/springcloud/tree/master