SpringCloud_健康自检

Posted zhougongjin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud_健康自检相关的知识,希望对你有一定的参考价值。

目的:监测Eureka客户端和服务器是否正常

需要依赖的jar: pom.xml

红色部分: springboot的actuator主要用于系统监控

<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.crazyit.cloud</groupId>
	<artifactId>health-handler-provider</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-actuator</artifactId>
			<version>1.5.3.RELEASE</version>
		</dependency>
	</dependencies>
	
</project>

  

application.yml文件

配置 healthcheck之后,就会调用实现HealthCheckHandler的类,比如MyHealthCheckHandler

spring:
  application:
    name: health-handler-provider
eureka:
  instance:
    hostname: localhost
  client:
    healthcheck: 
      enabled: true
    instanceInfoReplicationIntervalSeconds: 10
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

MyHealthCheckHandler模拟数据库是否可用的监测

return InstanceStatus.UP就会被Eureka服务器端监测到该client是down的状态
package org.crazyit.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo.InstanceStatus;

/**
 * 健康检查处理器
 * @author 杨恩雄
 *
 */
@Component
public class MyHealthCheckHandler implements HealthCheckHandler 

	@Autowired
	private MyHealthIndicator indicator;

	public InstanceStatus getStatus(InstanceStatus currentStatus) 
		Status s = indicator.health().getStatus();
		if(s.equals(Status.UP)) 
			System.out.println("数据库正常连接");
			return InstanceStatus.UP;
		 else 
			System.out.println("数据库无法连接");
			return InstanceStatus.DOWN;
		
	

  MyHealthIndicator监测数据数据库状态,是否可用

package org.crazyit.cloud;

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

/**
 * 鍋ュ悍鎸囩ず鍣?
 * @author 鏉ㄦ仼闆?
 *
 */
@Component
public class MyHealthIndicator implements HealthIndicator 

	public Health health() 
		if(HealthController.canVisitDb) 		
			// 鎴愬姛杩炴帴鏁版嵁搴擄紝杩斿洖UP
			return new Health.Builder(Status.UP).build();
		 else 
			// 杩炴帴鏁版嵁搴撳け璐ワ紝杩斿洖 out of service
			return new Health.Builder(Status.DOWN).build();
		
	

  HealthController修改数据库状态,输入http://localhost:8080/db/true即可开启数据库,而http://localhost:8080/db/false是关闭数据库

package org.crazyit.cloud;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthController 

	// 标识当前数据库是否可以访问
	static Boolean canVisitDb = false;
	
	@RequestMapping(value = "/db/canVisitDb", method = RequestMethod.GET)
	@ResponseBody
	public String setConnectState(@PathVariable("canVisitDb") Boolean canVisitDb) 
		this.canVisitDb = canVisitDb;
		return "当前数据库是否正常: " + this.canVisitDb;
	

  

 

处理流程

1. 执行main方法,启动server

2. 自动读取application.yml文件,看到有配置healthcheck的状态为true,找实现HealthCheckHandler接口的类和重写的方法getStatus

3. MyHealthCheckHandler实现了HealthCheckHandler这个接口,通过MyHealthIndicator去查看数据库状态

4. MyHealthIndicator获取到数据库状态

5. HealthController专门为测试改变数据库状态而存在的

6.application.yml文件配置了instanceInfoReplicationIntervalSeconds: 10,所以每隔10s,就会访问实现HealthCheckHandler接口的类和重写的方法getStatus。

 

以上是关于SpringCloud_健康自检的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud概述

SpringCloud概述

SpringCloud概述

企业级 SpringCloud 教程 断路器(Hystrix)

企业级 SpringCloud 教程 断路器(Hystrix)

SpringCloud之eureka注册中心入门