Skip to content

Commit

Permalink
1.1版本完成,提供查询和立即刷新配置的rest url
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Mar 9, 2018
1 parent c773f58 commit 72a8f14
Show file tree
Hide file tree
Showing 14 changed files with 766 additions and 176 deletions.
59 changes: 58 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@

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

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>org.wowtools</groupId>
<artifactId>catframe-common</artifactId>
Expand All @@ -19,6 +31,51 @@
<artifactId>jdom</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
50 changes: 0 additions & 50 deletions src/main/java/org/wowtools/springcloudext/ngineureka/Constant.java

This file was deleted.

130 changes: 6 additions & 124 deletions src/main/java/org/wowtools/springcloudext/ngineureka/Startup.java
Original file line number Diff line number Diff line change
@@ -1,138 +1,20 @@
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;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
* @author liuyu
* @date 2018/2/8
*/
@SpringBootApplication
@EnableEurekaClient
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();
}
}
}
SpringApplication.run(Startup.class, args);
}

/**
* 将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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.wowtools.springcloudext.ngineureka.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wowtools.springcloudext.ngineureka.service.NgineurekaService;

/**
* 操作Controller
*
* @author liuyu
* @date 2018/3/9
*/
@RestController()
@RequestMapping("/cmd")
public class CommandController {
@Autowired
private NgineurekaService ngineurekaService;

@RequestMapping("reload")
public String reload() {
ngineurekaService.doOne(true);
return "success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.wowtools.springcloudext.ngineureka.controller;

import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wowtools.springcloudext.ngineureka.pojo.Record;
import org.wowtools.springcloudext.ngineureka.service.RecordService;

/**
* 运行记录Controller
*
* @author liuyu
* @date 2018/3/9
*/
@RestController()
@RequestMapping("/record")
public class RecordController {

@Autowired
private RecordService recordService;

/**
* 获取最近几次运行记录
*
* @return
*/
@RequestMapping("lately")
public String getLately() {
Record[] lately = recordService.getLately();
JSONArray ja = new JSONArray();
for (Record record : lately) {
ja.put(record.toJson());
}
return ja.toString();
}
}
Loading

0 comments on commit 72a8f14

Please sign in to comment.