项目初始化

parents
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/build/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
\ No newline at end of file
<?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>com.cc</groupId>
<artifactId>service-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-rest</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!-- 远程调用 -->
<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210-hudson-1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.cc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceRestApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRestApplication.class, args);
}
}
package com.cc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* Created by changc on 2018/9/7.
*
允许任何域名使用
允许任何头
允许任何方法(post、get等)
*/
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1
corsConfiguration.addAllowedHeader("*"); // 2
corsConfiguration.addAllowedMethod("*"); // 3
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig()); // 4
return new CorsFilter(source);
}
}
\ No newline at end of file
package com.cc.controller;
import com.cc.service.ServiceService;
import com.cc.serviceutil.XzxtRestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author liuys
* @desc
* @date 2018-07-12 17:01
*/
@RequestMapping(value = "/")
@Controller
public class ServiceRestController {
@Autowired
ServiceService serviceService;
/*
初始化
*/
@GetMapping("/")
@ResponseBody
public String hello(Model model){
model.addAttribute("msg","你好");
return "login";
}
/*
初始化
*/
@PostMapping("/start")
@ResponseBody
public XzxtRestResult start(String ip, String ipUsername, String ipPassword,String shellPaht ,String shellname,String servletPath){
return serviceService.RemoteShellExecutor( ip, ipUsername,ipPassword,shellPaht,shellname,servletPath);
}
}
package com.cc.service;
import com.cc.serviceutil.XzxtRestResult;
/**
* @author yutons
*/
public interface ServiceService {
XzxtRestResult RemoteShellExecutor(String ip, String ipUsername, String ipPassword,String shellPaht ,String shellname,String servletPath);
}
package com.cc.service.serviceimpl;
import com.cc.service.ServiceService;
import com.cc.serviceutil.RemoteShellExecutor;
import com.cc.serviceutil.XzxtRestResult;
import org.springframework.stereotype.Service;
@Service
public class ServiceIServiceImpl implements ServiceService {
@Override
public XzxtRestResult RemoteShellExecutor(String ip, String ipUsername, String ipPassword,String shellPaht ,String shellname,String servletPath) {
RemoteShellExecutor executor = new RemoteShellExecutor(ip, ipUsername, ipPassword);
// 执行myTest.sh 参数为java Know dummy
try {
///usr/local/startTomcat.sh
String shell=shellPaht+shellname;
System.out.println("调用shell文件地址:====="+shell+" \""+servletPath+"\"");
return XzxtRestResult.build(200,"启动成功",executor.exec(shell+" \""+servletPath+"\""));
} catch (Exception e) {
e.printStackTrace();
return XzxtRestResult.ok(201,"启动失败",null);
}
}
}
package com.cc.serviceutil;
import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class RemoteShellExecutor {
private Connection conn;
/** 远程机器IP */
private String ip;
/** 用户名 */
private String osUsername;
/** 密码 */
private String password;
private String charset = Charset.defaultCharset().toString();
private static final int TIME_OUT = 1000 * 5 * 60;
/**
* 构造函数
* @param ip
* @param usr
* @param pasword
*/
public RemoteShellExecutor(String ip, String usr, String pasword) {
this.ip = ip;
this.osUsername = usr;
this.password = pasword;
}
/**
* 登录
* @return
* @throws IOException
*/
private boolean login() throws IOException {
conn = new Connection(ip);
conn.connect();
return conn.authenticateWithPassword(osUsername, password);
}
/**
* 执行脚本
*
* @param cmds
* @return
* @throws Exception
*/
public int exec(String cmds) throws Exception {
InputStream stdOut = null;
InputStream stdErr = null;
String outStr = "";
String outErr = "";
int ret = -1;
try {
if (login()) {
// Open a new {@link Session} on this connection
Session session = conn.openSession();
// Execute a command on the remote machine.
session.execCommand(cmds);
stdOut = new StreamGobbler(session.getStdout());
outStr = processStream(stdOut, charset);
stdErr = new StreamGobbler(session.getStderr());
outErr = processStream(stdErr, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
System.out.println("outStr=" + outStr);
System.out.println("outErr=" + outErr);
ret = session.getExitStatus();
} else {
throw new Exception("登录远程机器失败" + ip); // 自定义异常类 实现略
}
} finally {
if (conn != null) {
conn.close();
}
IOUtils.closeQuietly(stdOut);
IOUtils.closeQuietly(stdErr);
}
return ret;
}
/**
* @param in
* @param charset
* @return
* @throws IOException
* @throws UnsupportedEncodingException
*/
private String processStream(InputStream in, String charset) throws Exception {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
return sb.toString();
}
public static void main(String args[]) throws Exception {
RemoteShellExecutor executor = new RemoteShellExecutor("192.168.193.133", "root", "18234077206");
// 执行myTest.sh 参数为java Know dummy
// ./startTomcat.sh "/usr/local/tomcat-portal"
System.out.println(executor.exec("/usr/local/startTomcat.sh \"/usr/local/tomcat-portal\""));
}
}
\ No newline at end of file
package com.cc.serviceutil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
/**
* Xzxt-Rest自定义响应结构
*/
public class XzxtRestResult {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
// 响应业务状态
private Integer status;
// 响应消息
private String msg;
// 响应中的数据
private Object data;
public static XzxtRestResult build(Integer status, String msg, Object data) {
return new XzxtRestResult(status, msg, data);
}
public static XzxtRestResult ok(Object data) {
return new XzxtRestResult(data);
}
public static XzxtRestResult ok(int i, String count, List<Object> list) {
return new XzxtRestResult(null);
}
public XzxtRestResult() {
}
public static XzxtRestResult build(Integer status, String msg) {
return new XzxtRestResult(status, msg, null);
}
public XzxtRestResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public XzxtRestResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
/**
* 将json结果集转化为Xzxt-Rest对象
*
* @param jsonData json数据
* @param clazz Xzxt-Rest中的object类型
* @return
*/
public static XzxtRestResult formatToPojo(String jsonData, Class<?> clazz) {
try {
if (clazz == null) {
return MAPPER.readValue(jsonData, XzxtRestResult.class);
}
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (clazz != null) {
if (data.isObject()) {
obj = MAPPER.readValue(data.traverse(), clazz);
} else if (data.isTextual()) {
obj = MAPPER.readValue(data.asText(), clazz);
}
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}
/**
* object对象的转化
*
* @param json
* @return
*/
public static XzxtRestResult format(String json) {
try {
return MAPPER.readValue(json, XzxtRestResult.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Object是集合转化
*
* @param jsonData json数据
* @param clazz 集合中的类型
* @return
*/
public static XzxtRestResult formatToList(String jsonData, Class<?> clazz) {
try {
JsonNode jsonNode = MAPPER.readTree(jsonData);
JsonNode data = jsonNode.get("data");
Object obj = null;
if (data.isArray() && data.size() > 0) {
obj = MAPPER.readValue(data.traverse(),
MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
}
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
} catch (Exception e) {
return null;
}
}
}
# \u9879\u76EE\u8BBF\u95EE\u8DEF\u5F84
server.port=8081
server.context-path=/service-rest
server.tomcat.uri-encoding=UTF-8
logging.level.com.cc=debug
#spring.profiles.active=dev
#logging.path=
# \u4E0D\u6307\u5B9A\u8DEF\u5F84\u5728\u5F53\u524D\u9879\u76EE\u4E0B\u751F\u6210springboot.log\u65E5\u5FD7
# \u53EF\u4EE5\u6307\u5B9A\u5B8C\u6574\u7684\u8DEF\u5F84\uFF1B
#logging.file=G:/springboot.log
# \u5728\u5F53\u524D\u78C1\u76D8\u7684\u6839\u8DEF\u5F84\u4E0B\u521B\u5EFAspring\u6587\u4EF6\u5939\u548C\u91CC\u9762\u7684log\u6587\u4EF6\u5939\uFF1B\u4F7F\u7528?spring.log \u4F5C\u4E3A\u9ED8\u8BA4\u6587\u4EF6
logging.path=/spring/log
# \u5728\u63A7\u5236\u53F0\u8F93\u51FA\u7684\u65E5\u5FD7\u7684\u683C\u5F0F
logging.pattern.console=%d{yyyy-MM-dd} ===application===[%thread] %-5level %logger{50} - %msg%n
# \u6307\u5B9A\u6587\u4EF6\u4E2D\u65E5\u5FD7\u8F93\u51FA\u7684\u683C\u5F0F
logging.pattern.file=%d{yyyy-MM-dd} ===>===application=== [%thread] === %-5level === %logger{50} ==== %msg%n
\ No newline at end of file
package com.cc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceRestApplicationTests {
@Test
public void contextLoads() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment