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。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM