com.alibaba.csp.sentinel.util.AppNameUtil Java Examples

The following examples show how to use com.alibaba.csp.sentinel.util.AppNameUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpHeartbeatSender.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public boolean sendHeartbeat() throws Exception {
    if (StringUtil.isEmpty(consoleHost)) {
        return false;
    }
    URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort)
        .setPath("/registry/machine")
        .setParameter("app", AppNameUtil.getAppName())
        .setParameter("app_type", String.valueOf(SentinelConfig.getAppType()))
        .setParameter("v", Constants.SENTINEL_VERSION)
        .setParameter("version", String.valueOf(System.currentTimeMillis()))
        .setParameter("hostname", HostNameUtil.getHostName())
        .setParameter("ip", TransportConfig.getHeartbeatClientIp())
        .setParameter("port", TransportConfig.getPort())
        .setParameter("pid", String.valueOf(PidUtil.getPid()));

    HttpGet request = new HttpGet(uriBuilder.build());
    request.setConfig(requestConfig);
    // Send heartbeat request.
    CloseableHttpResponse response = client.execute(request);
    response.close();
    return true;
}
 
Example #2
Source File: SentinelManager.java    From jboot with Apache License 2.0 6 votes vote down vote up
public void init() {

        try {

//          throw ClassNotFoundException if not dependency sentinel
            Class.forName("com.alibaba.csp.sentinel.Sph");

            JbootApplicationConfig appConfig = JbootConfigManager.me().get(JbootApplicationConfig.class);
            Field field = AppNameUtil.class.getDeclaredField("appName");
            field.setAccessible(true);
            field.set(null, appConfig.getName());

            processer = new SentinelProcesser();

        } catch (Exception e) {
            // do nothing...
        }
    }
 
Example #3
Source File: HeartbeatMessage.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
public HeartbeatMessage() {
    message.put("hostname", HostNameUtil.getHostName());
    message.put("ip", TransportConfig.getHeartbeatClientIp());
    message.put("app", AppNameUtil.getAppName());
    // Put application type (since 1.6.0).
    message.put("app_type", String.valueOf(SentinelConfig.getAppType()));
    message.put("port", String.valueOf(TransportConfig.getPort()));
}
 
Example #4
Source File: FetchClusterServerInfoCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    JSONObject info = new JSONObject();
    JSONArray connectionGroups = new JSONArray();
    Set<String> namespaceSet = ClusterServerConfigManager.getNamespaceSet();
    for (String namespace : namespaceSet) {
        ConnectionGroup group = ConnectionManager.getOrCreateConnectionGroup(namespace);
        if (group != null) {
            connectionGroups.add(group);
        }
    }

    ServerTransportConfig transportConfig = new ServerTransportConfig()
        .setPort(ClusterServerConfigManager.getPort())
        .setIdleSeconds(ClusterServerConfigManager.getIdleSeconds());
    ServerFlowConfig flowConfig = new ServerFlowConfig()
        .setExceedCount(ClusterServerConfigManager.getExceedCount())
        .setMaxOccupyRatio(ClusterServerConfigManager.getMaxOccupyRatio())
        .setIntervalMs(ClusterServerConfigManager.getIntervalMs())
        .setSampleCount(ClusterServerConfigManager.getSampleCount())
        .setMaxAllowedQps(ClusterServerConfigManager.getMaxAllowedQps());

    JSONArray requestLimitData = buildRequestLimitData(namespaceSet);

    info.fluentPut("port", ClusterServerConfigManager.getPort())
        .fluentPut("connection", connectionGroups)
        .fluentPut("requestLimitData", requestLimitData)
        .fluentPut("transport", transportConfig)
        .fluentPut("flow", flowConfig)
        .fluentPut("namespaceSet", namespaceSet)
        .fluentPut("embedded", ClusterServerConfigManager.isEmbedded());

    // Since 1.5.0 the appName is carried so that the caller can identify the appName of the token server.
    info.put("appName", AppNameUtil.getAppName());

    return CommandResponse.ofSuccess(info.toJSONString());
}
 
Example #5
Source File: SentinelConfigLoader.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void load() {
    String fileName = System.getProperty(SENTINEL_CONFIG);
    if (StringUtil.isBlank(fileName)) {
        fileName = DEFAULT_SENTINEL_CONFIG_FILE;
    }

    Properties p = ConfigUtil.loadProperties(fileName);

    // Compatible with legacy config file path.
    if (p == null) {
        String path = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator;
        fileName = path + AppNameUtil.getAppName() + ".properties";
        File file = new File(fileName);
        if (file.exists()) {
            p = ConfigUtil.loadProperties(fileName);
        }
    }

    if (p != null && !p.isEmpty()) {
        RecordLog.info("[SentinelConfigLoader] Loading Sentinel config from " + fileName);
        properties.putAll(p);
    }

    for (Map.Entry<Object, Object> entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) {
        String configKey = entry.getKey().toString();
        String newConfigValue = entry.getValue().toString();
        String oldConfigValue = properties.getProperty(configKey);
        properties.put(configKey, newConfigValue);
        if (oldConfigValue != null) {
            RecordLog.info("[SentinelConfigLoader] JVM parameter overrides {0}: {1} -> {2}",
                    configKey, oldConfigValue, newConfigValue);
        }
    }
}
 
