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

cn.meteor.common.launch.spring.ConfigureUtil Maven / Gradle / Ivy

package cn.meteor.common.launch.spring;// Copyright (C), Created on 2021-04-02

import cn.meteor.common.launch.constants.BootConstants;
import cn.meteor.common.launch.constants.EnvConstants;
import org.springframework.core.env.ConfigurableEnvironment;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * SpringBoot 配置工具类
 *
 * @author ths
 * @since 1.0.0
 */
public class ConfigureUtil {

	/**
	 * 获取环境
	 *
	 * @return Environment
	 */
	public static ConfigurableEnvironment getEnvironment() {
		return SpringUtil.getBean(ConfigurableEnvironment.class);
	}

	/**
	 * 获取属性值
	 * @param propertyFullName 环境变量全名
	 *
	 * @return 属性值
	 */
	public static String getPropertyValue(String propertyFullName) {
		return getEnvironment().getProperty(propertyFullName);
	}

	/**
	 * 获取系统名称
	 *
	 * @return appName
	 */
	public static String getAppName() {
		return getEnvironment().getProperty(BootConstants.APP_NAME);
	}

	/**
	 * 获取本机机器IP
	 *
	 * @return ip
	 */
	public static String getClientIp() {
		String ip = "";
		try {
			for (Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); enumeration.hasMoreElements(); ) {
				NetworkInterface networkInterface = enumeration.nextElement();
				String name = networkInterface.getName();
				// 获取容器中服务IP
				if (name.contains("docker") || name.contains("lo")) {
					continue;
				}
				for (Enumeration enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
					InetAddress inAddress = enumIpAddr.nextElement();
					if (inAddress.isLoopbackAddress()) {
						continue;
					}
					String hostAddress = inAddress.getHostAddress();
					if (!hostAddress.contains("::") && !hostAddress.contains("0:0:") && !hostAddress.contains("fe80")) {
						ip = hostAddress;
						break;
					}
				}
			}
		} catch (SocketException e) {
			return EnvConstants.UNKNOWN;
		}
		return ip;
	}

	/**
	 * 获取当前环境
	 *
	 * @return 当前环境
	 */
	public static String[] getCurrentProfiles() {
		ConfigurableEnvironment environment = getEnvironment();
		return environment.getActiveProfiles();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy