Skip to content

Commit 3cafbb8

Browse files
committed
👌 更新嵌入式Tomcat启动类和脚本
1 parent cbc3b82 commit 3cafbb8

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

codes/javatool/server/src/main/java/io/github/dunwu/javatool/server/TomcatServer.java

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,86 @@
11
package io.github.dunwu.javatool.server;
22

33
import java.io.File;
4-
import java.util.Optional;
54

65
import org.apache.catalina.Server;
76
import org.apache.catalina.startup.Catalina;
87
import org.apache.catalina.startup.Tomcat;
98
import org.apache.tomcat.util.digester.Digester;
9+
import org.apache.tomcat.util.scan.Constants;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
12+
import org.springframework.util.StringUtils;
1213

1314
/**
14-
* 嵌入式 Tomcat 启动类
15-
* 启动后可访问 http://localhost:8080/javatool-server/
15+
* 嵌入式 Tomcat 启动类 启动后可访问 http://localhost:8080/javatool-server/
1616
* @author Zhang Peng
1717
*/
1818
public class TomcatServer {
1919

20-
private static final String PORT = "8080";
21-
// private static final String RELATIVE_DUBBO_RESOVE_FILE =
22-
// "src/main/resources/properties/dubbo-resolve.properties";
23-
private static final String RELATIVE_BASE_DIR = "src/main/resources/tomcat/";
20+
private static final Logger log = LoggerFactory.getLogger(TomcatServer.class);
21+
22+
private static final String CONNECTOR_PORT = "8080";
23+
private static final String RELATIVE_DEV_DUBBO_RESOVE_FILE = "src/main/resources/properties/dubbo-resolve.properties";
24+
private static final String RELATIVE_DUBBO_RESOVE_FILE = "WEB-INF/classes/properties/dubbo-resolve.properties";
25+
26+
// 以下设置轻易不要改动
27+
private static final String RELATIVE_DEV_BASE_DIR = "src/main/resources/tomcat/";
28+
private static final String RELATIVE_BASE_DIR = "WEB-INF/classes/tomcat/";
29+
private static final String RELATIVE_DEV_DOCBASE_DIR = "src/main/webapp";
30+
private static final String RELATIVE_DOCBASE_DIR = "./";
2431

2532
/**
2633
* 除了 spring.profiles.active,System.setProperty 设置的属性都是为了配置 server.xml
2734
*/
2835
public static void main(String[] args) throws Exception {
29-
// 设定 Spring 的 profile
30-
Optional<String> profile = Optional.ofNullable(System.getProperty("spring.profiles.active"));
31-
System.setProperty("spring.profiles.active", profile.orElse("develop"));
36+
// 设定Spring的profile
37+
if (StringUtils.isEmpty(System.getProperty("spring.profiles.active"))) {
38+
System.setProperty("spring.profiles.active", "develop");
39+
}
3240

33-
// 设定 Tomcat 的 catalina.base
34-
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR);
41+
System.setProperty("tomcat.host.appBase", getAbsolutePath());
42+
File checkFile = new File(System.getProperty("tomcat.host.appBase") + "/WEB-INF");
43+
if (!checkFile.exists()) {
44+
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_DEV_BASE_DIR);
45+
System.setProperty("tomcat.context.docBase", RELATIVE_DEV_DOCBASE_DIR);
46+
System.setProperty("dubbo.resolve.file", getAbsolutePath() + RELATIVE_DEV_DUBBO_RESOVE_FILE);
47+
} else {
48+
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR);
49+
System.setProperty("tomcat.context.docBase", RELATIVE_DOCBASE_DIR);
50+
if ("develop".equalsIgnoreCase(System.getProperty("spring.profiles.active"))
51+
|| "test".equalsIgnoreCase("spring.profiles.active")) {
52+
System.setProperty("dubbo.resolve.file", getAbsolutePath() + RELATIVE_DUBBO_RESOVE_FILE);
53+
}
54+
}
3555

36-
// 设定 Tomcat 的 port
37-
Optional<String> port = Optional.ofNullable(System.getProperty("tomcat.connector.port"));
38-
System.setProperty("tomcat.connector.port", port.orElse(PORT));
56+
if (StringUtils.isEmpty(System.getProperty("tomcat.connector.port"))) {
57+
System.setProperty("tomcat.connector.port", CONNECTOR_PORT);
58+
}
59+
if (StringUtils.isEmpty(System.getProperty("tomcat.server.shutdownPort"))) {
60+
System.setProperty("tomcat.server.shutdownPort",
61+
String.valueOf(Integer.valueOf(System.getProperty("tomcat.connector.port")) + 10000));
62+
}
63+
64+
log.info("====================ENV setting====================");
65+
log.info("spring.profiles.active:" + System.getProperty("spring.profiles.active"));
66+
log.info("dubbo.resolve.file:" + System.getProperty("dubbo.resolve.file"));
67+
log.info("catalina.base:" + System.getProperty("catalina.base"));
68+
log.info("tomcat.host.appBase:" + System.getProperty("tomcat.host.appBase"));
69+
log.info("tomcat.context.docBase:" + System.getProperty("tomcat.context.docBase"));
70+
log.info("tomcat.connector.port:" + System.getProperty("tomcat.connector.port"));
71+
log.info("tomcat.server.shutdownPort:" + System.getProperty("tomcat.server.shutdownPort"));
3972