Example #6
Source File: HeartbeatMessage.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public HeartbeatMessage() {
    message.put("hostname", HostNameUtil.getHostName());
    message.put("ip", TransportConfig.getHeartbeatClientIp());
    message.put("app", AppNameUtil.getAppName());
    // Put application type (since 1.6.0).
    message.put("app_type", String.valueOf(SentinelConfig.getAppType()));
    message.put("port", String.valueOf(TransportConfig.getPort()));
}
 
Example #7
Source File: HttpHeartbeatSender.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public boolean sendHeartbeat() throws Exception {
    if (StringUtil.isEmpty(consoleHost)) {
        return false;
    }
    URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort)
        .setPath(TransportConfig.getHeartbeatApiPath())
        .setParameter("app", AppNameUtil.getAppName())
        .setParameter("app_type", String.valueOf(SentinelConfig.getAppType()))
        .setParameter("v", Constants.SENTINEL_VERSION)
        .setParameter("version", String.valueOf(System.currentTimeMillis()))
        .setParameter("hostname", HostNameUtil.getHostName())
        .setParameter("ip", TransportConfig.getHeartbeatClientIp())
        .setParameter("port", TransportConfig.getPort())
        .setParameter("pid", String.valueOf(PidUtil.getPid()));

    HttpGet request = new HttpGet(uriBuilder.build());
    request.setConfig(requestConfig);
    // Send heartbeat request.
    CloseableHttpResponse response = client.execute(request);
    response.close();
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == OK_STATUS) {
        return true;
    } else if (clientErrorCode(statusCode) || serverErrorCode(statusCode)) {
        RecordLog.warn("[HttpHeartbeatSender] Failed to send heartbeat to "
            + consoleHost + ":" + consolePort + ", http status code: " + statusCode);
    }

    return false;
}
 
Example #8
Source File: FetchClusterServerInfoCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    JSONObject info = new JSONObject();
    JSONArray connectionGroups = new JSONArray();
    Set<String> namespaceSet = ClusterServerConfigManager.getNamespaceSet();
    for (String namespace : namespaceSet) {
        ConnectionGroup group = ConnectionManager.getOrCreateConnectionGroup(namespace);
        if (group != null) {
            connectionGroups.add(group);
        }
    }

    ServerTransportConfig transportConfig = new ServerTransportConfig()
        .setPort(ClusterServerConfigManager.getPort())
        .setIdleSeconds(ClusterServerConfigManager.getIdleSeconds());
    ServerFlowConfig flowConfig = new ServerFlowConfig()
        .setExceedCount(ClusterServerConfigManager.getExceedCount())
        .setMaxOccupyRatio(ClusterServerConfigManager.getMaxOccupyRatio())
        .setIntervalMs(ClusterServerConfigManager.getIntervalMs())
        .setSampleCount(ClusterServerConfigManager.getSampleCount())
        .setMaxAllowedQps(ClusterServerConfigManager.getMaxAllowedQps());

    JSONArray requestLimitData = buildRequestLimitData(namespaceSet);

    info.fluentPut("port", ClusterServerConfigManager.getPort())
        .fluentPut("connection", connectionGroups)
        .fluentPut("requestLimitData", requestLimitData)
        .fluentPut("transport", transportConfig)
        .fluentPut("flow", flowConfig)
        .fluentPut("namespaceSet", namespaceSet)
        .fluentPut("embedded", ClusterServerConfigManager.isEmbedded());

    // Since 1.5.0 the appName is carried so that the caller can identify the appName of the token server.
    info.put("appName", AppNameUtil.getAppName());

    return CommandResponse.ofSuccess(info.toJSONString());
}
 
