需要搭建springcloud项目,eureka是其中的一个模块,依赖主要继承父依赖
学习视频:b站狂神说
便于理解,我修改了本地域名=》这里!!!
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com

<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.4.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
server:
port: 7001
# eureka 配置
eureka:
instance:
hostname: localhost # Eureka 服务端实例名称
client:
register-with-eureka: false # 表示是否向服务器Eureka注册中心注册自己 这里本身激素服务器 false
fetch-registry: false # 如果为false 则表示自己为注册中心
service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
# 单机
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication
@EnableEurekaServer // 开启EurekaServer 服务端启动类 可以接收别人注册
public class EurekaServer_7001 {
// 启动访问http://localhost:7001/
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
http://localhost:7001/
目前还没有任何服务注册到Eureka 所以显示是No instances available
这是一个普通的springcloud项目,需要注册到Eureka中

<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.6.RELEASEversion>
dependency>
eureka:
client:
service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept8001 # 修改eureka 上 Status默认描述信息
@EnableEurekaClient // 自动在服务启动后自动注册到Eureka中
1.需要先启动Eureka服务
2.访问http://localhost:7001/
在Instances currently registered with Eureka中Application对于的是配置文件中spring.application.name: springcloud-provider-dept的值,Status对应的是eureka.instance.instance-id的值。
实际是http://localhost:8001/actuator/info地址,需要配置信息

解决出现错误页面的问题,下面使用actuator来解决。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
# 导入 actuator
info:
app.name: jiangyl-springcloud
company.name: jiang.yl.com
再次启动springcloud-provider-dept-8001
也就是访问http://localhost:8001/actuator/info

可以在提供者模块的Controller控制层自动注入DiscoveryClient类来实现
// 还需要在启动类中添加 @EnableDiscoveryClient 服务发现 用于团队协助
@GetMapping("/dept/discovery")
//注册进来的微服务 , 获取一些信息
public Object discovery(){
// 获取微服务列表的清单
List<String> services = client.getServices();
System.out.println("discovery->services = " + services);
// Eureka 中Application iD
List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
for (ServiceInstance instance : instances) {
System.out.println(
instance.getHost()+"\t"+
instance.getUri()+"\t"+
instance.getPort()+"\t"+
instance.getServiceId());
}
return this.client;
}
重新启动该服务,控制台输出内容:
discovery->services = [springcloud-provider-dept]
localhost http://localhost :8001 8001 SPRINGCLOUD-PROVIDER-DEPT
访问接口。
访问浏览器地址http://localhost:8001/dept/discovery

如果已经注册到Eureka的服务,突然关闭服务了,默认等待90秒后刷新http://localhost:7001/浏览器会发现出现了下面的英文:
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
翻译:紧急!Eureka可能错误地声称实例正在运行,但实际上它们并未运行。由于续订数量低于阈值,因此为了安全起见,这些实例并未过期。

自我保护机制:好死不如赖活着
一句话总结:某时刻某一个微服务不可以用了,eureka不会立刻清理,依旧会对该微服务的信息进行保存!
eureka.server.enable-se1f-preservation = false禁用自我保护模式【不推荐关闭自我保护机制】Eureka可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像zookeeper那样使整个注册服务瘫痪
为了演示的更接近真实集群环境,首先将C:\Windows\System32\drivers\etc\hosts文件添加一下配置:
# 模拟有3台主机,
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
复制文章前面提到的springcloud-eureka-7001项目:

只是需要修改对应的配置文件即可:
springcloud-eureka-7001模块中的application.yml修改之后
hostname: eueka7001.com 本机域名
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/需要关联的主机
server:
port: 7001
# eureka 配置
eureka:
instance:
hostname: eureka7001.com # Eureka 服务端实例名称 电脑1hostname
client:
register-with-eureka: false # 表示是否向服务器Eureka注册中心注册自己 这里本身激素服务器 false
fetch-registry: false # 如果为false 则表示自己为注册中心
service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
# 单机
# defaultZone: http://localhost:7001/eureka/
#集群
# 连接其它主机电脑,域名:端口
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
springcloud-eureka-7002模块中的application.yml
server:
port: 7002
# eureka 配置
eureka:
instance:
hostname: eureka7002.com # Eureka 服务端实例名称 电脑2hostname
client:
register-with-eureka: false # 表示是否向服务器Eureka注册中心注册自己 这里本身激素服务器 false
fetch-registry: false # 如果为false 则表示自己为注册中心
service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
# 连接其它主机电脑,域名:端口
# 单机
# defaultZone: http://localhost:7001/eureka/
#集群
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
springcloud-eureka-7003模块中的application.yml
server:
port: 7003
# eureka 配置
eureka:
instance:
hostname: eureka7003.com # Eureka 服务端实例名称 电脑3hostname
client:
register-with-eureka: false # 表示是否向服务器Eureka注册中心注册自己 这里本身激素服务器 false
fetch-registry: false # 如果为false 则表示自己为注册中心
service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

访问对应的域名:端口号,如:http://eureka7001.com:7001/,结果如下:
DS Replicas:注册中心中用于数据同步的相邻备份节点
只需要修改本文章上述提到的springcloud-provider-dept-8001项目中的application.yml文件defaultZone:的值
server:
port: 8001
eureka:
client:
service-url: # 替换源码 默认this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
# 端口号 7001 7002 7003 ,实际工作中是3台不同的电脑域名或IP
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7002.com:7003/eureka/
instance:
instance-id: springcloud-provider-dept8001 # 修改eureka 上 Status默认描述信息
先启动eureka集群,再启动提供者
访问三台eureka主机中的任意一个,都可以看到提供者信息,如:http://eureka7001.com:7001/

[每日鸡汤1/1]:学习不是一场短跑,而是一场马拉松。保持耐心和毅力,你会看到不一样的风景。