主机名称:服务名称修改(也就是将IP地址,换成可读性高的名字)
修改cloud-provider-payment8001,cloud-provider-payment8002
修改部分 - YML - eureka.instance.instance-id
eureka
eureka
修改之后
eureka主页将显示payment8001,payment8002代替原来显示的IP地址。
访问信息有IP信息提示,(就是将鼠标指针移至payment8001,payment8002名下,会有IP地址提示)
修改部分 - YML - eureka.instance.prefer-ip-address
eureka
eureka
24_服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
-
修改cloud-provider-payment8001的Controller
-
8001主启动类
-
自测
先要启动EurekaSeryer
再启动8001主启动类,需要稍等一会儿
浏览器输入http://localhost:8001/payment/discovery
浏览器输出:
{"services":["cloud-payment-service"],"order":0}
后台输出:
*****element: cloud-payment-service
CLOUD-PAYMENT-SERVICE 192.168.199.218 8001 http://192.168.199.218:8001
25_Eureka自我保护理论知识
概述
保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护。一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。
如果在Eureka Server的首页看到以下这段提示,则说明Eureka进入了保护模式:
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THANTHRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUSTTO BE SAFE
导致原因
一句话:某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存。
属于CAP里面的AP分支。
为什么会产生Eureka自我保护机制?
为了EurekaClient可以正常运行,防止与EurekaServer网络不通情况下,EurekaServer不会立刻将EurekaClient服务剔除
什么是自我保护模式?
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生(延时、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。
自我保护机制∶默认情况下EurekaClient定时向EurekaServer端发送心跳包
如果Eureka在server端在一定时间内(默认90秒)没有收到EurekaClient发送心跳包,便会直接从服务注册列表中剔除该服务,但是在短时间( 90秒中)内丢失了大量的服务实例心跳,这时候Eurekaserver会开启自我保护机制,不会剔除该服务(该现象可能出现在如果网络不通但是EurekaClient为出现宕机,此时如果换做别的注册中心如果一定时间内没有收到心跳会将剔除该服务,这样就出现了严重失误,因为客户端还能正常发送心跳,只是网络延迟问题,而保护机制是为了解决此问题而产生的)。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。
它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话讲解:好死不如赖活着。
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
26_怎么禁止自我保护
-
在eurekaServer端7001处设置关闭自我保护机制
-
出厂默认,自我保护机制是开启的
使用
eureka.server.enable-self-preservation = false
可以禁用自我保护模式
eureka
关闭效果:
spring-eureka主页会显示出一句:
THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
-
生产者客户端eureakeClient端8001
默认:
eureka.instance.lease-renewal-interval-in-seconds=30
eureka.instance.lease-expiration-duration-in-seconds=90
eureka
-
测试
-
7001和8001都配置完成
-
先启动7001再启动8001
-
结果:先关闭8001,马上被删除了
27_Eureka停更说明
https://github.com/Netflix/eureka/wiki
Eureka 2.0 (Discontinued)
The existing open source work on eureka 2.0 is discontinued. The code base and artifacts that were released as part of the existing repository of work on the 2.x branch is considered use at your own risk.
Eureka 1.x is a core part of Netflix’s service discovery system and is still an active project.
我们用ZooKeeper代替Eureka功能。
28_支付服务注册进zookeeper
-
注册中心Zookeeper
zookeeper是一个分布式协调工具,可以实现注册中心功能
关闭Linux服务器防火墙后,启动zookeeper服务器
用到的Linux命令行:
-
systemctl stop firewalld
关闭防火墙 -
systemctl status firewalld
查看防火墙状态 -
ipconfig
查看IP地址 -
ping
查验结果
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
视频里是用虚拟机CentOS开启ZooKeeper,我打算在本机启动ZooKeeper,具体操作参考
-
服务提供者
1.新建名为cloud-provider-payment8004的Maven工程。
2.POM