相关资料:
一.什么是Eureka
Eureka的本意是我发现了,我找到了的意思,它同时也是SpringCloud中的Netflix 出品的用于实现服务注册和发现的工具。首先了解一些概念:

- Eureka Server:提供服务发现能力,各个微服务启动时,会向 Eureka Server 注册自己的信息,Eureka Server 会保存这些信息
- Eureka Client:是一个 Java 客户端,用于简化与Eureka Server的交互,Eureka Client每30秒发送心跳,如果服务端没有接收到心跳信号。它将在90秒后从服务器注册表中删除。
- Eureka自我保护机制:自我保护模式正是一种针对网络异常波动的安全保护措施,使用自我保护模式能使Eureka集群更加的健壮、稳定的运行。自我保护机制的工作机制是如果在15分钟内超过85%的客户端节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,Eureka Server自动进入自我保护机制,可以通过
enable-self-preservation
开启,此时会出现以下几种情况:- Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。
- Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用。
- 当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中。
因此Eureka Server可以很好的应对因网络故障导致部分节点失联的情况,而不会像ZK那样如果有一半不可用的情况会导致整个集群不可用而变成瘫痪。
二.实践部分
程序主要分为三部分,在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配置文件,主要是要把几个Server之间要互相注册,服务注册时只要在其中一个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
启动服务时,只要将打出来的Jar包依次带参执行即可:
java -jar eureka-sever-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka1 --server.port=8761
java -jar eureka-sever-0.0.1-SNAPSHOT.jar --spring.profiles.active = eureka2 --server.port=8762
java -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();
}
}
启动服务时,只要将打出来的Jar包依次带参执行即可:
java -jar eureka-provider-0.0.1-SNAPSHOT.jar --spring.profiles.active = provider1 --server.port=8200
java -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
可以开启负载均衡,默认是轮询式。
@EnableDiscoveryClient //开启发现服务功能
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Controller:调用restTemplate通过Http的方式访问服务,访问的格式为应用的名字+方法的名字
/**
* @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
三.运行程序

负载均衡机制让请求在两个Provider上依次执行,同时在三个Server上都可以看到Provider与Consumer服务。
四.Eureka与Dubbo的区别
- Dubbo 实践通常以ZooKeeper 为注册中心(Dubbo 原生支持的Redis 方案需要服务器时间同步,且性能消耗过大)。针对分布式领域著名的CAP理论(C——数据一致性,A——服务可用性,P——服务对网络分区故障的容错性),Zookeeper 保证的是CP ,但对于服务发现而言,可用性比数据一致性更加重要 ,而 Eureka 设计则遵循AP原则 。
- dubbo是二进制的传输的,占用带宽会更少,springCloud是http协议传输,带宽会比较多,同时使用http协议一般会使用JSON报文,消耗会更大。
- Dubbo是RPC调用,Eureka的RestFul调用。
网友评论