相關(guān)資料:
一.什么是Eureka
Eureka的本意是我發(fā)現(xiàn)了,我找到了的意思,它同時(shí)也是SpringCloud中的Netflix 出品的用于實(shí)現(xiàn)服務(wù)注冊和發(fā)現(xiàn)的工具。首先了解一些概念:

- Eureka Server:提供服務(wù)發(fā)現(xiàn)能力,各個(gè)微服務(wù)啟動時(shí),會向 Eureka Server 注冊自己的信息,Eureka Server 會保存這些信息
- Eureka Client:是一個(gè) Java 客戶端,用于簡化與Eureka Server的交互,Eureka Client每30秒發(fā)送心跳,如果服務(wù)端沒有接收到心跳信號。它將在90秒后從服務(wù)器注冊表中刪除。
- Eureka自我保護(hù)機(jī)制:自我保護(hù)模式正是一種針對網(wǎng)絡(luò)異常波動的安全保護(hù)措施,使用自我保護(hù)模式能使Eureka集群更加的健壯、穩(wěn)定的運(yùn)行。自我保護(hù)機(jī)制的工作機(jī)制是如果在15分鐘內(nèi)超過85%的客戶端節(jié)點(diǎn)都沒有正常的心跳,那么Eureka就認(rèn)為客戶端與注冊中心出現(xiàn)了網(wǎng)絡(luò)故障,Eureka Server自動進(jìn)入自我保護(hù)機(jī)制,可以通過
enable-self-preservation開啟,此時(shí)會出現(xiàn)以下幾種情況:- Eureka Server不再從注冊列表中移除因?yàn)殚L時(shí)間沒收到心跳而應(yīng)該過期的服務(wù)。
- Eureka Server仍然能夠接受新服務(wù)的注冊和查詢請求,但是不會被同步到其它節(jié)點(diǎn)上,保證當(dāng)前節(jié)點(diǎn)依然可用。
- 當(dāng)網(wǎng)絡(luò)穩(wěn)定時(shí),當(dāng)前Eureka Server新的注冊信息會被同步到其它節(jié)點(diǎn)中。
因此Eureka Server可以很好的應(yīng)對因網(wǎng)絡(luò)故障導(dǎo)致部分節(jié)點(diǎn)失聯(lián)的情況,而不會像ZK那樣如果有一半不可用的情況會導(dǎo)致整個(gè)集群不可用而變成癱瘓。
二.實(shí)踐部分
程序主要分為三部分,在Server一般選擇集群式部署,在本文中,單部署和集群部署均有寫。
- Server
- Provider
- Client
1.Server
Maven:添加eureka-server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Config:添加@EnableEurekaServer注釋
/**
* 注冊中心
* @author: BaoZhou
* @date : 2018/6/29 11:06
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件:
單Server配置文件
server:
port: 8761
eureka:
instance:
hostname: eureka-server
client:
#不把自己注冊到Eureka上
register-with-eureka: false
#不從Eureka獲取注冊信息
fetch-registry: false
service-url:
default-zone: http://localhost:8761/eureka
集群Server配置文件,主要是要把幾個(gè)Server之間要互相注冊,服務(wù)注冊時(shí)只要在其中一個(gè)Server上注冊,其他Server會自動同步:
spring:
application:
name: eureka
---
spring:
profiles: eureka1
eureka:
instance:
hostname: eureka1
client:
service-url:
default-zone: http://localhost:8762/eureka,http://localhost:8763/eureka
---
spring:
profiles: eureka2
eureka:
instance:
hostname: eureka2
client:
service-url:
default-zone: http://localhost:8761/eureka,http://localhost:8763/eureka
---
spring:
profiles: eureka3
eureka:
instance:
hostname: eureka3
client:
service-url:
default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka
啟動服務(wù)時(shí),只要將打出來的Jar包依次帶參執(zhí)行即可:
java -jar eureka-sever-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka1 --server.port=8761java -jar eureka-sever-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka2 --server.port=8762java -jar eureka-sever-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka3 --server.port=8763
2.Provider
Maven中添加eureka-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
Config中添加@EnableDiscoveryClient注釋
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
配置文件
#多provide配置
---
spring:
profiles: provider1
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/
---
spring:
profiles: provider2
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8762/eureka/
Controller
/**
* @author BaoZhou
* @date 2018/6/29
*/
@RestController
public class TicketController {
@Autowired
TicketService ticketService;
@GetMapping("/ticket")
public String getTicket() {
return ticketService.getTicket();
}
}
啟動服務(wù)時(shí),只要將打出來的Jar包依次帶參執(zhí)行即可:
java -jar eureka-provider-0.0.1-SNAPSHOT.jar --spring.profiles.active = provider1 --server.port=8200java -jar eureka-provider-0.0.1-SNAPSHOT.jar --spring.profiles.active = provider2 --server.port=8201
3.Client
Maven中添加eureka-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
Config中添加@EnableDiscoveryClient注釋,并且將RestTemplate加入到容器中,@LoadBalanced可以開啟負(fù)載均衡,默認(rèn)是輪詢式。
@EnableDiscoveryClient //開啟發(fā)現(xiàn)服務(wù)功能
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Controller:調(diào)用restTemplate通過Http的方式訪問服務(wù),訪問的格式為應(yīng)用的名字+方法的名字
/**
* @author BaoZhou
* @date 2018/6/29
*/
@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/buy")
public String buyTicket(String name) {
/*application:name + methodName*/
return name+restTemplate.getForObject("http://PROVIDER/ticket", String.class);
}
}
配置文件:
server:
port: 8002
spring:
application:
name: comsumer-ticket
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka
三.運(yùn)行程序

負(fù)載均衡機(jī)制讓請求在兩個(gè)Provider上依次執(zhí)行,同時(shí)在三個(gè)Server上都可以看到Provider與Consumer服務(wù)。
四.Eureka與Dubbo的區(qū)別
- Dubbo 實(shí)踐通常以ZooKeeper 為注冊中心(Dubbo 原生支持的Redis 方案需要服務(wù)器時(shí)間同步,且性能消耗過大)。針對分布式領(lǐng)域著名的CAP理論(C——數(shù)據(jù)一致性,A——服務(wù)可用性,P——服務(wù)對網(wǎng)絡(luò)分區(qū)故障的容錯性),Zookeeper 保證的是CP ,但對于服務(wù)發(fā)現(xiàn)而言,可用性比數(shù)據(jù)一致性更加重要 ,而 Eureka 設(shè)計(jì)則遵循AP原則 。
- dubbo是二進(jìn)制的傳輸?shù)?,占用帶寬會更少,springCloud是http協(xié)議傳輸,帶寬會比較多,同時(shí)使用http協(xié)議一般會使用JSON報(bào)文,消耗會更大。
- Dubbo是RPC調(diào)用,Eureka的RestFul調(diào)用。