All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.AppBootConstants Maven / Gradle / Ivy

/*
 * @(#)AppBootConstants.java	2022-8-9
 *
 * Copyright (c) 2022. All Rights Reserved.
 *
 */

package com.github.javaclub;

import java.util.concurrent.TimeUnit;

import com.github.javaclub.Constants.CacheConfig;
import com.github.javaclub.Constants.MonitorAlarmConfig;
import com.github.javaclub.Constants.MonitorConstants;
import com.github.javaclub.Constants.SepChars;
import com.github.javaclub.toolbox.ToolBox.Objects;
import com.github.javaclub.toolbox.ToolBox.Strings;
import com.github.javaclub.toolbox.ToolBox.Systems;
import com.github.javaclub.toolbox.cache.redis.RedisKeys;
import com.github.javaclub.toolbox.conf.CompositeAppConfigProperties;
import com.github.javaclub.toolbox.thread.ExecutorServiceInstance;

/**
 * AppBootConstants
 *
 * @author Gerald Chen
 * @version $Id: AppBootConstants.java 2022-8-9 22:54:44 Exp $
 */
public class AppBootConstants {
	
	public static String ENV;
	public static String APP_NAME;
	
	public static String SERVER_HOST_NAME;   // 服务器主机名
	public static String SERVER_INTRANET_IP; // 服务器内网IP
	public static String SERVER_INTERNET_IP; // 服务器公网IP
	
	public static String MACHINE_NODE_ID;

	private static volatile boolean dubooWarn = true; // Dubbo调用是否启用告警
	private static volatile boolean controllerWarn = true; // Http接口是否启用告警
	private static volatile boolean sqlWarn = true; // DB执行是否启用告警
	private static volatile boolean httpWarn = true; // Http调用执行是否启用告警
	
	private static volatile boolean perfReportEnabled; // 是否开启性能监控上报
	private static volatile boolean mutexworkEnabled; // 是否开启互斥任务配置
	private static volatile boolean delaytaskEnabled; // 是否开启delaytask配置
	
	private static volatile boolean generalCacheInterceptorEnabled; // 是否启用GeneralCacheInterceptor组件
	private static volatile boolean generalCacheLocalEnabled = true; // 是否启用本地缓存
	private static volatile boolean generalCacheStatslogEnabled; // 是否开启缓存统计日志打印
	
	private static volatile long dubooWarnMills = MonitorAlarmConfig.DEFAULT_DUBBO_WARN_MILLS; // Dubbo调用耗时告警阀值
	private static volatile long mvcHttpWarnMills = MonitorAlarmConfig.DEFAULT_CONTROLLER_WARN_MILLS; // Mvc Http请求耗时告警阀值
	private static volatile long httpWarnMills = MonitorAlarmConfig.DEFAULT_HTTP_WARN_MILLS;
	
	private static volatile String[] skipWarnSqlmapIds;    // 跳过不告警的sqlMapperId
	private static volatile String[] skipWarnDubboMethods; // 跳过不告警的dubbo方法

	static {
		refresh();
		ExecutorServiceInstance.get().scheduleWithFixedDelay(AppBootConstants::refresh, 60, 30, TimeUnit.SECONDS);
		ENV = CompositeAppConfigProperties.getInstance().getEnv();
		APP_NAME = CompositeAppConfigProperties.getInstance().getAppName();
		SERVER_HOST_NAME = Systems.getServerHostName();
		MACHINE_NODE_ID = Strings.concat(Systems.getServerHostName(), SepChars.AT, Systems.getLocalIp());
	}