4073
ExtendedTomcat tomcat = new ExtendedTomcat();
41-
// 开启JNDI,注意maven必须添加tomcat-dbcp依赖
42-
// tomcat.enableNaming();
4374
tomcat.start();
4475
tomcat.getServer().await();
4576
}
4677

4778
private static String getAbsolutePath() {
4879
String path = null;
49-
String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath()
50-
.substring(1);
51-
if (folderPath.indexOf("target") > 0) {
80+
String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
81+
if (folderPath.indexOf("WEB-INF") > 0) {
82+
path = folderPath.substring(0, folderPath.indexOf("WEB-INF"));
83+
} else if (folderPath.indexOf("target") > 0) {
5284
path = folderPath.substring(0, folderPath.indexOf("target"));
5385
}
5486
return path;
@@ -76,18 +108,26 @@ public Server getServer() {
76108
if (server != null) {
77109
return server;
78110
}
111+
// 默认不开启JNDI. 开启时, 注意maven必须添加tomcat-dbcp依赖
112+
System.setProperty("catalina.useNaming", "false");
79113
ExtendedCatalina extendedCatalina = new ExtendedCatalina();
114+
115+
// 覆盖默认的skip和scan jar包配置
116+
System.setProperty(Constants.SKIP_JARS_PROPERTY, "");
117+
System.setProperty(Constants.SCAN_JARS_PROPERTY, "");
118+
80119
Digester digester = extendedCatalina.createStartDigester();
81120
digester.push(extendedCatalina);
82121
try {
83122
server = ((ExtendedCatalina) digester
84123
.parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer();
124+
// 设置catalina.base和catalna.home
85125
this.initBaseDir();
126+
return server;
86127
} catch (Exception e) {
87128
log.error("Error while parsing server.xml", e);
88-
server = null;
89-
} finally {
90-
return server;
129+
throw new RuntimeException("server未创建,请检查server.xml(路径:" + System.getProperty("catalina.base")
130+
+ RELATIVE_SERVERXML_PATH + ")配置是否正确");
91131
}
92132
}
93133

scripts/embed-tomcat-server-boot.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ execOper() {
3939
start)
4040
echo -n "starting server: "
4141
#检查服务是否已经启动
42-
if checkStarted ;then
43-
echo "ERROR: server already started!"
44-
echo "PID: $PIDS"
45-
exit 1
46-
fi
42+
# if checkStarted ;then
43+
# echo "ERROR: server already started!"
44+
# echo "PID: $PIDS"
45+
# exit 1
46+
# fi
4747

4848
args="${javaArgs} -classpath ${classpathArgs} ${bootstrapClass}"
4949
#echo -e "启动参数:\n${args}"

scripts/init.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ cd /home/temp
1212
wget https://raw.githubusercontent.com/dunwu/java-stack/master/scripts/git-clone.sh
1313
chmod 777 git-clone.sh
1414
./git-clone.sh java-stack master
15-
chmod 777 /home/zp/source/java-stack
15+
chmod 777 -R /home/zp/source/java-stack
1616
rm -rf /home/temp

scripts/javatool-server-run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ execBootScript(){
2828

2929
# JVM 参数
3030
# JAVA_OPTS=" -Ddubbo.resolve.file=${RESOURCES_PATH}/dubbo/dubbo-resolve.properties -Djava.awt.headless=true -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true -Dspring.profiles.active=${profile} -Djava.security.egd=file:/dev/./urandom -Xms1024m -Xmx1024m -Xss2m "
31-
JAVA_OPTS=" -Djava.awt.headless=true -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true -Dspring.profiles.active=${profile} -Xms1024m -Xmx1024m -Xss2m "
31+
JAVA_OPTS=" -Djava.awt.headless=true -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Dspring.profiles.active=${profile} -Xms1024m -Xmx1024m -Xss2m "
3232
JAVA_DEBUG_OPTS=""
3333
if [ "$3" == "debug" ]; then
3434
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=2235,server=y,suspend=n "

0 commit comments

Comments
 (0)