admin 管理员组文章数量: 1184232
bill
[开发背景]
tomcat启动之后占用三个端口。我对此非常不理解,我决定在tomcat基础之上,开发一个新的tomcat,仅仅占用一个端口。
gitee地址:
bill-tomcat开发
创建工程
pom.xml中添加依赖
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><jdk.version>1.6</jdk.version>
</properties>
<dependencies><dependency><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>
</dependencies>
<build><plugins><plugin><!-- java编译插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>${jdk.version}</source><target>${jdk.version}</target><encoding>UTF-8</encoding></configuration></plugin></plugins>
</build>
在src/main/resources创建webapps目录
在src/main/resources创建tomcat.properties
内容如下
tomcat.port=8080
进行编码
ConfigUtil.java
package com.litong.utils.file;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.URL;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;/*** @author litong 读取config.properties*/
public class ConfigUtil {private static String configFilePath = "tomcat.properties";private static Properties prop = null;private static InputStream ins = null;static {ins = ConfigUtil.class.getClassLoader().getResourceAsStream(configFilePath);// 防止读取乱码InputStreamReader insReader = null;try {insReader = new InputStreamReader(ins, "UTF-8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}prop = new Properties();try {prop.load(insReader);} catch (IOException e) {e.printStackTrace();}}/*** get value of key*/public static String getValue(String key) {return prop.getProperty(key);}public static int getIntValue(String key) {return Integer.parseInt(prop.getProperty(key));}/*** 设置值,设置值后存盘* @param key* @param value*/public static void put(String key, String value) {prop.put(key, value);ConfigUtil.save();}public static void save() {URL url = ConfigUtil.class.getClassLoader().getResource(configFilePath);File file = new File(url.getFile());FileOutputStream fileOutputStream = null;try {fileOutputStream = new FileOutputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();}try {prop.store(fileOutputStream, null);} catch (IOException e) {e.printStackTrace();}}/*** get properties*/public static Properties getProperties() {return prop;}/*** * @return 配置文件名称*/public static String getConfigFile() {return configFilePath;}public static void main(String[] args) {// String value = ConfigUtil.getValue("prorject_id");// System.out.println(value);Properties prop = ConfigUtil.getProperties();Set<Entry<Object, Object>> entrySet = prop.entrySet();for (Entry<Object, Object> entry : entrySet) {System.out.println("" + entry.getKey() + "=" + entry.getValue());}}
}
Constants.java
package com.uairobot.bill.tomcat;public class Constants {public static final String tomcatPort = "tomcat.port";
}
TomcatUtil.java
package com.uairobot.bill.tomcat;import java.io.File;
import java.URL;import com.litong.utils.file.ConfigUtil;public class TomcatUtil {private static URL webappsPath;private static File webappFile;private static ClassLoader classLoader;public static URL getWebappsPath() {if (webappsPath == null) {webappsPath = getClassLoader().getResource("webapps");}return webappsPath;}public static File getWebappsFile() {if (webappFile == null) {URL webappPath = getWebappsPath();if (webappPath == null) {String file = getClassLoader().getResource("").getFile();System.out.println("webappPath is null,auto create webapps in " + file);webappFile = new File(file + "webapps");if (!webappFile.exists()) {webappFile.mkdirs();}} else {webappFile = new File(webappPath.getFile());}}return webappFile;}public static ClassLoader getClassLoader() {if (classLoader == null) {classLoader = TomcatUtil.class.getClassLoader();}return classLoader;}public static int getPort() {return ConfigUtil.getIntValue(Constants.tomcatPort);}
}
TomcatServer.java
package com.uairobot.bill.tomcat;import java.io.File;import javax.servlet.ServletException;import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;public class TomcatServer {public static Tomcat tomcat = null;public static void main(String[] args) {long start = System.currentTimeMillis();File webappsFile = TomcatUtil.getWebappsFile();File[] listFiles = webappsFile.listFiles();// 创建root目录String rootPath = webappsFile.getAbsoluteFile() + File.separator + "ROOT";File rootFile = new File(rootPath);if (!rootFile.exists()) {rootFile.mkdirs();}tomcat = new Tomcat();// 添加webapptry {tomcat.addWebapp("/", rootPath);System.out.println("add webapp " + "/" + "=>" + rootPath);for (File file : listFiles) {String contextPath = "/" + file.getName();String baseDir = file.getAbsolutePath();tomcat.addWebapp(contextPath, baseDir);System.out.println("add webapp " + contextPath + "=>" + baseDir);}} catch (ServletException e1) {e1.printStackTrace();}// 设置tomcat端口tomcat.setPort(TomcatUtil.getPort());// 启动tomcattry {tomcat.start();} catch (LifecycleException e) {e.printStackTrace();}long end = System.currentTimeMillis();System.out.println("启动完成,共使用了:" + (end - start) + "ms");// 等待接收关闭指令tomcat.getServer().await();}
}
执行TomcatServer就可以启动项目
bill-tomcat打包
打包概述和启动命令
what is 打包
打包:将程序打包成tar.gz文件,可以放到服务器上运行
[总体概述]
将依赖包和项目包放到lib目录下
将配置文件放到config目录下载
将webapps,放到webapps目录下
[启动命令]
因为要在classpath下载寻找webapps所以需要将当前目录,config目录和lib目录添加到到classpath下
在linux上前台启动
java -Xverify:none -cp .:./config:./lib/*: com.uairobot.bill.tomcat.TomcatServer
maven-jar-plugin
maven-jar-plugin排除配置文件和webapps目录不添加到jar包中
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.6</version><configuration><excludes><exclude>*.txt</exclude><exclude>*.xml</exclude><exclude>*.properties</exclude><exclude>webapps</exclude><exclude>webapps/*</exclude><exclude>sql</exclude><exclude>sql/*</exclude></excludes></configuration>
</plugin>
maven-assembly-plugin
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><version>3.1.0</version><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals><configuration><!-- jar 等压缩文件在被打包进入 zip、tar.gz 时是否压缩,设置为 false 可加快打包速度 --><recompressZippedFiles>false</recompressZippedFiles><!-- 打包生成的文件是否要追加 release.xml 中定义的 id 值 --><appendAssemblyId>true</appendAssemblyId><!-- 指向打包描述文件 package.xml --><descriptors><descriptor>src/main/assembly/package.xml</descriptor></descriptors><!-- 打包结果输出的基础目录 --><outputDirectory>${project.build.directory}/</outputDirectory></configuration></execution></executions>
</plugin>
src\main\assembly\package.xml内容如下
<assembly xmlns=".0.0" xmlns:xsi=""xsi:schemaLocation=".0.0 .0.0.xsd"><!-- assembly 打包配置更多配置可参考官司方文档: .html --><id>release</id><!-- 设置打包格式,可同时设置多种格式,常用格式有:dir、zip、tar、tar.gz dir 格式便于在本地测试打包结果 zip 格式便于 windows 系统下解压运行 tar、tar.gz 格式便于 linux 系统下解压运行 --><formats><!-- <format>dir</format> <format>zip</format> --><format>tar.gz</format></formats><!-- 打 zip 设置为 true 时,会在 zip 包中生成一个根目录,打 dir 时设置为 false 少层目录 --><includeBaseDirectory>true</includeBaseDirectory><fileSets><!-- src/main/resources 配置文件 copy 到 config 目录下 --><fileSet><directory>${basedir}/src/main/resources</directory><outputDirectory>config</outputDirectory><includes><include>*.xml</include><include>*.properties</include><include>*.txt</include></includes></fileSet><!-- 复制sql文件到config目录下 --><fileSet><directory>${basedir}/src/main/resources/sql</directory><outputDirectory>config/sql</outputDirectory></fileSet><!--复制静态文件 --><fileSet><directory>${basedir}/src/main/resources/webapps</directory><outputDirectory>webapps</outputDirectory></fileSet><!-- 复制service文件 --><fileSet><directory>${basedir}/src/main/bin</directory><lineEnding>unix</lineEnding><outputDirectory>service</outputDirectory><!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 --><fileMode>755</fileMode><includes><include>*.service</include></includes></fileSet><!-- 项目根下面的脚本文件 copy 到根目录下 --><fileSet><directory>${basedir}/src/main/bin</directory><lineEnding>unix</lineEnding><outputDirectory></outputDirectory><!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 --><fileMode>755</fileMode><includes><include>*.sh</include></includes></fileSet><fileSet><directory>${basedir}/src/main/bin</directory><lineEnding>windows</lineEnding><outputDirectory></outputDirectory><!-- 脚本文件在 linux 下的权限设为 755,无需 chmod 可直接运行 --><fileMode>755</fileMode><includes><include>*.bat</include></includes></fileSet></fileSets><!-- 依赖的 jar 包 copy 到 lib 目录下 --><dependencySets><dependencySet><outputDirectory>lib</outputDirectory></dependencySet></dependencySets>
</assembly>
启动脚本
src\main\bin\bill-tomcat.sh内容如下
#!/bin/sh
# chkconfig: 345 99 01
# description:bill-tomcat##########################
# get app home start
###########################
PRG="$0"
while [ -h "$PRG" ] ; dols=`ls -ld "$PRG"`link=`expr "$ls" : '.*-> \(.*\)$'`if expr "$link" : '/.*' > /dev/null; thenPRG="$link"elsePRG=`dirname "$PRG"`/"$link"fi
done
##########################
# get app home end
#####################################################
# custom variables start
###########################
JAVA_HOME=/usr/java/jdk1.8.0_211
APP_HOME=`dirname "$PRG"`
APP_NAME=`basename "$PRG"`
PID_FILE=$APP_HOME/$APP_NAME.pid
CP=$APP_HOME:$APP_HOME/config:$APP_HOME/lib/*
# 启动入口类,该脚本文件用于别的项目时要改这里
MAIN_CLASS=com.uairobot.bill.tomcat.TomcatServer
# Java 命令行参数,根据需要开启下面的配置,改成自己需要的,注意等号前后不能有空格
# JAVA_OPTS="-Xms256m -Xmx1024m -Dundertow.port=80 -Dundertow.host=0.0.0.0"
# JAVA_OPTS="-Dundertow.port=80 -Dundertow.host=0.0.0.0"
CMD="$JAVA_HOME/bin/java -Xverify:none ${JAVA_OPTS} -cp ${CP} ${MAIN_CLASS}"
###########################
# custom variables end
###########################
source /etc/init.d/functions
#########################
# define funcation start
##########################
if [[ "$MAIN_CLASS" == "com.yourpackage.YourMainClass" ]]; then
echo "请先修改 MAIN_CLASS 的值为你自己项目启动Class,然后再执行此脚本。"exit 0
fi
lock_dir=/var/lock/subsys
lock_file=$lock_dir/$APP_NAME
createLockFile(){[ -w $lock_dir ] && touch $lock_file
}start(){[ -e $APP_HOME/logs ] || mkdir $APP_HOME/logs -pif [ -f $PID_FILE ]thenecho 'alread running...'elseecho $CMDnohup $CMD >> $APP_HOME/logs/$APP_NAME.log 2>&1 &echo $! > $PID_FILEcreateLockFileecho_successfi
}stop(){if [ -f $PID_FILE ]thenkillproc -p $PID_FILErm -f $PID_FILEecho_successelseecho 'not running...'fi
}restart(){stopstart
}status(){cat $PID_FILE
}
##########################
# define function end
##########################
ACTION=$1
case $ACTION instart)start;;stop)stop;;restart)restart;;status)status;;*)echo usage "{start|stop|restart|status}";;
esac
打包
使用clean package -DskipTests在Eclipse会出现中会出现MANIFEST.MF (系统找不到指定的路径。)所以执行install
clean install -DskipTests
打包完成后生成文件名bill-tomcat-1.0-release.tar.gz,文件大小是13.3 MB
解压后的目录结果如下
bill-tomcat-1.0
├── bill-tomcat.sh
├── config
│ ├── tomcat.properties
├── lib
│ ├── backport-util-concurrent-3.1.jar
│ ├── bill-tomcat-1.0.jar
│ ├── classworlds-1.1-alpha-2.jar
│ ├── commons-cli-1.2.jar
│ ├── commons-codec-1.6.jar
│ ├── commons-compress-1.4.1.jar
│ ├── commons-io-2.2.jar
│ ├── commons-lang-2.6.jar
│ ├── commons-logging-1.1.3.jar
│ ├── common-tomcat-maven-plugin-2.2.jar
│ ├── doxia-sink-api-1.0-alpha-7.jar
│ ├── ecj-4.2.2.jar
│ ├── guava-10.0.1.jar
│ ├── httpclient-4.3.1.jar
│ ├── httpcore-4.3.jar
│ ├── jcl-over-slf4j-1.7.5.jar
│ ├── jsch-0.1.27.jar
│ ├── jsr305-1.3.9.jar
│ ├── jtidy-4aug2000r7-dev.jar
│ ├── maven-archiver-2.4.2.jar
│ ├── maven-artifact-2.0.6.jar
│ ├── maven-artifact-manager-2.2.1.jar
│ ├── maven-core-2.0.6.jar
│ ├── maven-error-diagnostics-2.0.6.jar
│ ├── maven-filtering-1.0.jar
│ ├── maven-model-2.0.6.jar
│ ├── maven-monitor-2.0.6.jar
│ ├── maven-plugin-api-2.2.1.jar
│ ├── maven-plugin-descriptor-2.0.6.jar
│ ├── maven-plugin-parameter-documenter-2.0.6.jar
│ ├── maven-plugin-registry-2.2.1.jar
│ ├── maven-profile-2.2.1.jar
│ ├── maven-project-2.2.1.jar
│ ├── maven-reporting-api-2.0.6.jar
│ ├── maven-repository-metadata-2.0.6.jar
│ ├── maven-settings-2.0.6.jar
│ ├── plexus-archiver-2.1.1.jar
│ ├── plexus-build-api-0.0.4.jar
│ ├── plexus-classworlds-2.2.2.jar
│ ├── plexus-component-annotations-1.5.5.jar
│ ├── plexus-container-default-1.0-alpha-9-stable-1.jar
│ ├── plexus-interactivity-api-1.0-alpha-4.jar
│ ├── plexus-interpolation-1.13.jar
│ ├── plexus-io-2.0.3.jar
│ ├── plexus-utils-3.0.15.jar
│ ├── slf4j-api-1.7.5.jar
│ ├── tomcat7-maven-plugin-2.2.jar
│ ├── tomcat7-war-runner-2.2.jar
│ ├── tomcat-annotations-api-7.0.47.jar
│ ├── tomcat-api-7.0.47.jar
│ ├── tomcat-catalina-7.0.47.jar
│ ├── tomcat-catalina-ha-7.0.47.jar
│ ├── tomcat-coyote-7.0.47.jar
│ ├── tomcat-dbcp-7.0.47.jar
│ ├── tomcat-el-api-7.0.47.jar
│ ├── tomcat-embed-core-7.0.47.jar
│ ├── tomcat-embed-logging-juli-7.0.47.jar
│ ├── tomcat-embed-logging-log4j-7.0.47.jar
│ ├── tomcat-jasper-7.0.47.jar
│ ├── tomcat-jasper-el-7.0.47.jar
│ ├── tomcat-jdbc-7.0.47.jar
│ ├── tomcat-jsp-api-7.0.47.jar
│ ├── tomcat-juli-7.0.47.jar
│ ├── tomcat-servlet-api-7.0.47.jar
│ ├── tomcat-tribes-7.0.47.jar
│ ├── tomcat-util-7.0.47.jar
│ ├── wagon-file-1.0-beta-2.jar
│ ├── wagon-http-lightweight-1.0-beta-2.jar
│ ├── wagon-http-shared-1.0-beta-2.jar
│ ├── wagon-provider-api-1.0-beta-2.jar
│ ├── wagon-ssh-1.0-beta-2.jar
│ ├── wagon-ssh-common-1.0-beta-2.jar
│ ├── wagon-ssh-external-1.0-beta-2.jar
│ ├── xml-apis-1.0.b2.jar
│ └── xz-1.0.jar
└── webapps└── ROOT└── index.html
bill-tomcat安装
[下载]
.0
下载bill-tomcat-1.0-release.tar.gz
[bill-tomcat安装]
mkdir /opt/package/bill-tomcat
cd /opt/package/bill-tomcat
#上传bill-tomcat-1.0-release.tar.gz到此目录下
tar -xf bill-tomcat-1.0-release.tar.gz -C /usr/local/
cd /opt/package/bill-tomcat
#在linux上后台启动
./bill-tomcat.sh start
在linux上前台启动
cd /opt/package/bill-tomcat
/usr/java/jdk1.8.0_211/bin/java -Xverify:none -cp ./config:./lib/*: com.uairobot.bill.tomcat.TomcatServer
[向bill-tomcat安装部署项目]
#进入webapps目录
cd /usr/local/bill-tomcat-1.0/webapps
#本tomcat不会自动解压war包,需要手动下载war并解压到此目录下
wget .0.war
mkdir bill-websocket-web-chat
unzip bill-websocket-web-chat-1.0.war -d bill-websocket-web-chat
#重启让部署的项目生效
/usr/local/bill-tomcat-1.0/bill-tomcat.sh restart
#访问
http://192.168.78.131:8080/bill-websocket-web-chat/
基于bill-tomcat进行开发
其实很简单,在pom.xml中添加omcat7-maven-plugin的依赖即可,这样我们就不需要向项目中添加重复的依赖了
<dependency><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><scope>provided</scope>
</dependency>
依赖图
本文标签: bill
版权声明:本文标题:bill 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1693852285a245925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论