1.服务治理
主要用来实现各个微服务实例的自动化注册与发现
服务注册:服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,
注册中心按服务名分类组织服务清单
服务发现:调用方需要向服务注册中心咨询服务,并获取所有服务的实例清单,以实现对具体服务实例的访问
2.Eureka服务注册中心
① @EnableEurekaServer 启动一个服务注册中心给其他应用进行对话
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } } |
②配置application.yml
eureka: instance: hostname: localhost client: register-with-eureka: false #是否注册到eureka service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/# eureka服务器的地址 fetch-registry: false spring: application: name: eureka server: port: 8761 |
@SpringBootApplication @EnableDiscoveryClient public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } } |
②配置application.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client
server:
port: 8080 |
补充:当缺少spring-boot-starter-web包时启动报错
2018-05-04 15:11:17.823 INFO 9904 --- [ Thread-14] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1bd39d3c: startup date [Fri May 04 15:11:15 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@38c6f217 2018-05-04 15:11:17.823 INFO 9904 --- [ Thread-14] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application client with eureka with status DOWN 2018-05-04 15:11:17.824 WARN 9904 --- [ Thread-14] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1525417877824, current=DOWN, previous=UP] 2018-05-04 15:11:17.825 INFO 9904 --- [ Thread-14] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0 2018-05-04 15:11:17.826 INFO 9904 --- [ Thread-14] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ... 2018-05-04 15:11:17.841 INFO 9904 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/DESKTOP-O0E1C9K:client:8080 - registration status: 204 2018-05-04 15:11:17.841 INFO 9904 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/DESKTOP-O0E1C9K:client:8080: registering service... 2018-05-04 15:11:17.845 INFO 9904 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/DESKTOP-O0E1C9K:client:8080 - registration status: 204 2018-05-04 15:11:17.846 INFO 9904 --- [ Thread-14] com.netflix.discovery.DiscoveryClient : Unregistering ... 2018-05-04 15:11:17.854 INFO 9904 --- [ Thread-14] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT/DESKTOP-O0E1C9K:client:8080 - deregister status: 200 2018-05-04 15:11:17.863 INFO 9904 --- [ Thread-14] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient 2018-05-04 15:11:17.863 INFO 9904 --- [ Thread-14] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2018-05-04 15:11:17.864 INFO 9904 --- [ Thread-14] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> |
4.高可用Eureka
将自己作为服务向其他服务注册中心注册自己
#配置节点1的application.yml eureka: instance:hostname: peer1 client: register-with-eureka: false service-url: defaultZone: http://peer2:8762/eureka/ fetch-registry: false spring: application: name: eureka server: port: 8761 |
#配置节点2的application.yml eureka: |
5.服务提供者
①服务注册
·启动时通过REST请求将自己注册到Eureka Server上,同时携带自身服务的一些元数据信息
·Eureka Server接收到请求后,将元数据信息存储在一个双层结构Map中,第一层key为服务名,第二层key为服务实例名
eureka: client: register-with-eureka: true #是否启动注册 |
②服务同步
两个服务注册中心之间相互注册为服务。 当一个服务提供者注册到一个服务注册中心时,
该请求转发给集群中相连的其他注册中心,从而实现注册中心之间的服务同步。服务提供
者的服务信息可以通过任意服务注册中心获取。
③服务续约
注册完成之后,服务提供者维护一个心跳检测服务是否活着,防止Eureka Server 的剔除任务。
eureka: instance: lease-renewal-interval-in-seconds:30 #续约任务调用间隔时间 lease-expiration-duration-in-seconds:90 #服务时效时间 |
6.服务消费者
①获取服务
发送REST请求给服务注册中心,获取注册的服务清单。
Eureka Server会维护一份只读的服务清单,同时该缓存清单每隔30秒更新一次。
eureka: client: fetch-registry: true #是否获取服务 registry-fetch-interval-seconds: 30 #设置缓存清单刷新时间 |
②服务调用
通过服务名获得具体提供服务的实例名和实例的元数据信息。
③服务下线
关闭或重启服务的某个实例。触发一个服务下线的REST请求给Eureka Server,服务端将服务状态置为下线(DOWN)
7.服务注册中心
①失效剔除
Eureka Server在启动时创建一个定时任务,默认每个一段时间(60秒)将当前清单中超时(90秒)没有续约的服务剔除
②自我保护
统计心跳失败的比例在15分钟之内是否低于85%,Eureka Server将当前实例注册信息保护起来
eureka: server: enable-self-preservation: true #是否开启自我保护 |