Eureka
使用場景
微服務,將業務模塊向下圖1一樣划分為了一個個服務。初期,服務間的調用我們可以使用RESTApi來通信,但到了后期,服務多了以后,還通過RESTApi訪問其他服務,因為RESTApi使用ip+port來訪問,服務的數量級上去,代碼嵌入地址或者配置文件的方式都會造成管理上的不變。
圖1
so,我們開始使用一種更為簡單的方式注冊中心(Eureka)來實現服務的互相調用,GO!
技術架構
網上有完整版的eureka架構圖,這里我們不在贅述,以一個簡單的架構圖,來了解eureka的架構
圖2
根據圖2,我們可以知道eureka將參與者分為了
·服務發現者
·服務提供者
·服務消費者
首先,服務發現者即是服務端(Server),提供者和消費者是客戶端(Client);
客戶端將自己注冊到服務端,服務端可以在其他服務需要客戶端是將客戶端返回;
好處:客戶端和客戶端之間,只需要知道彼此在服務端注冊的名字,無論對方ip,port,內容更改 都不會受到影響;
開始實戰
服務端
pom
<!-- 父模塊信息 --> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Finchley.SR1</version> </parent> <!-- 引用jar --> <dependencies> <!-- cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <!-- eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
yml
server:
port: 8080 # 當前eureka server 服務的端口號為8080
eureka:
instance:
hostname: localhost # 當前eureka 的hostname 為 localhost
client:
register-with-eureka: false # 當前服務不需要到eureka server上注冊為客戶端
fetch-registry: false #
serviceUrl: # eureka服務地址
defaultZone: http://localhost:8080/eureka/
客戶端A
pom
<!-- 父模塊信息 --> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Finchley.SR1</version> </parent> <!-- 引用jar --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <!-- eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
yml
spring:
application:
name: provider-source #服務名稱
server:
port: 8082 #啟動端口號
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true #是否注冊到eureka服務器
fetch-registry: true #是否可檢索
service-url:
defaultZone: http://localhost:8080/eureka/ #服務中心
客戶端B
pom
<!-- 父模塊信息 --> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Finchley.SR1</version> </parent> <!-- 引用jar --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <!-- eureka client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependency> </dependencies>
yml
spring:
application:
name: provider-speaker # 服務名稱
server:
port: 8081 # 服務占用端口號
eureka:
instance:
hostname: localhost
client:
register-with-eureka: true #是否注冊到eureka服務器
fetch-registry: true #是否可檢索
service-url:
defaultZone: http://tj:tj123456@localhost:8080/eureka/ #服務中心
服務端啟動類添加注解
@SpringBootApplication
@EnableEurekaServer # eureka 服務端注解
public class DiscoveryApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryApplication.class, args); } }
客戶端A,B分別添加注解
package com.tj;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient # eureka客戶端注解 public class SpeakerApplication { public static void main(String[] args) { SpringApplication.run(SpeakerApplication.class, args); } }
接着,依此啟動服務端,客戶端A,客戶端B;
打開,服務端地址http://localhost:8080,可以看到以下圖3,便說明客戶端在服務器注冊成功
圖3
至此,Eureka入門,已經完成。