Skip to content

Commit

Permalink
第一版完成
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Feb 11, 2018
0 parents commit a7447b1
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.idea/
.settings/
.project
.classpath
*.iml
*.class
target/
nb-configuration.xml
nb_jr_remoting.cfg
rebel.xml
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.wowtools.springcloudext</groupId>
<artifactId>ngineureka</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.wowtools</groupId>
<artifactId>catframe-common</artifactId>
<version>1.4STABLE</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>

</project>
50 changes: 50 additions & 0 deletions src/main/java/org/wowtools/springcloudext/ngineureka/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.wowtools.springcloudext.ngineureka;

import org.wowtools.common.utils.ResourcesReader;

import java.util.Properties;

/**
* 常量
*
* @author liuyu
* @date 2018/2/8
*/
public class Constant {
/**
* 注册中心url,如 http://10.111.58.121:10000/eureka
*/
public static final String eurekaUrl;
/**
* nginx用到的服务配置文件ngineureka_xx.conf的路径
*/
public static final String confPath;


/**
* 心跳周期,定时获取注册中心的情况的周期
*/
public static final long heartbeatCycle;

public static final String rootPath;

static {
try {
rootPath = ResourcesReader.getRootPath(Constant.class);
Properties p = new Properties();
p.load(ResourcesReader.readStream(rootPath + "/config.properties"));
eurekaUrl = p.getProperty("eurekaUrl");
confPath = p.getProperty("confPath");

long t;
try {
t = (long) (Double.valueOf(p.getProperty("heartbeatCycle")) * 1000);
} catch (Exception e) {
t = 300000;
}
heartbeatCycle = t;
} catch (Exception e) {
throw new RuntimeException("读取配置文件异常", e);
}
}
}
138 changes: 138 additions & 0 deletions src/main/java/org/wowtools/springcloudext/ngineureka/Startup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.wowtools.springcloudext.ngineureka;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.wowtools.common.utils.SimpleHttpUtil;
import org.xml.sax.InputSource;

import java.io.*;
import java.util.List;

/**
* @author liuyu
* @date 2018/2/8
*/
public class Startup {

private static String lastXml;

public static void main(String[] args) throws Exception {
System.out.println("启动完毕,初始化配置文件");
update();
System.out.println("配置文件初始化完毕,开始定期监控注册中心,可通过检查/ngineureka_upstream.conf来观察服务注册情况");
while (true) {
try {
Thread.sleep(Constant.heartbeatCycle);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
update();
} catch (Exception e) {
e.printStackTrace();
}
}
}

private static void update() throws Exception {
String xml = SimpleHttpUtil.sendGet(Constant.eurekaUrl + "/apps");
if (xml.equals(lastXml)) {//数据无变化,终止
return;
}
lastXml = xml;
xml2Config(xml);

reloadCfg();
}


/**
* 重新加载nginx配置
*/
private static void reloadCfg() {
String fileName = "/".equals(File.separator) ? "/reload.sh" : "/reload.bat";
exeCmd(Constant.rootPath + fileName);
}

/**
* 执行控制台命令
*
* @param commandStr
*/
private static String exeCmd(String commandStr) {
BufferedReader br = null;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(commandStr);
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}

return sb.toString();
} catch (Exception e) {
throw new RuntimeException("执行控制台命令异常:" + commandStr, e);
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

/**
* 将http://10.111.58.121:10000/eureka/apps返回的xml转为nginx的配置
*
* @param xml
*/
private static void xml2Config(String xml) throws Exception {
StringReader read = new StringReader(xml);
InputSource source = new InputSource(read);
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build(source);
Element root = doc.getRootElement();
List<Element> apps = root.getChildren("application");
StringBuilder sbUpstream = new StringBuilder();
StringBuilder sbServer = new StringBuilder();
for (Element app : apps) {
String appName = app.getChild("name").getText().toLowerCase();
List<Element> instances = app.getChildren("instance");

sbUpstream.append("upstream upstream_").append(appName).append("{\n");
sbUpstream.append("\tleast_conn;\n");

sbServer.append("location ^~ /").append(appName).append("/ {\n");
sbServer.append("\tproxy_pass http://upstream_").append(appName).append(";\n");
sbServer.append("}\n\n");

for (Element instance : instances) {
String ip = instance.getChild("ipAddr").getText();
String port = instance.getChild("port").getText();

sbUpstream.append("\tserver ").append(ip).append(":").append(port).append(";\n");
}
sbUpstream.append("}\n\n");

}
writeConfig(sbUpstream.toString(), "/ngineureka_upstream.conf");
writeConfig(sbServer.toString(), "/ngineureka_location.conf");
}

private static void writeConfig(String cfg, String name) throws Exception {
String path = Constant.confPath + name;
File f = new File(path);
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
out.write(cfg.getBytes());
} finally {
out.close();
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eurekaUrl=http://10.111.58.121:10000/eureka
confPath=D:/nginx-1.8.1/conf/apps
heartbeatCycle=300
3 changes: 3 additions & 0 deletions src/main/resources/reload.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
D:
cd D:/nginx-1.8.1/
nginx -s reload
2 changes: 2 additions & 0 deletions src/main/resources/reload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd D:/nginx-1.8.1/
./nginx -s reload

0 comments on commit a7447b1

Please sign in to comment.