三大角色

springcloud-eureka-7001
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloudartifactId>
<groupId>top.muquanyugroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>springcloud-eureka-7001artifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
<version>1.4.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
dependencies>
project>
Eureka 和 使用 @EnableEurekaServer 注解server:
port: 7001
# Eureka 配置
eureka:
instance:
hostname: localhost # Eureka 服务端的实例名
client:
register-with-eureka: false # 表示是否向 eureka 注册中心注册自己
fetch-registry: false # fetch-registry 如果为 false 则 表示 自己为 注册中心
service-url: # url 地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
<version>1.4.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>

2. 配置 Eureka 和 开启 @EnableEurekaClient 注解支持
#Eureka 的配置,只需要配置 服务 注册到 哪里就行了
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka

神奇的是,只要我们 这样子去做,那么 它就会自动的把 所有的服务 自动注册到 Eureka 中。不需要我们 去管 服务在哪里。


其实 Application 和 Status 这两个描述 都是可以更改的。
spring:
application:
name: springcloud-provider-dept # 这个更改的就是 Application 的名字
#Eureka 的配置,只需要配置 服务 注册到 哪里就行了
eureka:
instance:
instance-id: springcloud-provider-dept8001 # 这个更改的就是 Status 的描述

当我们点击 那个 Status 那里的 描述时,它会跳到 一个 info 页面。而这个 info 页面 里面的信息 是可以配置的。
# info 配置
info:
app.name: MQy-springcloud
company.name: MQy
management:
endpoints:
web:
exposure:
include: "*"
info:
env:
enabled: true

当我们 关掉某个服务,一段时间后(大概是 90s),它就是 提示 一行红字。

好死不如赖活着
一句话总结:某时刻某一个微服务突然不可以用了,Eureka 不会like清理吊,依旧会对该微服务的信息进行保存。
eureka.server.enable-self-preservation = false 禁用自我保护模式【不推荐关闭自我保护机制】注册进来的微服务,其实 是可以 通过 discoveryClient 来获取到 信息的。
@RequestMapping("/dept/discovery")
public Object discovery(){
// 获取微服务列表的清单
List<String> services = client.getServices();
System.out.println("discovery=>services:" + services);
// 得到一个具体的微服务信息,通过具体的微服务 id 或 applicationName
List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
for (ServiceInstance instance : instances) {
System.out.println(instance.getHost()
+ "\t" + instance.getPort() + "\t" +
instance.getUri() + "\t" + instance.getInstanceId());
}
return this.client;
}
使用 @EnableDiscoveryClient

