微服务组件 – 服务治理 一、服务治理 服务治理是微服务架构中最为核心和基础的模块,它是用于实现各个微服务实例的自动化注册与发现 。
1、为什么需要服务治理 我们可以通过做一些静态配置来完成服务的调用。比如,有两个服务A和B,其中服务A需要调用服务B来完成一个业务操作时,我们需要手工维护服务B的具体实例清单。
但是随着业务的发展,系统功能越来越复杂,相应的微服务应用也不断增加。我们的静态配置就会变得越来越难以维护。同时,我们集群规模、服务的位置、服务的命名等都有可能发生变化。
如果我们依旧使用静态配置的方式,会消耗大量的人力,甚至可能会出现错误。
2、服务治理管理什么 服务治理的这些框架和产品的实现都围绕着服务注册 和服务发现 机制来完成对微服务应用实例的自动化管理。
服务注册
在服务治理框架中,通常会构建一个注册中心 ,每个微服务向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告诉注册中心。
注册中心还需要以心跳的方式去监测清单中的服务是否可用,若不可用需要从清单中剔除,达到排除故障服务的效果。
服务发现
通过服务治理的框架,服务间的调用不在通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。调用方需要向服务注册中心咨询某一个服务, 并获取该服务的所有实例清单。
比如,服务C调用服务A,服务C就需要向注册中心发起咨询请求,服务C获得了服务A的可用位置。
二、Eureka 1、Eureka介绍 Spring Cloud Eureka
是Spring Cloud Netflix
微服务套件中的一部分。
Spring Cloud Eureka
,使用Netflix Eureka
来实现注册与发现,它既包含了服务端组件,也包含客户端组件。
在Eureka服务治理体系中,有三个核心角色:注册中心,服务提供者,服务消费者。
2、Eureka实例 2.1、Eureka服务端 依赖环境 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-eureka-server</artifactId > </dependency > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > Brixton.SR5</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement >
properties文件 1 2 3 4 5 6 7 8 9 server.port =1111 eureka.instance.hostname =localhost eureka.client.register-with-eureka =false eureka.client.fetch-registry =false eureka.client.service-url.defaultZone =http://${eureka.instance.hostname}:${server.port}/eureka/
添加注解 1 2 3 4 5 6 7 8 9 @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main (String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
启动
2.2、Eureka客户端 引入依赖 1 2 3 4 5 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > </dependency >
properties文件 1 2 3 4 5 server.port =8888 spring.application.name =eurekaclient eureka.client.service-url.defaultZone =http://localhost:1111/eureka
添加注解 1 2 3 4 5 6 7 8 9 @SpringBootApplication @EnableEurekaClient public class EurekaClientApplication { public static void main (String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
启动
3、高可用注册中心 在微服务架构这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署 。
Eureka Server 的设计一开始就考虑了高可用问题。在Eureka的服务治理设计中,所有节点即是服务提供方,也是服务消费方,服务注册中心也不例外。
是否还记得在单节点的配置中,我们设置过下面的两个参数,让服务注册中心不注册自己。
1 2 eureka.client.register-with-eureka =false eureka.client.fetch-registry =false
Eureka Server的高可用实际上就是将自己作为服务向其他服务注册中心注册自己 ,这样就可以形成一组相互注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果。
1 2 3 4 5 6 7 8 9 10 11 12 13 spring.application.name = eureka-server server.port = 1111 eureka.instance.hostname = peer1 eureka.client.serviceUrl.defaultZone =http://peer2:1112/eureka/ spring.application.name = eureka-server server.port = 1112 eureka.instance.hostname = peer2 eureka.client.serviceUrl.defaultZone =http://peer1:1111/eureka/
4、Eureka停止更新 在1.x版本项目还是活跃的,但是在2.x版本中停止维护。
三、Consul https://www.consul.io
1、Consul简介 Consul是一个可以提供服务发现,健康检查,多数据中心,Key/Value存储等功能的分布式服务框架,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,使用起来也较为简单。Consul用Golang实现,因此具有天然可移植性(支持Linux、Windows和Mac OS X);安装包仅包含一个可执行文件,方便部署。
安装Consul:https://www.consul.io/downloads
启动Consul服务:consul agent -dev
访问consul的web服务端口:http://localhost:8500
2、开发Consul客户端 引入依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-consul-discovery</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-actuator</artifactId > </dependency > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > ${spring-cloud.version}</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement >
properties文件 1 2 3 4 server.port =8888 spring.application.name =client spring.cloud.consul.host =localhost spring.cloud.consul.port =8500
启动