Spring Cloud實戰系列(一) - 服務註冊與發現Eureka
Spring Cloud
封裝了Netflix
公司開發的Eureka
模組來實現服務註冊和發現
。Eureka
採用了C-S
的設計架構
。Eureka Server
作為服務註冊中心
,系統中的其他微服務
,使用Eureka
的客戶端
連線到Eureka Server
,並通過心跳連線
檢測服務的存活狀態
。
正文
-
Eureka Server: 作為服務註冊中心 ,提供服務註冊和發現 。
-
Eureka Client: 所有註冊到服務中心 的服務。
-
Service Provider: 把自身的服務 註冊到
Eureka Server
,從而使服務消費方 能夠找到。 -
Service Consumer: 從
Eureka Server
獲取服務註冊列表 ,從而能夠消費服務 。
-
1. 建立服務註冊中心
建立2
個專案Module
,一個Module
(即Spring Boot
)工程作為服務註冊中心
,即Eureka Server
,另一個作為Eureka Client
。
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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>io.ostenant.github.springcloud</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-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> 複製程式碼
2. 啟動服務註冊中心
Eureka
是一個高可用
的元件,它沒有後端快取
。每一個例項
註冊之後,需要定時
向註冊中心
傳送心跳
(因此可以在記憶體中完成)。在預設情況下Eureka Server
也是一個Eureka Client
,必須要指定一個Server
。在啟動之前,首先對Eureka Server
配置application.yml
檔案。
server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 複製程式碼
- eureka.client.register-with-eureka :
設定是否將自己作為Eureka Client
註冊到Eureka Server
,預設為true
。
- eureka.client.fetch-registry
設定是否從Eureka Server
獲取註冊資訊
,預設為true
。因為本例是一個單點
的Eureka Server
,不需要同步
其他Eureka Server
節點的資料,所以設定為false
。
- eureka.client.service-url.defaultZone
設定的是與Eureka Server
的互動地址
,查詢
和註冊服務
都依賴這個地址,如果有多個可以使用英文逗號分隔
。
然後再把註解@EnableEurekaServer
加在Spring Boot
工程的啟動類Application
上面:
@EnableEurekaServer @SpringBootApplication public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } } 複製程式碼
Eureka Server
是有介面的,啟動專案後,開啟瀏覽器訪問http://localhost:8761
即可檢視。
3. 建立服務提供者
當一個Eureka Client
向Eureka Server
發起註冊
時,它會提供一些元資料
,例如主機
和埠
等等。Eureka Server
從每個Eureka Client
例項接收心跳訊息
。 如果心跳超時
,則通常將該例項從Eureka Server
中刪除。
建立一個service-hi
的Module
,建立完成後的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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>io.ostenant.github.springcloud</groupId> <artifactId>service-hi</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-hi</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Dalston.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-cloud-starter-web</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> 複製程式碼
通過註解
@EnableEurekaClient
表明自己是一個Eureka Client
。
@SpringBootApplication @EnableEurekaClient @RestController public class ServiceHiApplication { @Value("${server.port}") private String port; public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } @RequestMapping("/hi") public String home(@RequestParam String name) { return "Hi " + name + ", I am from port: "+ port; } } 複製程式碼
僅僅@EnableEurekaClient
是不夠的,還需要在配置檔案
中註明的服務註冊中心
的地址,application.yml
配置檔案如下:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8763 spring: application: name: service-hi 複製程式碼
Eureka
客戶端
需要指明spring.application.name
,用於服務的唯一標識
,服務之間
相互呼叫會基於這個name
。
啟動並訪問Eureka Server
的地址http://localhost:8761
,會發現服務名稱為SERVICE-HI
,埠為7862
的服務,已註冊到Eureka Server
的列表上。
3. 高可用Eureka Server
在一個分散式系統 中,服務註冊中心 是最重要的基礎部分,必須處於可以提供服務 的狀態。為了維持其可用性 ,使用叢集 是很好的解決方案。
Eureka
通過節點對等註冊
的方式實現高可用的部署
,所以只需要為每一個Eureke Server
配置其他可用的
Eureke Server
的serviceUrl
,就能實現高可用部署。
spring: profiles: active: peer1 #peer2 --- spring: profiles: peer1 server: port: 8761 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8762/eureka/ --- spring: profiles: peer2 server: port: 8762 eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/ 複製程式碼
更改Eureka Server
的配置檔案,再分別以spring.profiles.active=peer1
和spring.profiles.active=peer2
作為引數,啟動兩次Eureka Server
即可。
-
訪問
http://localhost:8761/
。可以發現,Eureka Client
已經向埠號 為8761
的Eureka Server
發起註冊。 -
服務提供者的配置檔案 並沒有向埠號 為
8762
的Eureka Server
註冊。訪問http://localhost:8762/
。可以發現,服務提供者 的資訊已經向8762
發起註冊了,即8761
的註冊資訊 已經同步到8762
節點。