Authored by jack

add nginxsync

<?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">
<parent>
<artifactId>monitor-service-parent</artifactId>
<groupId>monitor-service</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>monitor-service-nginxsync</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-common</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.34.Final</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
... ...
package com.monitor.nginxsync.constant;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
/**
* Created by yoho on 2016/8/23.
*/
public interface Constants {
ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String SCRIPT_PATH = "/home/master/nginx-sync/";
String H5_SCRIPT = "h5_nginx.sh";
String N_SCRIPT = "nginx.sh";
String DNS_SCRIPT = "dns_nginx.sh";
}
... ...
package com.monitor.nginxsync.model;
import lombok.Data;
/**
* Created by yoho on 2016/8/23.
*/
@Data
public class CmdTaskInfo {
String user;
String cmd;
String dir;
String taskId;
}
... ...
package com.monitor.nginxsync.model;
import lombok.Data;
/**
* Created by yoho on 2016/8/23.
*/
@Data
public class TaskInfo {
//命令执行用户
String user;
//命令类型
int type;
//shell 命令行
String cmd;
String taskId;
}
... ...
package com.monitor.nginxsync.nio.handler;
import com.monitor.nginxsync.constant.Constants;
import com.monitor.nginxsync.nio.model.LogMsg;
import com.monitor.nginxsync.nio.service.LogService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
/**
* Created by yoho on 2016/8/23.
*/
public class LogMsgHandler extends SimpleChannelInboundHandler {
public static final LogService LOG_SERVICE = new LogService();
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object msg) throws Exception {
ByteBuf result = (ByteBuf) msg;
byte[] resultWarp = new byte[result.readableBytes()];
// msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中
result.readBytes(resultWarp);
LogMsg logMsg = Constants.OBJECT_MAPPER.readValue(resultWarp, LogMsg.class);
// 插入日志管理
LOG_SERVICE.inLogMsg(logMsg);
// 释放资源,这行很关键
result.release();
}
}
... ...
package com.monitor.nginxsync.nio.model;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by yoho on 2016/8/23.
*/
@Data
public class LogMsg {
String taskId = StringUtils.EMPTY;
int isFinished ;
List<String> msgList = new ArrayList<>();
}
... ...
package com.monitor.nginxsync.nio.service;
import com.monitor.nginxsync.nio.model.LogMsg;
import com.sun.javafx.collections.MappingChange;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Created by yoho on 2016/8/23.
*/
@Service
public class LogService {
public static final Map<String, List<String>> LOGMAPPER = new ConcurrentHashMap<String, List<String>>();
public static final Map<String, Integer> FINISHMAPPER = new ConcurrentHashMap<>();
public void inLogMsg(LogMsg logMsg) {
String taskId = logMsg.getTaskId();
synchronized (taskId.intern()) {
if (!LOGMAPPER.containsKey(taskId)) {
List<String> logList = new ArrayList<>();
LOGMAPPER.put(taskId, logList);
}
List<String> logList = LOGMAPPER.get(taskId);
for (String logItem : logMsg.getMsgList()) {
logList.add(logItem);
}
FINISHMAPPER.put(taskId, logMsg.getIsFinished());
}
}
public LogMsg outLogMsg(String taskId) {
LogMsg logMsg = new LogMsg();
List<String> logList = new ArrayList<>();
synchronized (taskId.intern()) {
List<String> savedList = LOGMAPPER.get(taskId);
if (null != savedList && !savedList.isEmpty()) {
CollectionUtils.addAll(logList, new String[savedList.size()]);
Collections.copy(logList, savedList);
savedList.clear();
}
logMsg.setTaskId(taskId);
logMsg.setMsgList(logList);
if (!FINISHMAPPER.containsKey(taskId)) {
logMsg.setIsFinished(0);
} else {
logMsg.setIsFinished(FINISHMAPPER.get(taskId));
}
}
return logMsg;
}
}
... ...
package com.monitor.nginxsync.nio.service;
import com.monitor.nginxsync.nio.handler.LogMsgHandler;
import com.squareup.okhttp.Call;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by yoho on 2016/8/23.
* 提供NIO 服务端功能
*/
@Service
public class NIOService {
public static final ExecutorService SERVICE = Executors.newSingleThreadExecutor();
@PostConstruct
public void start() {
SERVICE.submit(new NIOServer());
}
public static class NIOServer implements Callable {
@Override
public Object call() throws Exception {
init();
return null;
}
public void init() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
// 注册handler
ch.pipeline().addLast(new LogMsgHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(8080).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
}
... ...
package com.monitor.nginxsync.service;
import com.google.common.base.Preconditions;
import com.monitor.model.response.BaseResponse;
import com.monitor.nginxsync.constant.Constants;
import com.monitor.nginxsync.model.CmdTaskInfo;
import com.monitor.nginxsync.model.TaskInfo;
import com.monitor.nginxsync.nio.model.LogMsg;
import com.monitor.nginxsync.nio.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.text.MessageFormat;
import java.util.logging.Logger;
/**
* Created by yoho on 2016/8/23.
*/
@RestController
@RequestMapping(value = "/nginxsync")
public class NginxSyncService {
@Value("${nginxsync.agent}")
String agent;
@Autowired
LogService logService;
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/pushtask")
public BaseResponse pushTask(@RequestBody TaskInfo taskInfo) {
Preconditions.checkNotNull(taskInfo);
CmdTaskInfo cmdTaskInfo = new CmdTaskInfo();
//设置运行目录
cmdTaskInfo.setDir(Constants.SCRIPT_PATH);
//根据前台选项选择执行脚本
switch (taskInfo.getType()) {
case 1:
cmdTaskInfo.setCmd(Constants.SCRIPT_PATH + Constants.N_SCRIPT);
break;
case 2:
cmdTaskInfo.setCmd(Constants.SCRIPT_PATH + Constants.H5_SCRIPT);
break;
case 3:
cmdTaskInfo.setCmd(Constants.SCRIPT_PATH + Constants.DNS_SCRIPT);
break;
default:
break;
}
//如果任务类型为空
if (0 == taskInfo.getType()) {
return new BaseResponse();
}
//设置唯一的任务ID
cmdTaskInfo.setTaskId(String.valueOf(System.currentTimeMillis()));
BaseResponse response = new BaseResponse();
taskInfo.setTaskId(cmdTaskInfo.getTaskId());
response.setData(taskInfo);
response.setCode(200);
try {
restTemplate.postForObject("http://" + agent + "/handle", cmdTaskInfo, String.class);
} catch (RestClientException e) {
response.setCode(500);
response.setMessage("agent error: " + e.getMessage());
}
//返回结果
return response;
}
@RequestMapping(value = "/getasklog")
public BaseResponse getTaskLog(@RequestBody TaskInfo taskInfo) {
BaseResponse baseResponse = new BaseResponse();
baseResponse.setData(logService.outLogMsg(taskInfo.getTaskId()));
return baseResponse;
}
}
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="restClientPool" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager"
p:maxTotal="10"
p:defaultMaxPerRoute="5">
</bean>
<bean id="restClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create"
p:connectionManager-ref="restClientPool">
</bean>
<bean id="restClient" factory-bean="restClientBuilder" factory-method="build">
</bean>
<bean id="clientFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"
p:connectTimeout="10000" p:readTimeout="10000">
<constructor-arg ref="restClient"></constructor-arg>
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="clientFactory"></constructor-arg>
</bean>
</beans>
... ...
... ... @@ -51,6 +51,10 @@
<artifactId>monitor-service-other</artifactId>
</dependency>
<dependency>
<groupId>monitor-service</groupId>
<artifactId>monitor-service-nginxsync</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
... ...
... ... @@ -3,4 +3,6 @@ system.envi=product
#java api 执行核心线程数
JavaApiExecutorPoolCoreSize=30
#java api 执行最大线程数
JavaApiExecutorPoolMaxSize=50
\ No newline at end of file
JavaApiExecutorPoolMaxSize=50
nginxsync.agent=172.31.16.167:6060
\ No newline at end of file
... ...
... ... @@ -3,4 +3,6 @@ system.envi=test
#java api 执行核心线程数
JavaApiExecutorPoolCoreSize=30
#java api 执行最大线程数
JavaApiExecutorPoolMaxSize=50
\ No newline at end of file
JavaApiExecutorPoolMaxSize=50
nginxsync.agent=192.168.102.15:6060
\ No newline at end of file
... ...
... ... @@ -150,6 +150,7 @@
<module>monitor-service-middleware</module>
<module>monitor-service-user</module>
<module>monitor-service-other</module>
<module>monitor-service-nginxsync</module>
</modules>
... ...