Authored by jack

add nginxsync

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