From 6004b5fddbb2739d93d6dd92fcc026ab421ef95b Mon Sep 17 00:00:00 2001 From: Rikako Wu <496063163@qq.com> Date: Tue, 21 Mar 2023 11:39:51 +0800 Subject: [PATCH] =?UTF-8?q?spring=20cloud=20=E6=96=87=E6=A1=A3=E9=98=85?= =?UTF-8?q?=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/Spring Cloud/Spring Cloud Netflix.md | 41 ++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/spring/Spring Cloud/Spring Cloud Netflix.md b/spring/Spring Cloud/Spring Cloud Netflix.md index a89bf06..d7b0a9e 100644 --- a/spring/Spring Cloud/Spring Cloud Netflix.md +++ b/spring/Spring Cloud/Spring Cloud Netflix.md @@ -37,4 +37,43 @@ eureka: ``` 在上述的配置中,defaultZone是一个默认的回退值,用于向client提供service url默认值。 将`spring-cloud-starter-netflix-eureka-client`依赖包含在classpath中,既会将应用注册为eureka的一个实例,也会将应用变为eureka的一个客户端(可以查询注册中心并且获取其他service的信息)。 -eureka实例的行为将会由`eureka.instance.*`来进行配置,但是当项目制定了`spring.application.name`时,使用默认值来控制eureka实例行为即可 +eureka实例的行为将会由`eureka.instance.*`来进行配置,但是当项目制定了`spring.application.name`时,使用默认值来控制eureka实例行为即可。 +如果想要禁用Eureka Discovery Client,可以将`eureka.client.enabled`属性配置为false,当`spring.cloud.discovery.enabled`属性被设置为false时,Eureka Discovery Client同样会被禁用。 +## status page和health indicator +Eureka实例的status page和health indicator分别指向/info和/health,是spring boot actuator应用的默认端点。 +## Eureka的健康检查 +默认情况下,Eureka使用客户端的心跳来判断该客户端是否启用。除非显式指定,否则客户端并不会传播当前应用的健康检查状态。因此,在成功注册之后,Eureka总是宣布该应用处于`UP`状态。 +通过显式启用健康检查,上述行为可以被改变,客户端将会将应用的状态传播给eureka server。所有的应用都不会向处于非`UP`状态的节点发送负载。 +可以按如下方式启用健康检查: +```yml +eureka: + client: + healthcheck: + enabled: true +``` +## Eureka实例和客户端的元数据 +标准元数据包含hostname、ip address、port、status page、health indicator等信息。这些元数据信息将会被发布到注册中心,并且被eureka client使用,用于调用远程服务。 +### 修改Eureka instance id +Spring Cloud为Eureka instance提供了一个合理的id默认值,其默认值如下: +`${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}` +例如`myhost:myappname:8080` +在Spring Cloud中,可以通过如下方式自定义Eureka实例的id: +```yml +eureka: + instance: + instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}} +``` +## 使用EurekaClient +当应用中包含discovery client时,可以使用其从Eureka Server中发现服务实例: +```java +@Autowired +private EurekaClient discoveryClient; + +public String serviceUrl() { + InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false); + return instance.getHomePageUrl(); +} +``` +## 注册服务速度 +在注册为服务时,需要在一定期间内持续向eureka server发送心跳包,默认情况下该期间持续时间为30s。可以通过定义`eureka.instance.leaseRenewalIntervalInSeconds`值来缩短该期间的时间,将其值设置为小于30.但是,默认情况下,最好将其保留为默认值。 +## Eureka Server \ No newline at end of file