	private static void refresh() {
		SERVER_INTRANET_IP = Systems.getLocalIp();
		SERVER_INTERNET_IP = Systems.getNetworkIp();
				
		dubooWarn = matchBooleanKeyWithDefaultValue(true, MonitorAlarmConfig.DUBBO_WARN_ENABLED);
		controllerWarn = matchBooleanKeyWithDefaultValue(true, MonitorAlarmConfig.CONTROLLER_WARN_ENABLED);
		sqlWarn = matchBooleanKeyWithDefaultValue(true, MonitorAlarmConfig.SQL_WARN_ENABLED, MonitorAlarmConfig.SQL_WARN_ENABLED_MYBATIS);
		httpWarn = matchBooleanKeyWithDefaultValue(true, MonitorAlarmConfig.HTTP_WARN_ENABLED);
		mutexworkEnabled = matchBooleanKeyWithDefaultValue(true, MonitorConstants.MUTEXWORK_ENABLED, MonitorConstants.MONITOR_MUTEXWORK_ENABLED);
		delaytaskEnabled = matchBooleanKey(MonitorConstants.DELAYTASK_ENABLED);
		perfReportEnabled = matchBooleanKey(MonitorConstants.MONITOR_REPORT_ENABLED);
		generalCacheInterceptorEnabled = matchBooleanKey(CacheConfig.GENERAL_CACHE_ENABLED);
		generalCacheLocalEnabled = matchBooleanKeyWithDefaultValue(true, CacheConfig.CACHE_LOCAL_ENABLED);
		generalCacheStatslogEnabled = matchBooleanKey(CacheConfig.CACHE_STATLOG_ENABLED);
		
		dubooWarnMills = CompositeAppConfigProperties.getInstance().longValue(MonitorAlarmConfig.DUBBO_WARN_MILLS, MonitorAlarmConfig.DEFAULT_DUBBO_WARN_MILLS);
		mvcHttpWarnMills = CompositeAppConfigProperties.getInstance().longValue(MonitorAlarmConfig.CONTROLLER_WARN_MILLS, MonitorAlarmConfig.DEFAULT_CONTROLLER_WARN_MILLS);
		httpWarnMills = CompositeAppConfigProperties.getInstance().longValue(MonitorAlarmConfig.HTTP_WARN_MILLS, MonitorAlarmConfig.DEFAULT_HTTP_WARN_MILLS);
		
		skipWarnSqlmapIds = matchArray(MonitorAlarmConfig.SQL_WARN_SKIP_MAPPER, MonitorAlarmConfig.SQL_WARN_SKIP_MAPPER_MYBATIS);
		skipWarnDubboMethods = matchArray(MonitorAlarmConfig.DUBBO_WARN_SKIP_METHODS);
	}
	
	static String[] matchArray(String ... keys) {
		if (null == keys) {
			return null;
		}
		for (String key : keys) {
			String[] array = CompositeAppConfigProperties.getInstance().arrayValue(key);
			if (null != array && 0 < array.length) {
				return array;
			}
		}
		
		return null;
	}
	
	static boolean matchBooleanKey(String ... keys) {
		String val = CompositeAppConfigProperties.getInstance().getValue(keys);
		Boolean flag = Objects.parseBoolean(val);
		return Boolean.TRUE.equals(flag);
	}
	
	static boolean matchBooleanKeyWithDefaultValue(boolean defaultValue, String ... keys) {
		String val = CompositeAppConfigProperties.getInstance().getValue(keys);
		Boolean flag = Objects.parseBoolean(val);
		return (null == flag) ? defaultValue : flag.booleanValue();
	}

	public static boolean isDubooWarn() {
		return dubooWarn;
	}

	public static boolean isControllerWarn() {
		return controllerWarn;
	}

	public static boolean isSqlWarn() {
		return sqlWarn;
	}
	
	public static boolean isHttpWarn() {
		return httpWarn;
	}

	public static boolean isMutexworkEnabled() {
		return mutexworkEnabled;
	}

	public static boolean isDelaytaskEnabled() {
		return delaytaskEnabled;
	}

	public static boolean isPerfReportEnabled() {
		return perfReportEnabled;
	}

	public static boolean isGeneralCacheInterceptorEnabled() {
		return generalCacheInterceptorEnabled;
	}

	public static boolean isGeneralCacheLocalEnabled() {
		return generalCacheLocalEnabled;
	}

	public static boolean isGeneralCacheStatslogEnabled() {
		return generalCacheStatslogEnabled;
	}

	public static long getDubooWarnMills() {
		return dubooWarnMills;
	}

	public static long getMvcHttpWarnMills() {
		return mvcHttpWarnMills;
	}
	
	public static long getHttpWarnMills() {
		return httpWarnMills;
	}
	
	public static boolean isSkipWarnSqlmap(String mapperId) {
		return Objects.contains(skipWarnSqlmapIds, mapperId);
	}
	
	public static boolean isSkipWarnDubboMethod(String methodKey) {
		return Objects.contains(skipWarnDubboMethods, methodKey);
	}
	
	public static boolean isLoggerEnabled(String loggerKey) {
		return matchBooleanKeyWithDefaultValue(false, loggerKey);
	}
	
	public static String getCurrentAppName() {
		if (Strings.isNotBlank(APP_NAME)) {
			return APP_NAME;
		}
		String appName = CompositeAppConfigProperties.getInstance().getAppName();
		APP_NAME = Strings.noneBlank(appName, "app");
		return APP_NAME;
	}
	
	public static String getActiveServersRedisKey() {
		return RedisKeys.custom("monitor").end("active_machines").build();
	}
	
	public static String getCurrentMachineName() {
		if (Strings.isNotBlank(SERVER_HOST_NAME)) {
			return SERVER_HOST_NAME;
		}
		String hostname = Systems.getServerHostName();
		if (Strings.isNotBlank(hostname)) {
			SERVER_HOST_NAME = hostname;
		}
		return SERVER_HOST_NAME;
	}
	
	public static String getCurrentAppNodeRedisConfKey() {
		String namespace = "node-conf";
		return RedisKeys.custom(namespace).end(getCurrentMachineName()).build();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy