之前,已经新建了microservicecloud-api,microservicecloud-consumer-dept-80,microservicecloud-provider-8001三个模块,并且测试了restTemplate。
Eureka Server 提供服务注册和发现,Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到,Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务。
搭建eureka服务注册中心Module microservicecloud-eureka-9001
pom文件如下
<?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>microservicecloud</artifactId>
<groupId>com.example.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-eureka-9001</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
yml文件如下
server:
port: 9001
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
EurekaServer9001_App主启动类
package com.example.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer9001_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer9001_App.class,args);
}
}
通过使用注解@EnableEurekaServer 使用Eureka服务器
测试时可以通过http://localhost:9001/
No application available,此时application中为空,因为没有注入任何服务
改造microservicecloud-provider-8001,将其注入到eureka中
pom文件修改
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
yml文件修改
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:9001/eureka
DeptProvider8001_App主启动类增加@EnableEurekaClient
在启动EurekaServer后再启动服务
actuator与注册微服务信息完善
- 主机名称:服务名称修改
yml修改
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:9001/eureka
instance:
instance-id: microservicecloud-dept8001
- 访问信息有IP信息提示
yml修改
instance:
instance-id: microservicecloud-provider8001
prefer-ip-address: true
- 微服务info内容详细信息
info:
app.name: zy-example-microservicecloud
company.name: zy_123
build.artifactId: $project.artifactId$
build.version: $project.version$
pom文件
总得父工程
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
microservicecloud-provider-8001 的pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
eureka自我保护
原因是某时刻某一个微服务不可用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存
microservicecloud-provider-dept-8001服务发现Discovery
在服务注册到Server中后,需要通过服务发现来获得该服务的信息
修改microservicecloud-provider-dept-8001的controller
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
public Object discovery()
{
List<String> list = client.getServices();
System.out.println("**********" + list);
List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT");
for (ServiceInstance element : srvList) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return this.client;
}
修改主启动类
DeptProvider8001_App
@EnableDiscoveryClient
Eureka集群配置
新建microservicecloud-eureka-9002/microservicecloud-eureka-9003
按照9001为模板粘贴POM和主启动类
修改pom文件主要修改defaultZone
9001配置
http://localhost:9002/eureka/,http://localhost:9003/eureka/
9002配置
http://localhost:9001/eureka/,http://localhost:9003/eureka/
9003配置
http://localhost:9001/eureka/,http://localhost:9002/eureka/
修改microservicecloud-provider-dept-8001的yml文件
defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/,http://localhost:9003/eureka/
启动集群,启动服务即可进行测试
但此时两个服务中心,但是都在unavailable-replicas中
需要修改配置文件以9001为例子
server:
port: 9001
eureka:
instance:
hostname: eureka9001.com
# hostname: localhost
client:
register-with-eureka: true #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://localhost:9002/eureka/,http://localhost:9003/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
spring:
application:
name: microservicecloud-eureka
需要设置register-with-eureka: true 以及应用名称,保证集群应用名称相同
作为服务注册中心,Eureka比Zookeeper好在哪里
作为服务注册中心,Eureka比Zookeeper好在哪里
著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性)、A(可用性)和P(分区容错性)。由于分区容错性P在是分布式系统中必须要保证的,因此我们只能在A和C之间进行权衡。
因此
Zookeeper保证的是CP,
Eureka则是AP。
1 Zookeeper保证CP
当向注册中心查询服务列表时,我们可以容忍注册中心返回的是几分钟以前的注册信息,但不能接受服务直接down掉不可用。也就是说,服务注册功能对可用性的要求要高于一致性。但是zk会出现这样一种情况,当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行leader选举。问题在于,选举leader的时间太长,30 ~ 120s, 且选举期间整个zk集群都是不可用的,这就导致在选举期间注册服务瘫痪。在云部署的环境下,因网络问题使得zk集群失去master节点是较大概率会发生的事,虽然服务能够最终恢复,但是漫长的选举时间导致的注册长期不可用是不能容忍的。
2 Eureka保证AP
Eureka看明白了这一点,因此在设计时就优先保证可用性。Eureka各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其它节点,只要有一台Eureka还在,就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。除此之外,Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现以下几种情况:
- Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
- Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用)
- 当网络稳定时,当前实例新的注册信息会被同步到其它节点中
因此, Eureka可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪。