Skip to content

Commit

Permalink
解决对接的app部署在容器中无法获取host
Browse files Browse the repository at this point in the history
  • Loading branch information
JThink committed Dec 7, 2017
1 parent 20404a5 commit b3efd11
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 20 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ branch=$2

pwd=`pwd`

echo "start build skyeye-agent"
cd $pwd/skyeye-agent/image
sudo bash build.sh $version
echo "finished build skyeye-agent"

echo "start build skyeye-alarm"
cd $pwd/skyeye-alarm/image
sudo bash build.sh $version $branch
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include 'skyeye-agent'
include 'skyeye-base'
include 'skyeye-client:skyeye-client-core', 'skyeye-client:skyeye-client-log4j', 'skyeye-client:skyeye-client-logback', 'skyeye-client:skyeye-client-log4j2'
include 'skyeye-trace:skyeye-trace-core', 'skyeye-trace:skyeye-trace-sc'
Expand Down
23 changes: 23 additions & 0 deletions skyeye-agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
bin/
target/
file/
logs/
gen-java/
.externalToolBuilders/
.settings/
.gradle/
.classpath
.gradletasknamecache
.buildpath
.project
.springBeans
dependency-reduced-pom.xml
*.iml
nohup.out
/tmp
/.apt_generated/
.idea/
disconf/
/target/
/build/
/out/
2 changes: 2 additions & 0 deletions skyeye-agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# 项目介绍
rancher容器部署的agent,提供宿主机的host给对接app的容器使用
32 changes: 32 additions & 0 deletions skyeye-agent/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apply plugin: 'application'

dependencies {
compile project(':skyeye-base')
}

configurations {
compile.exclude group: "log4j", module: "log4j"
compile.exclude group: "org.slf4j", module: "slf4j-log4j12"
compile.exclude group: "org.springframework", module: "spring-web"
}

mainClassName = 'com.jthink.skyeye.agent.launcher.Launcher'

buildscript {

repositories {
mavenLocal()
maven { url mavenPublicUrl }
mavenCentral()
}

dependencies {
classpath("io.spring.gradle:dependency-management-plugin:$gradlePluginVersion")
}
}

startScripts {
doLast {
unixScript.text = unixScript.text.replaceAll("lib/(.*)\n", "lib/\\*")
}
}
25 changes: 25 additions & 0 deletions skyeye-agent/image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# build the base image: jdk
# this is the docker file, use the jdk 8u144-ubuntu16.04
# VERSION 1
# Author: leviqian

# the basic image
FROM 192.168.88.73:8888/common/jdk:8u144-ubuntu16.04

# maintainer
MAINTAINER leviqian [email protected]

# get the args
ARG version

# set env
ENV VERSION $version

# copy the file
RUN mkdir -p /home/deploy
ADD skyeye-agent-$version.tar /home/deploy
COPY run.sh /run.sh
RUN chmod +x /run.sh

# CMD to start
CMD ["/run.sh"]
7 changes: 7 additions & 0 deletions skyeye-agent/image/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

version=$1
cp ../target/distributions/*.tar .

sudo docker build --build-arg version=$version -t 192.168.88.73:8888/skyeye/skyeye-agent:$version .
sudo docker push 192.168.88.73:8888/skyeye/skyeye-agent:$version
6 changes: 6 additions & 0 deletions skyeye-agent/image/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

source /etc/profile.d/java.sh

cd /home/deploy/skyeye-agent-$VERSION
nohup bin/skyeye-agent &
1 change: 1 addition & 0 deletions skyeye-agent/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'skyeye-agent'
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jthink.skyeye.agent.launcher;

import com.jthink.skyeye.base.util.DockerHostUtil;

/**
* JThink@JThink
*
* @author JThink
* @version 0.0.1
* @desc 项目启动器
* @date 2016-08-24 18:31:48
*/
public class Launcher {

public static void main(String[] args) {
// 进行宿主机host获取, 并写入
String host = DockerHostUtil.getHostFromLocal();
DockerHostUtil.writeToFile(host);
}
}
2 changes: 1 addition & 1 deletion skyeye-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ artifacts {
//}

dependencies {

compile "org.slf4j:slf4j-api:$slf4jVersion"
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,8 @@ public class Constants {

// docker容器相关
public static final String SKYEYE_HOST_TO_REGISTRY = "SKYEYE_HOST_TO_REGISTRY";
public static final String COMPUTERNAME = "COMPUTERNAME";
public static final String SKYEYE_HOST_FILE = System.getProperty("user.home") + "/.skyeye/host";
public static final String UNKNOWN_HOST = "UnknownHost";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.jthink.skyeye.base.util;

import com.jthink.skyeye.base.constant.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* JThink@JThink
*
* @author JThink
* @version 0.0.1
* @desc docker部署app情况下获取host的工具包
* @date 2017-12-07 19:38:39
*/
public class DockerHostUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(DockerHostUtil.class);

/**
* 从运行机器获取host
* @return
*/
public static String getHostFromLocal() {
try {
return (InetAddress.getLocalHost()).getHostName();
} catch (UnknownHostException uhe) {
String host = uhe.getMessage();
if (host != null) {
int colon = host.indexOf(Constants.COLON);
if (colon > 0) {
return host.substring(0, colon);
}
}

}
return Constants.UNKNOWN_HOST;
}

/**
* 将host写入到
* @param host
* @return
*/
public static void writeToFile(String host) {
BufferedWriter bw = null;
try {
File file = new File(Constants.SKYEYE_HOST_FILE);
if (!file.getParentFile().exists()) {
if (!file.getParentFile().mkdirs()) {
LOGGER.info("创建父文件夹失败");
}
}
bw = new BufferedWriter(new FileWriter(file, false));
bw.write(host);
} catch (IOException e) {
LOGGER.info("写文件报错, ", e);
} finally {
if (bw != null) {
try {
bw.flush();
bw.close();
} catch (IOException e) {
LOGGER.info("写文件报错, ", e);
}
}
}
}

/**
* 从文件中读取host
* @return
*/
public static String readFromFile() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(Constants.SKYEYE_HOST_FILE));
return br.readLine();
} catch (IOException e) {
LOGGER.error("读文件报错, ", e);
} catch (Exception e) {
LOGGER.error("解密出错, ", e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LOGGER.info("读文件报错, ", e);
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.jthink.skyeye.client.core.util;

import com.jthink.skyeye.base.constant.Constants;

import java.net.InetAddress;
import java.net.UnknownHostException;
import com.jthink.skyeye.base.util.DockerHostUtil;
import com.jthink.skyeye.base.util.StringUtil;

/**
* JThink@JThink
Expand All @@ -19,25 +18,19 @@ public class SysUtil {
public static String userDir = Constants.EMPTY_STR;

static {
if (System.getenv("COMPUTERNAME") != null) {
host = System.getenv("COMPUTERNAME");
} else {
// host的读取,为了配合容器部署和rancher部署,首先是需要从环境变量里面取,如果取不到再从.skyeye/host这个文件取,最后再取不到从运行机器中取
host = System.getenv(Constants.COMPUTERNAME);
if (StringUtil.isBlank(host)) {
// 未获取到, 从docker设置的环境变量取
host = System.getenv(Constants.SKYEYE_HOST_TO_REGISTRY);
if (host == null || host.length() == 0) {
try {
host = (InetAddress.getLocalHost()).getHostName();
} catch (UnknownHostException uhe) {
String host = uhe.getMessage();
if (host != null) {
int colon = host.indexOf(':');
if (colon > 0) {
host = host.substring(0, colon);
}
}
host = "UnknownHost";
if (StringUtil.isBlank(host)) {
// 未获取到,从.skyeye/host获取
host = DockerHostUtil.readFromFile();
if (StringUtil.isBlank(host)) {
// 未获取到,从运行机器中取
host = DockerHostUtil.getHostFromLocal();
}
}

}

userDir = System.getProperty("user.dir", "<NA>");
Expand Down

0 comments on commit b3efd11

Please sign in to comment.