com.nepxion.discovery.plugin.framework.util.MetadataUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of discovery-plugin-framework-starter Show documentation
Show all versions of discovery-plugin-framework-starter Show documentation
Nepxion Discovery is a solution for Spring Cloud with blue green, gray, weight, limitation, circuit breaker, degrade, isolation, monitor, tracing, dye, failover, async agent
The newest version!
package com.nepxion.discovery.plugin.framework.util;
/**
* Title: Nepxion Discovery
* Description: Nepxion Discovery
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.core.env.Environment;
import com.nepxion.discovery.common.constant.DiscoveryConstant;
import com.nepxion.discovery.common.util.PropertiesUtil;
public class MetadataUtil {
// 过滤设置元数据到Metadata Map
public static void filter(Map metadata, Environment environment) {
// 运维参数元数据的设置方式
// 支持从-Dmetadata.xyz参数获取
Properties properties = System.getProperties();
Set propertyNames = properties.stringPropertyNames();
for (String propertyName : propertyNames) {
if (propertyName.startsWith(DiscoveryConstant.METADATA + ".")) {
String key = propertyName.substring((DiscoveryConstant.METADATA + ".").length());
String value = properties.get(propertyName).toString();
// -Dmetadata.xyz优先级最高
// 不管元数据是否存在,直接放入或者覆盖
metadata.put(key, value);
}
}
// 统一注册中心元数据的设置方式
// 支持spring.cloud.discovery.metadata.xyz配置获取
Properties enrichProperties = new Properties();
PropertiesUtil.enrichProperties(enrichProperties, environment, DiscoveryConstant.SPRING_CLOUD_DISCOVERY_PREFIX, false, true);
Set enrichPropertyNames = enrichProperties.stringPropertyNames();
for (String enrichPropertyName : enrichPropertyNames) {
if (enrichPropertyName.startsWith(DiscoveryConstant.METADATA + ".")) {
String key = enrichPropertyName.substring((DiscoveryConstant.METADATA + ".").length());
String value = enrichProperties.get(enrichPropertyName).toString();
// spring.cloud.discovery.metadata.xyz优先级最低
// 当元数据不存在,才放入
if (!metadata.containsKey(key)) {
metadata.put(key, value);
}
}
}
}
// 过滤设置元数据到Metadata List
// 该方式适用于旧版Consul元数据模式
public static void filter(List metadata, Environment environment) {
// 运维参数元数据的设置方式
// 支持从-Dmetadata.xyz参数获取
Properties properties = System.getProperties();
Set propertyNames = properties.stringPropertyNames();
for (String propertyName : propertyNames) {
if (propertyName.startsWith(DiscoveryConstant.METADATA + ".")) {
String key = propertyName.substring((DiscoveryConstant.METADATA + ".").length());
String value = properties.get(propertyName).toString();
// -Dmetadata.xyz优先级最高
// 不管元数据是否存在,直接放入或者覆盖
int index = getIndex(metadata, key);
if (index > -1) {
metadata.set(index, key + "=" + value);
} else {
metadata.add(key + "=" + value);
}
}
}
// 统一注册中心元数据的设置方式
// 支持spring.cloud.discovery.metadata.xyz配置获取
Properties enrichProperties = new Properties();
PropertiesUtil.enrichProperties(enrichProperties, environment, DiscoveryConstant.SPRING_CLOUD_DISCOVERY_PREFIX, false, true);
Set enrichPropertyNames = enrichProperties.stringPropertyNames();
for (String enrichPropertyName : enrichPropertyNames) {
if (enrichPropertyName.startsWith(DiscoveryConstant.METADATA + ".")) {
String key = enrichPropertyName.substring((DiscoveryConstant.METADATA + ".").length());
String value = enrichProperties.get(enrichPropertyName).toString();
// spring.cloud.discovery.metadata.xyz优先级最低
// 当元数据不存在,才放入
int index = getIndex(metadata, key);
if (index <= -1) {
metadata.add(key + "=" + value);
}
}
}
}
public static int getIndex(List metadata, String key) {
for (int i = 0; i < metadata.size(); i++) {
String value = metadata.get(i);
if (value.startsWith(key + "=")) {
return i;
}
}
return -1;
}
public static boolean containsKey(List metadata, String key) {
for (String value : metadata) {
if (value.startsWith(key + "=")) {
return true;
}
}
return false;
}
}