Example #9
Source File: SentinelEndpoint.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@ReadOperation
public Map<String, Object> invoke() {
	final Map<String, Object> result = new HashMap<>();
	if (sentinelProperties.isEnabled()) {

		result.put("appName", AppNameUtil.getAppName());
		result.put("logDir", LogBase.getLogBaseDir());
		result.put("logUsePid", LogBase.isLogNameUsePid());
		result.put("blockPage", SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY));
		result.put("metricsFileSize", SentinelConfig.singleMetricFileSize());
		result.put("metricsFileCharset", SentinelConfig.charset());
		result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount());
		result.put("consoleServer", TransportConfig.getConsoleServerList());
		result.put("clientIp", TransportConfig.getHeartbeatClientIp());
		result.put("heartbeatIntervalMs", TransportConfig.getHeartbeatIntervalMs());
		result.put("clientPort", TransportConfig.getPort());
		result.put("coldFactor", sentinelProperties.getFlow().getColdFactor());
		result.put("filter", sentinelProperties.getFilter());
		result.put("datasource", sentinelProperties.getDatasource());

		final Map<String, Object> rules = new HashMap<>();
		result.put("rules", rules);
		rules.put("flowRules", FlowRuleManager.getRules());
		rules.put("degradeRules", DegradeRuleManager.getRules());
		rules.put("systemRules", SystemRuleManager.getRules());
		rules.put("authorityRule", AuthorityRuleManager.getRules());
		rules.put("paramFlowRule", ParamFlowRuleManager.getRules());
	}
	return result;
}
 
Example #10
Source File: ConfigSupplierRegistry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Override
public String get() {
    return AppNameUtil.getAppName();
}
 
Example #11
Source File: SentinelConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static String getAppName() {
    return AppNameUtil.getAppName();
}
 
Example #12
Source File: ConfigPropertyHelper.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static void setAppNameProperty(String appName) {
    System.setProperty(AppNameUtil.APP_NAME, appName);
}
 
Example #13
Source File: ConfigPropertyHelper.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static void clearAppNameProperty() {
    System.clearProperty(AppNameUtil.APP_NAME);
}
 
Example #14
Source File: ConfigSupplierRegistry.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Override
public String get() {
    return AppNameUtil.getAppName();
}
 
Example #15
Source File: SentinelAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@PostConstruct
private void init() {
	if (StringUtils.isEmpty(System.getProperty(LogBase.LOG_DIR))
			&& StringUtils.hasText(properties.getLog().getDir())) {
		System.setProperty(LogBase.LOG_DIR, properties.getLog().getDir());
	}
	if (StringUtils.isEmpty(System.getProperty(LogBase.LOG_NAME_USE_PID))
			&& properties.getLog().isSwitchPid()) {
		System.setProperty(LogBase.LOG_NAME_USE_PID,
				String.valueOf(properties.getLog().isSwitchPid()));
	}
	if (StringUtils.isEmpty(System.getProperty(AppNameUtil.APP_NAME))
			&& StringUtils.hasText(projectName)) {
		System.setProperty(AppNameUtil.APP_NAME, projectName);
	}
	if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT))
			&& StringUtils.hasText(properties.getTransport().getPort())) {
		System.setProperty(TransportConfig.SERVER_PORT,
				properties.getTransport().getPort());
	}
	if (StringUtils.isEmpty(System.getProperty(TransportConfig.CONSOLE_SERVER))
			&& StringUtils.hasText(properties.getTransport().getDashboard())) {
		System.setProperty(TransportConfig.CONSOLE_SERVER,
				properties.getTransport().getDashboard());
	}
	if (StringUtils.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_INTERVAL_MS))
			&& StringUtils
					.hasText(properties.getTransport().getHeartbeatIntervalMs())) {
		System.setProperty(TransportConfig.HEARTBEAT_INTERVAL_MS,
				properties.getTransport().getHeartbeatIntervalMs());
	}
	if (StringUtils.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_CLIENT_IP))
			&& StringUtils.hasText(properties.getTransport().getClientIp())) {
		System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP,
				properties.getTransport().getClientIp());
	}
	if (StringUtils.isEmpty(System.getProperty(SentinelConfig.CHARSET))
			&& StringUtils.hasText(properties.getMetric().getCharset())) {
		System.setProperty(SentinelConfig.CHARSET,
				properties.getMetric().getCharset());
	}
	if (StringUtils
			.isEmpty(System.getProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE))
			&& StringUtils.hasText(properties.getMetric().getFileSingleSize())) {
		System.setProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE,
				properties.getMetric().getFileSingleSize());
	}
	if (StringUtils
			.isEmpty(System.getProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT))
			&& StringUtils.hasText(properties.getMetric().getFileTotalCount())) {
		System.setProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT,
				properties.getMetric().getFileTotalCount());
	}
	if (StringUtils.isEmpty(System.getProperty(SentinelConfig.COLD_FACTOR))
			&& StringUtils.hasText(properties.getFlow().getColdFactor())) {
		System.setProperty(SentinelConfig.COLD_FACTOR,
				properties.getFlow().getColdFactor());
	}
	if (StringUtils.hasText(properties.getBlockPage())) {
		setConfig(BLOCK_PAGE_URL_CONF_KEY, properties.getBlockPage());
	}

	// earlier initialize
	if (properties.isEager()) {
		InitExecutor.doInit();
	}

}