Skip to content

Commit 1c3655c

Browse files
committed
支持Linux服务器部署在线使用
1 parent 748b941 commit 1c3655c

File tree

14 files changed

+262
-75
lines changed

14 files changed

+262
-75
lines changed

pom.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.example</groupId>
77
<artifactId>ViewGenerator</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
8+
<version>1.0</version>
99
<packaging>jar</packaging>
1010

1111
<name>ViewGenerator</name>
@@ -48,12 +48,6 @@
4848
<scope>test</scope>
4949
</dependency>
5050

51-
<dependency>
52-
<groupId>org.springframework.boot</groupId>
53-
<artifactId>spring-boot-devtools</artifactId>
54-
<optional>true</optional>
55-
</dependency>
56-
5751
<!-- Web -->
5852
<dependency>
5953
<groupId>org.springframework.boot</groupId>
@@ -163,6 +157,13 @@
163157
<artifactId>commons-io</artifactId>
164158
<version>${commons-io.version}</version>
165159
</dependency>
160+
161+
<!-- hutool -->
162+
<dependency>
163+
<groupId>cn.hutool</groupId>
164+
<artifactId>hutool-all</artifactId>
165+
<version>5.7.2</version>
166+
</dependency>
166167
</dependencies>
167168

168169
<build>
Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package com.example;
22

3+
import cn.hutool.setting.dialect.Props;
4+
import com.example.constant.Constant;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.ConfigurableApplicationContext;
58
import org.springframework.context.annotation.PropertySource;
69

10+
import java.io.File;
11+
import java.util.concurrent.ArrayBlockingQueue;
12+
import java.util.concurrent.ExecutorService;
13+
import java.util.concurrent.ThreadPoolExecutor;
14+
import java.util.concurrent.TimeUnit;
15+
716
/**
817
* Application
918
*
@@ -12,9 +21,53 @@
1221
*/
1322
@SpringBootApplication
1423
@tk.mybatis.spring.annotation.MapperScan({"com.example.mapper", "com.example.dao"})
15-
@PropertySource(value = {"classpath:config/generator.properties"})
24+
/*@PropertySource(value = {"classpath:config/generator.properties"})*/
1625
public class Application {
26+
27+
public static String[] args;
28+
public static ConfigurableApplicationContext context;
29+
1730
public static void main(String[] args) {
18-
SpringApplication.run(Application.class, args);
31+
SpringApplication application = new SpringApplication(Application.class);
32+
application.setDefaultProperties(config());
33+
Application.args = args;
34+
Application.context = application.run(args);
35+
}
36+
37+
/**
38+
* 配置选择
39+
*
40+
* @param
41+
* @return cn.hutool.setting.dialect.Props
42+
* @throws
43+
* @author wliduo[[email protected]]
44+
* @date 2021/11/17 13:50
45+
*/
46+
public static Props config() {
47+
File file = new File(Constant.CONFIG_PATH_TEMP);
48+
if (file.exists()) {
49+
return new Props(Constant.CONFIG_PATH_TEMP);
50+
} else {
51+
return new Props("classpath:config/generator.properties");
52+
}
53+
}
54+
55+
/**
56+
* 重启项目方法,注意,必须将maven的spring-boot-devtools开发热部署包去除
57+
* 不然重启后ConfigurableApplicationContext会为空
58+
*
59+
* https://blog.csdn.net/weixin_44695125/article/details/108059877
60+
*/
61+
public static void restart() {
62+
ExecutorService threadPool = new ThreadPoolExecutor(1, 1, 0,
63+
TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.DiscardOldestPolicy());
64+
threadPool.execute(() -> {
65+
// SpringApplication.exit(context);
66+
context.close();
67+
SpringApplication application = new SpringApplication(Application.class);
68+
application.setDefaultProperties(config());
69+
Application.context = application.run(args);
70+
});
71+
threadPool.shutdown();
1972
}
2073
}

src/main/java/com/example/config/ExceptionAdvice.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class ExceptionAdvice {
2727
@ResponseStatus(HttpStatus.BAD_REQUEST)
2828
@ExceptionHandler(CustomException.class)
2929
public ResponseBean handle(CustomException e) {
30+
e.printStackTrace();
3031
return new ResponseBean(HttpStatus.BAD_REQUEST.value(), e.getMessage(), null);
3132
}
3233

@@ -38,6 +39,7 @@ public ResponseBean handle(CustomException e) {
3839
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
3940
@ExceptionHandler(SystemException.class)
4041
public ResponseBean handle(SystemException e) {
42+
e.printStackTrace();
4143
return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), null);
4244
}
4345

