1. 应用中心-关键组件
1.1. 应用自定义关键组件对接
1.1.1. @NextCmptCheck
通过NTC提供的该注解来定义一个纳入NTC管理的关键组件。
注解属性说明:
id:唯一标识一个组件
name:组件名称,默认为空
type:组件类型,有CmptType.ntc、CmptType.act、CmptType.cust三种类型,其中默认为cust类型
desc:组件描述,默认为空
checkMethod:组件健康状态检查调用的无参方法,默认为check方法,方法结果对象为CheckResult(具体字段见文件)。该方法每分钟调用一次
CheckResult类
public class CheckResult implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2730204761783599326L;
/**
* 组件ID
*/
private String componentId;
/**
* 实例ID
*/
private String instanceId;
/**
* 组件名称
*/
private String name;
/**
* 描述
*/
private String message;
/**
* 检查状态
*/
private CheckStatus status = CheckStatus.UNKNOWN;
/**
* 其他信息:日志等
*/
private String details;
/**
* 上下文
*/
private Map<String, Object> context = new HashMap<String, Object>();
/**
* 类型
*/
private CmptType type;
/**
* 错误码
*/
private String code;
public CheckResult() {
super();
}
public CheckResult(CheckStatus status, String message) {
super();
this.message = message;
this.status = status;
}
public CheckResult(CheckStatus status, String message, String details) {
super();
this.message = message;
this.status = status;
this.details = details;
}
public CheckResult(String code, CheckStatus status, String message) {
super();
this.code = code;
this.status = status;
this.message = message;
}
public CheckResult(String code, CheckStatus status, String message, Throwable e) {
super();
this.code = code;
this.status = status;
this.message = message;
this.context.put("exception", formatError(e));
}
public CheckResult(NextCmpt check) {
super();
this.componentId = check.getComponentId();
this.instanceId = check.getInstanceId();
this.name = check.getName();
this.type = check.getType();
}
public CheckResult(NextCmpt check, String code, CheckStatus status, String message) {
super();
this.code = code;
this.componentId = check.getComponentId();
this.instanceId = check.getInstanceId();
this.name = check.getName();
this.type = check.getType();
this.status = status;
this.message = message;
}
public CheckResult(NextCmpt check, String code, CheckStatus status, String message, Throwable e) {
super();
this.code = code;
this.componentId = check.getComponentId();
this.instanceId = check.getInstanceId();
this.name = check.getName();
this.type = check.getType();
this.context.put("exception", formatError(e));
this.status = status;
this.message = message;
}
/**
* 格式化异常信息
*
* @param throwable
* @return
*/
public static String formatError(Throwable throwable) {
StringBuilder sbuf = new StringBuilder();
if (throwable == null) {
return null;
}
sbuf.append(throwable.getMessage()).append("\r\n");
StackTraceElement[] stackTraceElements = throwable.getStackTrace();
for (StackTraceElement stackTraceElement : stackTraceElements) {
if (stackTraceElement.isNativeMethod()) {
sbuf.append(stackTraceElement.getClassName()).append(".")
.append(stackTraceElement.getMethodName()).append("(native method)");
} else {
sbuf.append(stackTraceElement.getClassName()).append(".")
.append(stackTraceElement.getMethodName()).append("(")
.append(stackTraceElement.getLineNumber()).append(")");
}
sbuf.append("\r\n");
}
if (throwable.getCause() != null) {
sbuf.append(formatError(throwable.getCause()));
}
return sbuf.toString();
}
public String getComponentId() {
return componentId;
}
public void setComponentId(String componentId) {
this.componentId = componentId;
}
public String getInstanceId() {
return instanceId;
}
public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public CheckStatus getStatus() {
return status;
}
public void setStatus(CheckStatus status) {
this.status = status;
}
public CmptType getType() {
return type;
}
public void setType(CmptType type) {
this.type = type;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public Map<String, Object> getContext() {
return context;
}
public void setContext(Map<String, Object> context) {
this.context = context;
}
}
updFieldMethod:组件配置更新调用方法,参数为更新的配置的key和value值。默认为相应Field的set方法
1.1.2. @NextCmptCheckField
通过NTC提供的该注解来定义一个纳入NTC管理的关键组件的配置参数
注解属性说明:
key:配置相应的key值
value:配置相应的value值
desc:配置相应的描述
例以下定义了一个id为"myNextCmpt",名称为”自定义组件”,描述为测试使用。
其中配置key为status,默认值为up。默认每分钟调用的方法为check,返回结果CheckResult。
MyNextComponent类
@NextCmptCheck(id = "myNextCmpt", name = "自定义组件", desc = "测试使用")
public class MyNextComponent {
@NextCmptCheckField(key = "status", value = "up", desc = "status desc")
private String status = "up";
/**
* 每分钟调用一次
*
* @return
*/
public CheckResult check() {
CheckResult result = null;
// 模拟场景
if (status.equals("WARN")) {
result = new CheckResult(CheckStatus.WARN, "警告");
Map<String, Object> details = new HashMap<String, Object>();
details.put("msg", "this is warn");
result.setContext(details);
} else if (status.equals("down")) {
result = new CheckResult(CheckStatus.DOWN, "警告");
Map<String, Object> details = new HashMap<String, Object>();
details.put("msg", "this is down");
result.setContext(details);
} else if (status.equals("UNKNOWN")) {
result = new CheckResult(CheckStatus.UNKNOWN, "警告");
Map<String, Object> details = new HashMap<String, Object>();
details.put("msg", "this is warn");
result.setContext(details);
} else {
result = new CheckResult(CheckStatus.UP, "正常");
Map<String, Object> details = new HashMap<String, Object>();
details.put("msg", "this is up");
result.setContext(details);
}
return result;
}
}
1.2. springboot actuator healthIndicators对接
1、引入springboot的spring-boot-starter-actuator包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency></dependencies>