參考:方志朋的專欄
1. Eureka簡介
Spring Cloud 為開發人員提供了快速構建分布式系統的一些工具,包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分布式會話等等。
其中Eureka 是 Netflix 開發的,一個基於 REST 服務的,服務注冊與發現的組件。
它主要包括兩個組件:Eureka Server 和 Eureka Client。
- Eureka Server:提供服務注冊和發現的能力(通常就是微服務中的注冊中心)
- Eureka Client:一個Java客戶端,用於簡化與 Eureka Server 的交互(通常就是微服務中的客戶端和服務端)
各個微服務啟動時,會通過 Eureka Client 向 Eureka Server 注冊自己,Eureka Server 會存儲該服務的信息;也就是說,每個微服務的客戶端和服務端,都會注冊到 Eureka Server,這就衍生出了微服務相互識別的話題。
- 同步:每個 Eureka Server 同時也是 Eureka Client(邏輯上的)
多個 Eureka Server 之間通過復制的方式完成服務注冊表的同步,形成 Eureka 的高可用 - 識別:Eureka Client 會緩存 Eureka Server 中的信息
即使所有 Eureka Server 節點都宕掉,服務消費者仍可使用緩存中的信息找到服務提供者(筆者已親測) - 續約:微服務會周期性(默認30s)地向 Eureka Server 發送心跳以Renew(續約)信息(類似於heartbeat)
- 續期:Eureka Server 會定期(默認60s)執行一次失效服務檢測功能
它會檢查超過一定時間(默認90s)沒有Renew的微服務,發現則會注銷該微服務節點
Spring Cloud 已經把 Eureka 集成在其子項目 Spring Cloud Netflix 里面。
2. 創建服務注冊中心(Eureka Server)
首先創建一個主Maven工程,在其pom文件引入依賴,spring Boot版本為2.0.3.RELEASE,Spring Cloud版本為Finchley.RELEASE。這個pom文件作為父pom文件,起到依賴版本控制的作用,其他module工程繼承該pom。
<?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.zang</groupId> <artifactId>sc-f-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>sc-f-eureka</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/> </parent> <modules> <module>eureka-server</module> <module>service-hi</module> </modules> <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-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>
然后創建一個model工程作為服務注冊中心,即Eureka Server。Eureka Server的pom.xml繼承了父pom文件,並引入spring-cloud-starter-netflix-eureka-server的依賴。
<?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.zang</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>com.zang</groupId> <artifactId>sc-f-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
啟動一個服務注冊中心,只需要一個注解@EnableEurekaServer,這個注解需要在springboot工程的啟動application類上加:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run( EurekaServerApplication.class, args ); } }
eureka是一個高可用的組件,它沒有后端緩存,每一個實例注冊之后需要向注冊中心發送心跳(因此可以在內存中完成),在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置文件appication.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server。
Eureka Server 是有界面的,啟動工程,打開瀏覽器訪問: http://localhost:8761 ,界面如下:
3. 創建服務提供者(Eureka Client)
當client向server注冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka server 從每個client實例接收心跳消息。 如果心跳超時,則通常將該實例從注冊server中刪除。
創建過程同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.zang</groupId> <artifactId>service-hi</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-hi</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.zang</groupId> <artifactId>sc-f-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <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-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
通過注解@EnableEurekaClient 表明自己是一個Eureka Client。
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run( ServiceHiApplication.class, args ); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam(value = "name", defaultValue = "zang") String name) { return "hi " + name + " ,i am from port:" + port; } }
僅僅@EnableEurekaClient是不夠的,還需要在配置文件中注明自己的服務注冊中心的地址,application.yml配置文件如下:
server:
port: 8762
spring:
application:
name: service-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
需要指明spring.application.name,這個很重要,這在以后的服務與服務之間相互調用一般都是根據這個name 。 啟動工程,打開http://localhost:8761 ,即Eureka Server 的網址:
你會發現一個服務已經注冊在服務中了,服務名為SERVICE-HI ,端口為7862
這時打開 http://localhost:8762/hi?name=test,你會在瀏覽器上看到 :
如上,我們在8762端口啟動了Eureka Client,如果此時想要在8763端口啟動同樣的Eureka Client實例,方法很簡單,只需要修改Eureka Client工程中application.yml文件的server.port為8763再啟動即可,不過需要配置idea使其支持多實例啟動。