简介
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。
SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
实现流程
CAP原则:CAP原理讲解
Consistency(一致性)Availability(可用性)Partition tolerance(分区容错性),三者不可能同时实现
- 由于网络通信的不可靠,所有P是一定要保证的,
- C(一致性)要求我们多个服务器之间的数据要保持一致,因此要进行数据更新
- A(可用性),要求服务器一旦收到请求就要返回请求的结果
- 而因为网络不可靠,所以C(一致性)在进行数据更新期间是要锁起来的,这便与A(可用性)相矛盾(更新期间无法访问) 因此一般来说只有CP和AP,
zookeeper保证的是CP
当master节点因网络故障失去联系时,剩余节点会选择出一个leader节点。在leader节点的选举期间整个服务是不可用的
Eureka保证的是AP
Eureka节点之间是平等的,一个节点失去联系后会自动切换到下一个节点,但返回的信息可能不是最新的
在Eureka中有一个心跳机制,即服务提供者Server每30秒向Eureka发送一次心跳,如果90秒没有收到心跳就认为这个Server宕机了将其注销,如果超过85%的服务宕机了,则认为是网络故障,保持连接
Eureka 注册中心使用
导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
编写配置文件 在EurekaClientConfigBean类中有这么几段代码
public static final String PREFIX = "eureka.client";
public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX;
public static final String DEFAULT_ZONE = "defaultZone";
private Map<String, String> serviceUrl = new HashMap<>();
{
this.serviceUrl.put(DEFAULT_ZONE, DEFAULT_URL);
}
因此可以在配置文件中照猫画虎的设置dfaultZone
server:
port: 7001
eureka:
instance:
hostname: eureka1 #eureka 服务端实例的名称
client:
register_with_eureka: false #是否想eureka注册中心注册自己,false为不注册
fetch-registry: false #表示是否从Eureka Server获取注册的服务信息,false表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
# 向http://eureka1:7001/eureka/请求服务
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
开启Eureka功能 debug-无法使用@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaService7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaService7001.class,args);
}
}
运行结果
Eureka 服务注册
- 添加依赖 我们需要在之前的provider的pom文件中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>3.0.2</version>
</dependency>
- 添加eureka的配置
eureka:
client:
service-url:
# 向注册中心注册
defaultZone: http://eureka1:7001/eureka/
instance:
instance-id: providerdept-80801 # 修改默认显示信息
- 添加注解
@SpringBootApplication
@EnableEurekaClient //启动后注册到Eureka中
public class SApplication {
public static void main(String[] args) {
SpringApplication.run(SApplication.class,args);
}
}
运行结果
在正式的场景中我们往往有多个注册中心和多个服务
每一个注册中心都向其他注册中心注册自己
server:
port: 7001
eureka:
instance:
hostname: eureka1 #eureka 服务端实例的名称
client:
register_with_eureka: false #是否想eureka注册中心注册自己,false为不注册
fetch-registry: false #表示是否从Eureka Server获取注册的服务信息,false表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka1:7003/eureka/,http://eureka2:7002/eureka/
服务提供者向每一个注册中心注册
eureka:
client:
service-url:
defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/
instance:
instance-id: providerdept-80801
info:
app.name: appname-lin
company.name: companyname-lin
此处需在C盘 -> Windows -> System32 -> drives -> etc -> hosts文件; 添加配置
127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka3
获取服务信息
当我们点击providerdept-80801时会跳转到对应端口号下的"http://xxx:xxx/actuator/info" 其中可以显示一些服务相关信息
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
编写配置文件
eureka:
client:
service-url:
defaultZone: http://eureka1:7001/eureka/
instance:
instance-id: providerdept-80801
# 配置一些信息
info:
app.name: appname-lin
company.name: companyname-lin
运行结果:
我们也可以
通过DicoveryClient获取
@Autowired
//获取具体的微服务信息
DiscoveryClient client;
@GetMapping("/dev/discovery")
//获取注册进来的微服务,
public Object discovery(){
//微服务清单
List<String> services = client.getServices();
List<ServiceInstance> instances = client.getInstances("PROVIDER-NAME");
System.out.println("service------"+services);
//具体微服务
for (ServiceInstance instance : instances){
System.out.println(instance.getHost()+"\t"
+instance.getPort()+"\t"
+instance.getUri()+"\t"
+instance.getInstanceId()
);
}
return this.client;
}
@SpringBootApplication
@EnableEurekaClient //启动后注册到Eureka中
@EnableDiscoveryClient//获取服务信息
public class SApplication {
public static void main(String[] args) {
SpringApplication.run(SApplication.class,args);
}
}
debug
无法使用@EnableEurekaServer注解
导入的依赖错误
原本的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
正确的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>