改变eureka server中注册的服务的健康检测方式

默认情况下注册到eureka server的服务是通过心跳来告知自己是UP还是DOWN,并不是通过spring-boot-actuator模块的/health端点来实现的,这样其实不是很合理。

默认的心跳实现方式可以有效的检查eureka客户端进程是否正常运作,但是无法保证客户端应用能够正常提供服务。由于大多数微服务应用都会有一些其他的外部资源依赖,比如数据库,REDIS缓存等,如果我们的应用与这些外部资源无法连通的时候,实际上已经不能提供正常的对外服务了,但因为客户端心跳依然在运行,所以它还是会被服务消费者调用,而这样的调用实际上并不能获得预期的后果。

我们可以通过在eureka客户端中配置:eureka.client.healthcheck.enabled=true,就可以改变eureka server对客户端健康检测的方式,改用actuator的/health端点来检测。


我们在前面的ribbon consumer样例工程中添加一个自定义的HealthIndicator:

package com.example.eurekaclientconsumerribbon.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthChecker implements HealthIndicator {


    private boolean up = true;

    @Override
    public Health health() {
        if (up) {
            return new Health.Builder().withDetail("aaa_cnt", 10) //自定义监控内容
                    .withDetail("bbb_status", "up").up().build();
        } else {
            return new Health.Builder().withDetail("error", "client is down").down().build();
        }

    }

    public boolean isUp() {
        return up;
    }

    public void setUp(boolean up) {
        this.up = up;
    }
}


里面有一个成员变量up,用来控制是否监控,下面我们会在请求中改变这个变量的值来模拟健康状态UP->DOWN

package com.example.eurekaclientconsumerribbon.controller;

import com.example.eurekaclientconsumerribbon.health.MyHealthChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UpController {

    @Autowired
    MyHealthChecker myHealthChecker;

    @RequestMapping("/up")
    public String up(@RequestParam("up") Boolean up) {
        myHealthChecker.setUp(up);

        return up.toString();
    }



}

application.yml:

spring:
  application:
    name: eureka-client-consumer-ribbon
management:
  security:
    enabled: false
---
spring:
  profiles: peer1
server:
  port: 8200
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/
    healthcheck:
          enabled: true #使用health端点来代替心跳表明服务是否可用,反应到eureka server ui上服务的UP还是DOWN
  instance:
    hostname: peer1
#    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

---
spring:
  profiles: peer2
server:
  port: 8201
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/
  instance:
    hostname: peer2
#    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}



主要看配置里面设置了eureka.client.healthcheck.enabled=true,这个配置属性在IDEA里面不会自动提示,我一度怀疑写错了,试过确实有效的改变了检测方式。

启动类:

package com.example.eurekaclientconsumerribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableHystrixDashboard
public class EurekaClientConsumerRibbonApplication {


    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }




    public static void main(String[] args) {
        SpringApplication.run(EurekaClientConsumerRibbonApplication.class, args);
    }
}

pom:



	4.0.0

	com.example
	eureka-client-consumer-ribbon
	0.0.1-SNAPSHOT
	jar

	eureka-client-consumer-ribbon
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.6.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Dalston.SR3
	

	
		
			org.springframework.cloud
			spring-cloud-starter-eureka
		
		
			org.jolokia
			jolokia-core
		
		
			org.springframework.cloud
			spring-cloud-starter-ribbon
		
		
			org.springframework.cloud
			spring-cloud-starter-hystrix
		
		
			org.springframework.cloud
			spring-cloud-starter-hystrix-dashboard
		
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					
						
							build-info
						
					
				
			
		
	





启动该服务将会注册到eureka server中去,此时显示的应该是UP




然后我们调用一下服务:http://localhost:8200/up?up=false,将此时查看http://localhost:8200/health,整个应用的health状态变成DOWN了:


注册中心的服务状态也将变为DOWN:



我们可以试一下把application.yml中eureka.client.healthcheck.enabled=true这段配置去掉重新启动服务,然后调用服务将health变为DOWN,但是注册中心中仍然会显示该服务的status为UP!





你可能感兴趣的:(springboot,spring,cloud)