Skip to content

Commit ad3f811

Browse files
committed
✨ 添加嵌入式 Tomcat 示例
1 parent aa4e12b commit ad3f811

6 files changed

Lines changed: 145 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
classes
44
target
55
logs
6+
work
67

78
# temp files
89
*.class

codes/javatool/server/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# javatool-server
2+
3+
> 本示例代码主要展示嵌入式服务器和 web 项目的集成。
4+
>
5+
> 你可以在本项目中体验嵌入式 Tomcat 和嵌入式 Jetty 的启动方式。
6+
>
7+
8+
**版本**
9+
10+
- JDK:1.8
11+
- Tomcat:8.5.24
12+
13+
## Tomcat
14+
15+
### API 启动嵌入式 Tomcat
16+
17+
执行 `io.github.dunwu.javatool.server.SimpleTomcatServer#main` 方法。
18+
19+
或执行 `io.github.dunwu.javatool.server.TomcatServer.main` 方法。
20+
21+
启动后,访问 http://localhost:8080/javatool-server/
22+
23+
### 插件启动嵌入式 Tomcat
24+
25+
由于插件很久没有更新(最新版本发布时间:2013-11-11),目前只能找到 Tomcat6 、Tomcat7 插件,所以弃用。
26+
27+
## Jetty
28+
29+
待添加。。。

codes/javatool/server/pom.xml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
<!-- javaee end -->
4848

4949
<!-- server betgin -->
50-
<dependency>
51-
<groupId>org.apache.tomcat.embed</groupId>
52-
<artifactId>tomcat-embed-core</artifactId>
53-
<version>${tomcat.version}</version>
54-
</dependency>
50+
<dependency>
51+
<groupId>org.apache.tomcat.embed</groupId>
52+
<artifactId>tomcat-embed-core</artifactId>
53+
<version>${tomcat.version}</version>
54+
</dependency>
5555
<dependency>
5656
<groupId>org.apache.tomcat.embed</groupId>
5757
<artifactId>tomcat-embed-el</artifactId>
@@ -88,6 +88,19 @@
8888

8989
<!-- [Part 2] BUILD SETTINGS BEGIN -->
9090
<build>
91+
<plugins>
92+
<!-- 由于一直没找到 tomcat8 的插件,所以弃用 -->
93+
<!--<plugin>
94+
<groupId>org.apache.tomcat.maven</groupId>
95+
<artifactId>tomcat7-maven-plugin</artifactId>
96+
<version>2.2</version>
97+
<configuration>
98+
<port>8080</port>
99+
<path>/${project.artifactId}</path>
100+
<uriEncoding>UTF-8</uriEncoding>
101+
</configuration>
102+
</plugin>-->
103+
</plugins>
91104
</build>
92105
<!-- [Part 2] BUILD SETTINGS END -->
93106

codes/javatool/server/src/main/java/io/github/dunwu/javatool/function/SimpleTomcatServer.java renamed to codes/javatool/server/src/main/java/io/github/dunwu/javatool/server/SimpleTomcatServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package io.github.dunwu.javatool.function;
1+
package io.github.dunwu.javatool.server;
22

33
import java.util.Optional;
44

55
import org.apache.catalina.startup.Tomcat;
66

77
/**
88
* 简单的嵌入式 Tomcat 启动类
9+
* 启动后可访问 http://localhost:8080/javatool-server/
910
* @author Zhang Peng
1011
*/
1112
public class SimpleTomcatServer {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.github.dunwu.javatool.server;
2+
3+
import java.io.File;
4+
import java.util.Optional;
5+
6+
import org.apache.catalina.Server;
7+
import org.apache.catalina.startup.Catalina;
8+
import org.apache.catalina.startup.Tomcat;
9+
import org.apache.tomcat.util.digester.Digester;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
/**
14+
* 嵌入式 Tomcat 启动类
15+
* 启动后可访问 http://localhost:8080/javatool-server/
16+
* @author Zhang Peng
17+
*/
18+
public class TomcatServer {
19+
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/";
24+
25+
/**
26+
* 除了 spring.profiles.active,System.setProperty 设置的属性都是为了配置 server.xml
27+
*/
28+
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"));
32+
33+
// 设定 Tomcat 的 catalina.base
34+
System.setProperty("catalina.base", getAbsolutePath() + RELATIVE_BASE_DIR);
35+
36+
// 设定 Tomcat 的 port
37+
Optional<String> port = Optional.ofNullable(System.getProperty("tomcat.connector.port"));
38+
System.setProperty("tomcat.connector.port", port.orElse(PORT));
39+
40+
ExtendedTomcat tomcat = new ExtendedTomcat();
41+
// 开启JNDI,注意maven必须添加tomcat-dbcp依赖
42+
// tomcat.enableNaming();
43+
tomcat.start();
44+
tomcat.getServer().await();
45+
}
46+
47+
private static String getAbsolutePath() {
48+
String path = null;
49+
String folderPath = TomcatServer.class.getProtectionDomain().getCodeSource().getLocation().getPath()
50+
.substring(1);
51+
if (folderPath.indexOf("target") > 0) {
52+
path = folderPath.substring(0, folderPath.indexOf("target"));
53+
}
54+
return path;
55+
}
56+
}
57+
58+
/**
59+
* Tomcat 扩展类,添加功能是启动时,加载 src/main/resources/tomcat/conf/server.xml 文件中配置,而非默认配置。
60+
*/
61+
class ExtendedTomcat extends Tomcat {
62+
63+
private Logger log = LoggerFactory.getLogger(this.getClass());
64+
65+
private static final String RELATIVE_SERVERXML_PATH = "/conf/server.xml";
66+
67+
private static class ExtendedCatalina extends Catalina {
68+
@Override
69+
public Digester createStartDigester() {
70+
return super.createStartDigester();
71+
}
72+
}
73+
74+
@Override
75+
public Server getServer() {
76+
if (server != null) {
77+
return server;
78+
}
79+
ExtendedCatalina extendedCatalina = new ExtendedCatalina();
80+
Digester digester = extendedCatalina.createStartDigester();
81+
digester.push(extendedCatalina);
82+
try {
83+
server = ((ExtendedCatalina) digester
84+
.parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer();
85+
this.initBaseDir();
86+
} catch (Exception e) {
87+
log.error("Error while parsing server.xml", e);
88+
server = null;
89+
} finally {
90+
return server;
91+
}
92+
}
93+
94+
}

codes/javatool/server/src/main/resources/tomcat/conf/server.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<Host name="localhost" createDirs="false" appBase="${catalina.base}/../../../../" unpackWARs="false"
2020
autoDeploy="false"
2121
deployOnStartup="false" failCtxIfServletStartFails="true">
22-
<Context path="" docBase="src/main/webapp" reloadable="false" logEffectiveWebXml="false" privileged="false"
22+
<Context path="/javatool-server" docBase="src/main/webapp" reloadable="false" logEffectiveWebXml="false" privileged="false"
2323
swallowOutput="false" workDir="${catalina.base}/work"/>
2424
</Host>
2525
</Engine>

0 commit comments

Comments
 (0)