Eureka服务注册中心构建
1、Eureka基本架构及初步原理
SpringCloud封装了Netflix公司开发的Eureka模块来实现服务注册和发现。
Eureka采用了C-S的设计架构。Eureka作为服务注册功能的服务器,它是服务注册中心。
而系统中的其他微服务,采用Eureka 的客户端连接到Eureka Server并维持心跳连接,这样系统的维护人员就可以通过Eureka Server来作为监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(比如Zuul)就可以通过Eureka Server来发现系统中的其他微服务,并执行相关的逻辑。
Eureka 包含两个组件:Eureka Server和Eureka Client
Eureka Server提供服务注册服务
各个节点启动后,会在Eureka Server中进行注册,这样 Eureka Server中的服务注册表将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直接的看到。
Eureka Client是一个java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮训(round-robin)负载算法的负载均衡器,在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接到某个节点的心跳,Eureka Server将会从服务注册中把这个服务节点移除(默认90秒)。
2、Eureka的三大角色
- Eureka Server 提供服务注册和发现
- Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到。
- Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务
假如我们需要引入一个新技术组件基本上有两步。(这里以Eureka为例)
- 新增一个相关的maven坐标
<!--eureka-server 服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
-
在主启动类上标注新组件的相关注解标签。这里需要添加的是:@EnableEurekaClient注解
-
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>springcloud-study</artifactId>
<groupId>com.blj</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-study-eureka-7001</artifactId>
<dependencies>
<!--eureka-server 服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
- yml配置文件编写
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
- 编写主启动类
package com.blj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//Eureka Server服务器端启动类,代表可以接受其他服务注册进来。
public class EurekaServer7001_App {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001_App.class,args);
}
}
- 测试
在浏览器上输入:localhost:7001
如果出现下面界面说明Eureka Server启动成功
3、将已有的部门微服务注册进Eureka服务中心
- 8001provider服务中添加pom文件依赖
<!--将微服务provider注册进eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 8001provider服务中修改yml配置
#客户端注册进eureka服务列表
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
- 8001 启动类增加@EnableEurekaClient注解
package com.blj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient //本服务启动后会自动注册到Eureka服务中
@SpringBootApplication
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class,args);
}
}
4、actuator与注册微服务信息完善
actuator:在springboot中主管监控和配置。
主机名称、服务名称修改
- 修改8001的yml配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#服务实例名称修改
instance:
instance-id: cloud-dept8001
访问信息有ip信息提示
- 修改8001yml配置
#客户端注册进eureka服务列表
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: cloud-dept8001 #自定义服务名称信息
prefer-ip-address: true #房屋路径可以显示ip地址
微服务info内容详细信息
- 修改8001的pom文件,增加actuator的配置
<!--actuator监控自信息完善-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 父工程pom文件修改,增加build信息
<build>
<!--finalName 父工程名称-->
<finalName>spring-cloud-study</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>
- 修改8001的yml,增加info信息
#客户端注册进eureka服务列表
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: cloud-dept8001 #自定义服务名称信息
prefer-ip-address: true #房屋路径可以显示ip地址
info:
app.name: study-springcloud-micoservices
company.name: www.blj.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
Eureka自我保护机制
什么是自我保护模式
默认情况下,如果Eureka Server在一定时间内没有接受到某个微服务示例的心跳,Eureka Server将会注销示例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka之间无法正常通信,以上行为可能变得非常危险了。因为当微服务本身其实是健康的,此时不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当Eureka Server节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式,一旦进入该模式后,该Eureka Server节点会自动退出自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例,当它收到的心跳重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话就是:好死不如赖活着。
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定
Eureka服务发现
对于注册进eureka里面的服务,可以通过服务发现获得该服务的信息
供消费者调用
添加服务发现接口
对于注册进eureka里面的服务,可以通过服务发现获得该服务的信息
系统中的微服务可以通过Eureka的服务发现去获得在Eureka中注册的服务的信息,这是一个对外暴露的接口。
- 1.注入DiscoveryClient 对象(spring包下的),在controller方法中获取
@Autowired
private DiscoveryClient discoveryClient;
@ResponseBody
@GetMapping("/provider/discovery")
public Object discovery(){
List<String> list = discoveryClient.getServices();
System.out.println(list);
List<ServiceInstance> insList = discoveryClient.getInstances("SPRINGCLOUD-STUDY-DEPT");
for (ServiceInstance si:insList) {
System.out.println(si.getHost() +"," + si.getServiceId() +"," +si.getPort() +"," +si.getUri() +"," +si.getMetadata());
}
return this.discoveryClient;
}
- 2.在主启动类中加入@EnableDiscoveryClient注解
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Provider8001_APP {
public static void main(String[] args) {
SpringApplication.run(Provider8001_APP.class,args);
}
}
-
启动,调用http://localhost:8001/dept/discovery
8001需要等待注入一段时间,注入进server -
测试:在80消费端中增加相应调用
@RequestMapping(value = "/dept/discovery",method = RequestMethod.GET)
public Object discovery(){
return restTemplate.getForObject(
REST_URL_PREFFIX+"/dept/discovery",
Object.class);
}
Eureka集群配置
1、新建7004、7003Eureka服务项目
复制7001的pom、yml文件、主启动类
2、修改域名映射,修改hosts文件(路径在:C:\Windows\System32\drivers\etc\hosts)
必须修改,否则不成功
找到之后,打开直接添加下面内容即可
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
127.0.0.1 eureka7004.com
3.修改7001、7004、7003对应的yml配置
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
service-url:
# 单机版配置
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7004.com:7004/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
4.在8001配置注册
修改yml中集群的配置,使8001同时注册进7001,7003,7004集群环境
#配置服务器信息
server:
port: 8001
#数据库信息配置
spring:
application:
name: study-springcloud-dept #微服务名称
datasource:
url: jdbc:mysql://localhost:3306/clouddb01?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root1234
driver-class-name: com.mysql.jdbc.Driver #mysql驱动包
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
#druid连接池配置
initialSize: 5 #初始化连接数
minIdle: 5 #数据库连接池最小连接维持数
maxActive: 20 #数据库最大活动连接数
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉监控界面sql无法统计,‘wall’用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
userGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#Mybatis配置
mybatis:
#type-aliases-package: com.blj.springcloud.entities #所有Entities实体类别名所在包
mapper-locations: classpath:mybatis/mapper/**/*.xml #mapper映射文件
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
cache-enabled: true #开启二级缓存
type-aliases-package: com.blj.springcloud.entities #所有Entities实体类别名所在包
#客户端注册进eureka服务列表
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://localhost:7001/eureka,http://localhost:7004/eureka,http://localhost:7003/eureka
instance:
instance-id: cloud-dept8001 #自定义服务名称信息
prefer-ip-address: true #房屋路径可以显示ip地址
info:
app.name: study-springcloud-micoservices
company.name: www.blj.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
至此有三个eureka集群,一个微服务提供者
Eureka与Zookeeper的区别
CAP理论
- C:consistency 强一致性
- A:Availability 可用性 (HA高可用)
- P:Partition tolerance 分区容错性
著名的CAP理论指出,一个分布式系统不可能同时满足C(-致性)、A(可用性)和P(分区容错性)。 由于分区容错性P在是分布式系统中必须要保证的,因此我们只能在A和CZ间进行权衡。
因此
Zookeeper保证的是CP,
Eureka则是AP
- Zookeeper保证CP
当向注册中心查询服务列表时,我们可以客忍注册中心返回的是几分钟以前的注册信息,但不能接受服务直接down掉不可用。也就是说,服务注册功能对可用性的要求要高于一 敬性。但是zk会出现这样一 种情况,当master节点因为网络故障与其他节点失去联
系时,剩余节点会重新进行leaden选举。问题在F,选举leader的时间太长, 30 ~ 120s,日选举期间整个zk集群都是不可用的,这就导致在选举期间注册服务摊疾。在云部需的环境下,因网络问题使得zk集群失去master节点是较大概率会发牛的事虽然服务能
够最终恢复,但是漫长的选举时间导致的注册长期不可用是不能各忍的。
- Eureka保证AP
Eureka看明白了这一点, 因此在设计时就优先保证可用性。Eureka各 个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余的节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册或时如果发现连接失败,则会自动切换至其
它节点,只要有一台Eureka还在, 就能保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。 除此之外,Eureka还有一种自我保护机制,如果在1 5分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中
心出现了网络故障,此时会出现以下几种情况:
-
Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务
-
Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用)
3.当网络稳定时,当前实例新的注册信息会被同步到其它节点中
因此, Eureka可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪。