啥是服务治理?
服务治理就是注册中心?Spring Cloud使用什么软件作为注册中心?
我们要知道微服务之间相互调用需要知道对方服务器的IP和端口,服务治理就是管理各个服务的地址和状态。
在我们现在的项目中,可能大家最常用的就是Eureka 这也是面试官们最常问的一个模块,但是还有一点希望大家也都清楚,那就是Eureka 社区已经停更了,可能以后Eureka 会慢慢的淡出我们的视野中,那么我们都有神马技术可以去代替呢?
我们本次就来聊聊!
一、Eureka
• Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。
• Eureka 和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 Spring Cloud 社区整合为
Spring-Cloud-Netflix 模块。
• Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。
虽然Eureka 已经停更了,但是我感觉还是有必要把Eureka 的入门搭建给大家介绍一下
1、Eureka Server搭建
① 创建 eureka-server 模块(注册中心)
② 引入 SpringCloud 和 euraka-server 相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
③ 完成 Eureka Server 相关配置
server:
port: 8761
# eureka 配置
# eureka 一共有4部分 配置
# 1. dashboard:eureka的web控制台配置
# 2. server:eureka的服务端配置
# 3. client:eureka的客户端配置
# 4. instance:eureka的实例配置
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
register-with-eureka: false # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: false # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要
server:
enable-self-preservation: false # 关闭自我保护机制
eviction-interval-timer-in-ms: 3000 # 检查服务的时间间隔
④ 启动该模块
@SpringBootApplication
// 启用EurekaServer
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
启动服务,访问 http://localhost:8761/,就可以打开Eureka 的控制台了,大家一定会感到格外的亲切
2、Eureka Client
① 引 eureka-client 相关依赖
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
② 完成 eureka client 相关配置
server:
port: 9000
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
spring:
application:
name: eureka-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
③ 启动 测试
@EnableEurekaClient //该注解 在新版本中可以省略
@SpringBootApplication
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
3、完成了Eureka 的搭建,那我们怎么获得对应的请求路径呢?
- 首先在对应的服务启动类上添加注解@EnableDiscoveryClient // 激活DiscoveryClient
- 然后在对应的controller中注入DiscoveryClient
- 最后通过List instances = discoveryClient.getInstances(“EUREKA-PROVIDER”); 就可以获取对应的实例,完成调用
4、Eureka 自我保护机制
-
Eureka作为注册中心,需要知道其中注册的应用的状态是否可用
-
客户端应用需要设置向注册中心Eureka发生心跳检查的时间,和超时删除应用的时间
eureka: instance: lease-renewal-interval-in-seconds: 30 # 每隔30 秒发一次心跳包 lease-expiration-duration-in-seconds: 90 # 如果90秒没有发心跳包,服务器呀,你把我干掉吧~
-
服务器设置了检查应用的时间间隔
eureka: server: enable-self-preservation: false # 关闭自我保护机制 eviction-interval-timer-in-ms: 3000 # 检查服务的时间间隔
-
保护机制的目的
1. 当注册中心忽然失去大量心跳,低于预期85%,为了保障服务的可用,进入自我保护状态,暂时不进行应用的移除。 2. 当心跳恢复超过期望值,会退出自我保护状态,再次移除应用。
5、Eureka高可用
保证Eureka高可用,就是搭建Eureka集群。
搭建的步骤也很简单
-
创建两个Eureka server应用
-
配置
server: port: 8761 eureka: instance: hostname: eureka-server1 # 主机名 client: service-url: defaultZone: http://eureka-server2:8762/eureka register-with-eureka: true # 是否将自己的路径 注册到eureka上。eureka server 不需要的,eureka provider client 需要 fetch-registry: true # 是否需要从eureka中抓取路径。eureka server 不需要的,eureka consumer client 需要 spring: application: name: eureka-server-ha
hostname主机名不能一致,需要使用hosts配置为不同。
defaultZone 为集群中其他Eureka server的地址,多个使用,逗号分隔。
application.name应用名称要一致
-
修改hosts文件。C:\Windows\System32\drivers\etc
127.0.0.1 eureka-server1 127.0.0.1 eureka-server2
-
客户端也同理 配置集群中所有Eureka的地址,使用,逗号分隔。
client:
service-url:
defaultZone: http://eureka-server1:8761/eureka,http://eureka-server2:8762/eureka
Eureka 我们聊的差不多了,我们接下来就该看看他的替代品,zookeeper 和 Consul
二、Zookeeper
啥是Zookeeper呢?
- Zookeeper 是 Apache Hadoop 项目下的一个子项目,是一个树形目录服务。
- Zookeeper 翻译过来就是 动物园管理员,他是用来管 Hadoop(大象)、Hive(蜜蜂)、Pig(小猪)的管理员。简称zk
- Zookeeper 是一个分布式的、开源的分布式应用程序的协调服务。
- Zookeeper 提供的主要功能包括:配置管理;分布式锁;集群管理
- zookeeper本质是:树形目录服务
1、ZooKeeper 安装
ZooKeeper服务器是用Java创建的,它运行在JVM之上。需要安装JDK 7或更高版本。
将下载的ZooKeeper放到/opt/ZooKeeper目录下
#上传zookeeper alt+p
put d:/setup/apache-zookeeper-3.5.6-bin.tar.gz
#打开 opt目录
cd /opt
#创建zooKeeper目录
mkdir zooKeeper
#将zookeeper安装包移动到 /opt/zooKeeper
mv apache-zookeeper-3.5.6-bin.tar.gz /opt/zookeeper/
将tar包解压到/opt/zookeeper目录下
tar -zxvf apache-ZooKeeper-3.5.6-bin.tar.gz
配置zoo.cfg
进入到conf目录拷贝一个zoo_sample.cfg并完成配置
#进入到conf目录
cd /opt/zooKeeper/apache-zooKeeper-3.5.6-bin/conf/
#拷贝
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg
#打开目录
cd /opt/zooKeeper/
#创建zooKeeper存储目录
mkdir zkdata
#修改zoo.cfg
vim /opt/zooKeeper/apache-zooKeeper-3.5.6-bin/conf/zoo.cfg
修改存储目录:dataDir=/opt/zookeeper/zkdata
启动ZooKeeper
cd /opt/zooKeeper/apache-zooKeeper-3.5.6-bin/bin/
#启动
./zkServer.sh start
看到上图表示ZooKeeper成功启动
查看ZooKeeper状态
./zkServer.sh status
zookeeper启动成功。standalone代表zk没有搭建集群,现在是单节点
2、Zookeeper 核心理论
Zookeepe集群角色
在ZooKeeper集群服中务中有三个角色:
- Leader 领导者 :
1. 处理事务请求
2. 集群内部各服务器的调度者
- Follower 跟随者 :
1. 处理客户端非事务请求,转发事务请求给Leader服务器
2. 参与Leader选举投票
- Observer 观察者:
- 处理客户端非事务请求,转发事务请求给Leader服务器
Leader负责的是所有客户端的 “写” 操作请求, 将最新的数据同步给所有follower节点
follower负责是所有客户端的 “读” 操作请求
主写从读
如果Leader宕机, 先重新投票,选举出Leader后, 对外才可以提供服务, 选举时间30-120s之间。
三、Cansul
Consul 是什么?
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。
它提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点。包括:
- 基于 raft 协议,比较简洁;
- 支持健康检查, 同时支持 HTTP 和 DNS 协议
- 支持跨数据中心的 WAN 集群
- 提供图形界面 跨平台,支持 Linux、Mac、Windows
spring cloud consul 教会了我们应该怎么玩
在Consul的官网下载我们需要的版本后我们直接双击运行consul.exe文件即可;可以通过consul -version 查看版本信息
Consul 官网
使用开发模式启动,在cmd里面我们直接输入consul agent -dev即可,然后通过以下地址可以访问Consul的首页:http://localhost:8500
以上我们就完成了consul的安装和配置,可以直接进入使用了!
搭建Consul server
① 创建 consul-server 模块(注册中心)
② 引入 SpringCloud 和 consul-server 相关依赖
<dependencies>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
③ 完成 Consul Server 相关配置
###consul服务端口号
server:
port: 8006
spring:
application:
name: consul-provider-payment
####consul注册中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
④ 启动该模块
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
启动后即可在控制台看到新的服务注册到consul上了
消费端类似,便不再重复写了
以上三者的异同点
这里涉及到了CAP理论 ,之前也和大家讨论过,不记得的小伙伴可以观看 分布式事务理论