Skip to content

Commit

Permalink
1 添加丢失的文件
Browse files Browse the repository at this point in the history
2 移除spring-cloud依赖,war包过大
  • Loading branch information
yongfeigao committed Oct 25, 2018
1 parent 951b6b9 commit 4ff71c1
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ target
PROJECT_DIR_IS_UNDEFINED
logs
pom.xml.versionsBackup
nmon
nmon.zip

# ignore eclipse files
.project
Expand Down
13 changes: 1 addition & 12 deletions mq-cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<artifactId>mq-cloud</artifactId>
<packaging>war</packaging>

<!-- spring boot&cloud dependency -->
<!-- spring boot dependency -->
<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -21,13 +21,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -49,10 +42,6 @@
<groupId>com.sohu.tv</groupId>
<artifactId>mq-cloud-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* cpu状况
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sohu.tv.mq.cloud.task.server.data;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* tcp连接
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* io读写情况
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* 系统负载
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sohu.tv.mq.cloud.task.server.data;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* 内存使用情况
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
* 网络流量
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Locale;
import java.util.regex.Pattern;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;
/**
* 服务器基本状态
* @Description:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package com.sohu.tv.mq.cloud.task.server.nmon;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.sohu.tv.mq.cloud.task.server.data.OS;
import com.sohu.tv.mq.cloud.util.MQCloudConfigHelper;

/**
* nmon文件查询器
*
* @Description:
* @author yongfeigao
* @date 2018年7月18日
*/
@Component
public class NMONFileFinder implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(NMONFileFinder.class);
public static final String NMON_PATH = "/nmon";
public static final String NMON_DIR_PATH = "nmon.dir";
public static final String FILE = "file";
// nmon文件存储 key为OSType_ProcessorArchitecture_DistributionType
private static final Map<String, File> nmonFileMap = new HashMap<String, File>();

@Autowired
private MQCloudConfigHelper mqCloudConfigHelper;

@Override
public void run(String... args) throws Exception {
init();
}

/**
* 初始化nmon文件
*/
private void init() {
try {
String path = System.getProperty(NMON_DIR_PATH);
if (path == null) {
String classpath = null;
try {
CodeSource codeSource = NMONFileFinder.class.getProtectionDomain().getCodeSource();
classpath = codeSource.getLocation().getPath();
if (classpath.startsWith(FILE)) {
// like that: file:/opt/xxx.war!/WEB-INF/classes!/
classpath = classpath.substring(FILE.length() + 1);
}
// 如果是jar或者war包,取其上一级路径
if (new File(classpath).isDirectory()) {
path = classpath + "../.." + NMON_PATH;
} else {
// like that: /opt/xxx.war!/WEB-INF/classes!/
String[] tmp = classpath.split("!/", 2);
path = tmp[0].substring(0, tmp[0].lastIndexOf("/")) + NMON_PATH;
}
} catch (Exception e) {
logger.error(classpath, e);
}
}
File nmonDir = new File(path);
if (!nmonDir.exists()) {
// 线上环境尝试通过http加载
boolean ok = mqCloudConfigHelper.isOnline() && downloadAndUnzip(nmonDir.getParentFile());
if(!ok) {
logger.error("{} path not exist", nmonDir.getAbsolutePath());
return;
}
}
// 获取操作系统目录
File[] osDirs = nmonDir.listFiles();
if (osDirs == null) {
logger.error("{} not contains OS folders", nmonDir.getAbsolutePath());
return;
}
for (File osDir : osDirs) {
// 获取处理器架构目录
File[] archFiles = osDir.listFiles();
if (archFiles == null) {
logger.info("{} not contains architecture folders", osDir.getName());
continue;
}
for (File archDir : archFiles) {
// 获取nmon文件目录
File[] nmonFiles = archDir.listFiles();
if (nmonFiles == null) {
logger.info("{} not contains nomon files", archDir.getName());
continue;
}
for (File nmonFile : nmonFiles) {
nmonFileMap.put(osDir.getName() + "_" + archDir.getName()
+ "_" + nmonFile.getName(), nmonFile);
}
logger.info("init {} {} nmon file size=" + nmonFiles.length,
osDir.getName(), archDir.getName());
}
}
logger.info("init {} finished, os size={}", nmonDir.getAbsolutePath(), osDirs.length);
} catch (Exception e) {
logger.error("init nmon factory", e);
}
}

/**
* 从远程下载nmon
* @param nmonUrl
* @param nmonDir
* @return
*/
protected boolean downloadAndUnzip(File nmonDir) {
BufferedInputStream in = null;
OutputStream out = null;
try {
String urlStr = mqCloudConfigHelper.getNMONURL();
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
HttpURLConnection conn = (HttpURLConnection) urlConnection;
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.connect();
String path = nmonDir.getAbsolutePath() + File.separatorChar + MQCloudConfigHelper.NMON_ZIP;
File file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
out = new FileOutputStream(file);
int size = 0;
byte[] buf = new byte[1024];
in = new BufferedInputStream(conn.getInputStream());
while ((size = in.read(buf)) != -1) {
out.write(buf, 0, size);
}

// unzip
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(path));
ZipEntry zipEntry = zis.getNextEntry();
while(zipEntry != null){
String fileName = zipEntry.getName();
File newFile = new File(nmonDir.getAbsolutePath() + File.separatorChar + fileName);
if(zipEntry.isDirectory()) {
newFile.mkdirs();
} else {
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
logger.info("download nmon:{} OK!", urlStr);
} catch (Exception e) {
logger.error("download err", e);
return false;
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {}
}
if(out != null) {
try {
out.close();
} catch (IOException e) {}
}
}
return true;
}

/**
* 根据OS信息获取对应版本的NMON文件
*
* @param os
* @return File
*/
public File getNMONFile(OS os) {
String key = os.getOsType().getValue()
+ "_" + os.getProcessorArchitecture().getValue()
+ "_" + os.getDistributionType().getNmonName()
+ os.getDistributionVersion().getValue();
return nmonFileMap.get(key);
}

}
Loading

0 comments on commit 4ff71c1

Please sign in to comment.