版本选择一、Eureka(AP-高可用)
1、注册中心集群配置2、服务提供者集群配置3、服务发现Discovery初探4、自我保护机制
1、介绍2、关闭自我保护 二、Zookeeper(CP-一致性)
版本选择
本次教程所用版本
cloud Hoxton.SR1 boot 2.2.2.RELEASE cloud Alibaba 2.1.0.RELEASE Java 8 maven 3.5及以上 Mysql 5.7及以上
SpringCloud版本选择
1、Json
2、Cloud官网
点击Reference Doc.
3、cloud升级
一、Eureka(AP-高可用)
1、注册中心集群配置org.springframework.cloud spring-cloud-starter-netflix-eureka-server 注册中心 @EnableEurekaServer 服务 @EnableEurekaClient org.springframework.cloud spring-cloud-starter-netflix-eureka-client
1、本机host文件
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com
2、注册中心配置文件
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
3、服务集群注册
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ ...
2、服务提供者集群配置
1、添加负载均衡@LoadBanlanced
@Bean
@LoadBalanced //默认轮询机制
public RestTemplate restTemplate() {
return new RestTemplate();
}
2、RestTemplate指向服务名称
public static final String PAYMENT_URL = "http://cloud-payment-service";
//restTemplate用法
@GetMapping("/consumer/payment/create")
public CommonResult create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult get(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/ " + id, CommonResult.class);
}
3、自定义主机名称及让访问信息有ip显示
eureka: instance: instance-id: payment8001 #自定义主机名 prefer-ip-address: true #访问路径显示ip3、服务发现Discovery初探
主启动类@EnableDiscoveryClient
@GetMapping("/payment/discovery")
public Object discovery() {
List services = discoveryClient.getServices();
for (String service : services) {
log.info("service:" + service);
}
List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
//获取对应服务的信息
log.info(instance.getServiceId() + "t" + instance.getHost() + "t" + instance.getPort() + "t" + instance.getUri());
}
return this.discoveryClient;
}
4、自我保护机制
1、介绍
为什么会产生自我保护机制?
为防止EurekaClient可以正常运行,但是与EurekaServer网络不同的情况下,EurekaServer不会立刻将EurekaClient服务剔除。
什么是自我保护机制?
默认情况下,当Eureka server在一定时间内没有收到实例的心跳,便会把该实例从注册表中删除(默认是90秒),但是,如果短时间内丢失大量的实例心跳,便会触发eureka server的自我保护机制。
比如在开发测试时,需要频繁地重启微服务实例,但是我们很少会把eureka server一起重启(因为在开发过程中不会修改eureka注册中心),当一分钟内收到的心跳数大量减少时,会触发该保护机制。可以在eureka管理界面看到Renews threshold和Renews(last min),当后者(最后一分钟收到的心跳数)小于前者(心跳阈值)的时候,触发保护机制,会出现红色的警告:
EMERGENCY!EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT.RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEGING EXPIRED JUST TO BE SAFE.
从警告中可以看到,eureka认为虽然收不到实例的心跳,但它认为实例还是健康的,eureka会保护这些实例,不会把它们从注册表中删掉。
在自我保护模式中,EurekaServer会保护服务注册表中的信息,不再注销任何服务实例。
综上,自我保护模式是一种应对网络异常的安全保护措施它的架构哲学是宁可同时保留所有微服务,也不忙保姆注销如何健康的微服务,使用自我保护模式,可以让Eureka集群更加健壮,稳定。
署于CAP 的AP分支(高可用)
2、关闭自我保护1、注册中心配置
eureka: server: #关闭自我保护机制 enable-self-preservation: false #多久没收到心跳就剔除服务 eviction-interval-timer-in-ms: 2000
2、客户端配置
eureka: instance: #发送心跳的频率(单位为秒,默认30) lease-renewal-interval-in-seconds: 1 #服务端最后一次心跳等待上限时间(单位为秒,默认90),超时剔除 lease-expiration-duration-in-seconds: 2
二、Zookeeper(CP-一致性)
org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.4.9 org.slf4j slf4j-log4j12
1、配置yml
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.56.10:2181
2、linux操作
3、获得json串
{"name":"cloud-provider-payment","id":"df07725f-ce65-4fc2-b5ed-fb4a61ec8a71","address":"localhost","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{}},"registrationTimeUTC":1644322847645,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
4、服务节点是临时节点(一致性)



