Eureka服務(wù)注冊與實(shí)現(xiàn)
什么是Eureka
- Netflix在設(shè)計Eureka時遵循的就是AP原則
- Eureka是Netflix的一個子模塊卒蘸,也是核心模塊之一循帐。Eureka是一個基于REST的服務(wù)罪郊,用于定位服務(wù)饵逐,以實(shí)現(xiàn)云端中間層服務(wù)發(fā)現(xiàn)和故障轉(zhuǎn)移忠售,服務(wù)注冊與發(fā)現(xiàn)對于微服務(wù)來設(shè)是非常重要的恰矩,有了服務(wù)發(fā)現(xiàn)于注冊,只需要使用服務(wù)的標(biāo)識符谦去,就可以訪問到服務(wù)慷丽,而不需要修改服務(wù)調(diào)用的配置文件了,功能類似于Dubbo的注冊中心鳄哭,比如Zookeeper;
原理講解
4.png
- Eureka的基本架構(gòu)
- springCould封裝NetFlix公司開發(fā)的Eureka模塊來實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn)
- Eureka采用了C-S的架構(gòu)設(shè)計盈魁,EurekaServer作為服務(wù)注冊功能的服務(wù)器,他是服務(wù)注冊中心
- 而系統(tǒng)中的其他微服務(wù)窃诉。使用Eureka的客戶端連接到EurekaServer并維持心跳連接杨耙。這樣系統(tǒng)的維護(hù)人員就可以通過EurekaServer來監(jiān)控系統(tǒng)中各個微服務(wù)是否正常運(yùn)行,SpringCloud的一些其他模塊(比如Zuul)就可以通過EurekaServer來發(fā)現(xiàn)系統(tǒng)中的其他微服務(wù)飘痛,并執(zhí)行相關(guān)的邏輯珊膜;
- Eureka包含兩個組件:Eureka Server 和Eureka Client
- EurekaServer提供服務(wù)注冊服務(wù),各個節(jié)點(diǎn)啟動后宣脉,會再EurekaServer中進(jìn)行注冊车柠,這樣EurekaServer中的服務(wù)注冊表中將會存所有可用服務(wù)節(jié)點(diǎn)的信息,服務(wù)節(jié)點(diǎn)的信息可以在界面中直觀的看到塑猖。
- Eureka Client是一個Java客戶端竹祷,用于簡化EurekaServer的交互,客戶端同時也具備一個內(nèi)置的羊苟,使用輪詢負(fù)載算法的負(fù)載均衡器塑陵。在應(yīng)用啟動后,將會向EurekaServer發(fā)送心跳(默認(rèn)周期為30秒)蜡励。如果EurekaServer在多個心跳周期內(nèi)沒有接收到某個節(jié)點(diǎn)的心跳令花,EurekaServer將會從服務(wù)注冊表中把這個服務(wù)節(jié)點(diǎn)移除掉(默認(rèn)為90秒)
- 三大角色
- Eureka Server:提供服務(wù)的注冊與發(fā)現(xiàn)。
- Service Provider:將自身服務(wù)注冊到Eureka中凉倚,從而使消費(fèi)方能夠找到兼都。
- Service Consumer:服務(wù)消費(fèi)方從Eureka中獲取注冊服務(wù)列表,從而找到消費(fèi)服務(wù)稽寒。
eureka使用
導(dǎo)入依賴
<?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">
<parent>
<artifactId>jgnspringcloud</artifactId>
<groupId>com.jgn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-7001</artifactId>
<!-- 導(dǎo)包 -->
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
<!-- 熱部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
對eureka進(jìn)行配置
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: localhost #Eureka服務(wù)端的實(shí)例名稱
client:
register-with-eureka: false #表示是否向eureka注冊中心注冊自己
fetch-registry: false #fetch-registry如果為false扮碧,則表示自己為注冊中心
service-url: #監(jiān)控頁面 不配置默認(rèn)訪問端口8761
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #動態(tài)配置
編寫啟動類
package com.jgn.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
// 啟動之后,訪問http://localhost:7001/
@SpringBootApplication
@EnableEurekaServer // EnableEurekaServer 服務(wù)端的啟動類杏糙,可以接受別人注冊進(jìn)來
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
注冊
在要注冊的module的依賴中加入eureka相關(guān)依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
在該module的applction.yml文件中配置慎王,讓defaultZone與注冊中心保存一致。
#Eureka的配置搔啊,服務(wù)注冊到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
在該module的啟動類添加注解來啟動注冊
package com.jgn.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
測試柬祠,先啟動服務(wù)端,再啟動客戶端
5.png
Eureka集群
分別配置三個eurekaServer负芋,使用不同的端口號漫蛔,分別為7001,7002旧蛾,7003
6.png
在各自的配置文件進(jìn)行配置
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: eureka7001.com #Eureka服務(wù)端的實(shí)例名稱
client:
register-with-eureka: false #表示是否向eureka注冊中心注冊自己
fetch-registry: false #fetch-registry如果為false莽龟,則表示自己為注冊中心
service-url: #監(jiān)控頁面 不配置默認(rèn)訪問端口8761
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ #動態(tài)配置
# 單機(jī) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #動態(tài)配置
# 集群(管連) defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #動態(tài)配置
server:
port: 7002
#Eureka配置
eureka:
instance:
hostname: eureka7002.com #Eureka服務(wù)端的實(shí)例名稱
client:
register-with-eureka: false #表示是否向eureka注冊中心注冊自己
fetch-registry: false #fetch-registry如果為false,則表示自己為注冊中心
service-url: #監(jiān)控頁面 不配置默認(rèn)訪問端口8761
defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7001.com:7001/eureka/ #動態(tài)配置
server:
port: 7003
#Eureka配置
eureka:
instance:
hostname: eureka7003.com #Eureka服務(wù)端的實(shí)例名稱
client:
register-with-eureka: false #表示是否向eureka注冊中心注冊自己
fetch-registry: false #fetch-registry如果為false锨天,則表示自己為注冊中心
service-url: #監(jiān)控頁面 不配置默認(rèn)訪問端口8761
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ #動態(tài)配置
將提供者provider在集群中完成注冊毯盈,修改application.yml
#Eureka的配置,服務(wù)注冊到哪里
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001 #修改eureka上的默認(rèn)描述信息
CAP原則
ACID:RDBMS(MySQL病袄、Oracle搂赋、sqlServer)
- A(Atomicity)原子性
- C(Consistency)一致性
- I(Isolation)隔離性
- D(durability)持久性
CAP:NoSQL(redis赘阀、mongdb)
- C(Consistency)強(qiáng)一致性
- A(Availability)可用性
- P(Partition tolerance)分區(qū)容錯性
CAP的三進(jìn)二:CA、AP脑奠、CP
CAP的核心
一個分布式系統(tǒng)不可能同時很好的滿足一致性基公、可用性和分區(qū)容錯性這三個需求
根據(jù)CAP原理,將NoSQL數(shù)據(jù)庫分成了滿足CA原則宋欺、滿足CP原則和滿足AP原則三大類:
- CA:單點(diǎn)集群轰豆,滿足一致性、可用性的系統(tǒng)齿诞,通乘嵝荩可擴(kuò)展性較差
- CP:滿足一致性,分區(qū)容錯性的系統(tǒng)祷杈,通常性能不是特別高
- AP:滿足可用性斑司,分區(qū)容錯性的系統(tǒng),通撤褪剑可能對一致性要求低一些
zookeeper保證的是CP
Eureka保證的是AP