@@ -49,6 +51,7 @@ public ResponseBean handle(SystemException e) {
4951
@ResponseStatus(HttpStatus.NOT_FOUND)
5052
@ExceptionHandler(NoHandlerFoundException.class)
5153
public ResponseBean handle(NoHandlerFoundException e) {
54+
e.printStackTrace();
5255
return new ResponseBean(HttpStatus.NOT_FOUND.value(), e.getMessage(), null);
5356
}
5457

@@ -62,6 +65,7 @@ public ResponseBean handle(NoHandlerFoundException e) {
6265
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
6366
@ExceptionHandler(Exception.class)
6467
public ResponseBean globalException(HttpServletRequest request, Throwable ex) {
68+
ex.printStackTrace();
6569
return new ResponseBean(this.getStatus(request).value(), ex.toString() + ": " + ex.getMessage(), null);
6670
}
6771

src/main/java/com/example/constant/Constant.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.example.constant;
22

3+
import java.io.File;
4+
35
/**
46
* Constant
57
*
@@ -33,4 +35,19 @@ public interface Constant {
3335
*/
3436
String WINDOWS = "Windows";
3537

38+
/**
39+
* TMEP_DIR
40+
*/
41+
String TMEP_DIR = PROJECT_PATH + File.separator + "temp" + File.separator;
42+
43+
/**
44+
* CONFIG_PATH
45+
*/
46+
String CONFIG_PATH = "config" + File.separator + "generator.properties";
47+
48+
/**
49+
* CONFIG_PATH_TEMP
50+
*/
51+
String CONFIG_PATH_TEMP = TMEP_DIR + File.separator + CONFIG_PATH;
52+
3653
}

src/main/java/com/example/controller/DataBaseController.java

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
package com.example.controller;
77

8+
import cn.hutool.core.io.FileUtil;
89
import cn.org.rapid_framework.generator.Generator;
910
import cn.org.rapid_framework.generator.GeneratorFacade;
1011
import cn.org.rapid_framework.generator.GeneratorProperties;
12+
import com.alibaba.fastjson.JSON;
13+
import com.example.Application;
1114
import com.example.constant.Constant;
1215
import com.example.dao.GeneratorDao;
1316
import com.example.exception.SystemException;
@@ -17,6 +20,8 @@
1720
import org.apache.commons.io.FileUtils;
1821
import org.apache.commons.io.IOUtils;
1922
import org.apache.commons.lang3.StringUtils;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
2025
import org.springframework.beans.factory.annotation.Value;
2126
import org.springframework.web.bind.annotation.*;
2227
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,8 +35,6 @@
3035
import javax.servlet.http.HttpServletRequest;
3136
import javax.servlet.http.HttpServletResponse;
3237
import java.io.*;
33-
import java.net.URL;
34-
import java.net.URLConnection;
3538
import java.util.*;
3639

3740
/**
@@ -44,6 +47,11 @@
4447
@RequestMapping("database")
4548
public class DataBaseController {
4649

50+
/**
51+
* logger
52+
*/
53+
private static final Logger logger = LoggerFactory.getLogger(DataBaseController.class);
54+
4755
/**
4856
* 代码生成的文件输出路径
4957
*/
@@ -182,9 +190,11 @@ public void genTableToZip(@PathVariable("tableName") String tableName,
182190
// 配置表名
183191
String[] tableNames = tableName.split("-");
184192
// 获得临时路径
185-
String loadPath = request.getSession().getServletContext().getRealPath("/");
193+
String loadPath = request.getSession().getServletContext().getRealPath(File.separator);
194+
logger.info("临时路径: {}", loadPath);
186195
// 创建临时路径
187-
File tempDir = new File(loadPath + "/code");
196+
File tempDir = new File(loadPath + File.separator + "code");
197+
logger.info("完整临时路径: {}", loadPath + File.separator + "code");
188198
if (!tempDir.exists()) {
189199
tempDir.mkdirs();
190200
}
@@ -231,6 +241,7 @@ public ResponseBean open() {
231241
Runtime.getRuntime().exec("cmd.exe /c start " + System.getProperty("user.dir") + outRoot);
232242
}
233243
} catch (IOException e) {
244+
e.printStackTrace();
234245
throw new SystemException("操作失败");
235246
}
236247
return new ResponseBean(HttpStatus.OK.value(), "操作成功", null);
@@ -245,51 +256,49 @@ public ResponseBean open() {
245256
@PutMapping("/config")
246257
public ResponseBean config(@RequestBody Map<String, String> config) {
247258
try {
248-
final Enumeration urls = DataBaseController.class.getClassLoader().getResources("config/generator.properties");
249-
while (urls.hasMoreElements()) {
250-
final URL url = (URL) urls.nextElement();
251-
InputStream input = null;
252-
OutputStream output = null;
253-
try {
254-
final URLConnection con = url.openConnection();
255-
con.setUseCaches(false);
256-
input = con.getInputStream();
257-
SafeProperties safeProperties = new SafeProperties();
258-
if (Boolean.parseBoolean(config.get("isRead"))) {
259-
// 读
260-
safeProperties.load(input);
261-
config = (Map) safeProperties;
262-
} else {
263-
// 写
264-
safeProperties.load(input);
265-
// 遍历map写入
266-
Iterator<Map.Entry<String, String>> entries = config.entrySet().iterator();
267-
while (entries.hasNext()) {
268-
Map.Entry<String, String> entry = entries.next();
269-
if (Constant.TEMPLATE.equals(entry.getKey())) {
270-
// 查看配置路径下macro.includ文件是否存在
271-
File file = new File(Constant.PROJECT_PATH + entry.getValue() + "/macro.include");
272-
if (!file.exists()) {
273-
// 模板代码位置不存在
274-
return new ResponseBean(HttpStatus.BAD_REQUEST.value(), "当前填写的模板代码位置不存在", null);
275-
}
259+
InputStream input = null;
260+
OutputStream output = null;
261+
try {
262+
input = FileUtil.getInputStream(Constant.CONFIG_PATH_TEMP);
263+
SafeProperties safeProperties = new SafeProperties();
264+
if (Boolean.parseBoolean(config.get("isRead"))) {
265+
// 读
266+
safeProperties.load(input);
267+
config = (Map) safeProperties;
268+
logger.info("读取配置: {}", JSON.toJSONString(config));
269+
} else {
270+
// 写
271+
safeProperties.load(input);
272+
// 遍历map写入
273+
Iterator<Map.Entry<String, String>> entries = config.entrySet().iterator();
274+
while (entries.hasNext()) {
275+
Map.Entry<String, String> entry = entries.next();
276+
if (Constant.TEMPLATE.equals(entry.getKey())) {
277+
// 查看配置路径下macro.includ文件是否存在
278+
File file = new File(Constant.PROJECT_PATH + entry.getValue() + "/macro.include");
279+
if (!file.exists()) {
280+
// 模板代码位置不存在
281+
return new ResponseBean(HttpStatus.BAD_REQUEST.value(), "当前填写的模板代码位置不存在", null);
276282
}
277-
safeProperties.setProperty(entry.getKey(), entry.getValue());
278283
}
279-
output = new FileOutputStream(url.getPath());
280-
// 更新,服务器热重启
281-
safeProperties.store(output, null);
282-
}
283-
} finally {
284-
if (input != null) {
285-
input.close();
286-
}
287-
if (output != null) {
288-
output.close();
284+
safeProperties.setProperty(entry.getKey(), entry.getValue());
289285
}
286+
logger.info("更新配置: {}, url: {}", JSON.toJSONString(safeProperties), Constant.CONFIG_PATH_TEMP);
287+
output = new FileOutputStream(Constant.CONFIG_PATH_TEMP);
288+
// 更新,服务器热重启
289+
safeProperties.store(output, null);
290+
Application.restart();
291+
}
292+
} finally {
293+
if (input != null) {
294+
input.close();
295+
}
296+
if (output != null) {
297+
output.close();
290298
}
291299
}
292300
} catch (IOException e) {
301+
e.printStackTrace();
293302
throw new SystemException("操作失败");
294303
}
295304
return new ResponseBean(HttpStatus.OK.value(), "操作成功", config);
@@ -308,10 +317,11 @@ public ResponseBean config(@RequestBody Map<String, String> config) {
308317
public boolean genCode(String[] tableNames, String outRoot) throws IOException {
309318
GeneratorFacade generatorFacade = new CustomGeneratorFacade(outRoot);
310319
// 配置信息
311-
GeneratorProperties.load(new String[]{"classpath:config/generator.properties"});
320+
GeneratorProperties.load(Constant.CONFIG_PATH_TEMP);
312321
// 模板位置
313322
Generator generator = generatorFacade.getGenerator();
314-
generator.addTemplateRootDir(Constant.PROJECT_PATH + template);
323+
logger.info(Constant.PROJECT_PATH + File.separator + template);
324+
generator.addTemplateRootDir(Constant.PROJECT_PATH + File.separator + template);
315325
// 开始执行
316326
try {
317327
for (String tableName : tableNames) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.example.controller;
2+
3+
import com.example.Application;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* 重新启动项目
11+
*
12+
* @author wliduo[[email protected]]
13+
* @date 2021/11/17 11:14
14+
*/
15+
@RestController
16+
public class RestartController {
17+
18+
/**
19+
* logger
20+
*/
21+
private static Logger logger = LoggerFactory.getLogger(RestartController.class);
22+
23+
/**
24+
* 重新启动项目
25+
*
26+
* @param
27+
* @return java.lang.String
28+
* @throws
29+
* @author wliduo[[email protected]]
30+
* @date 2021/11/17 11:14
31+
*/
32+
@GetMapping("/restart")
33+
public String restart() {
34+
Application.restart();
35+
return "success";
36+
}
37+
38+
}

0 commit comments

Comments
 (0)