本文说明基于tomcat8-maven-plugin打包普通的web项目为可运行jar包的过程;
以及在实际项目执行过程中遇到的中间件连接池依赖改为druid连接池:普通web项目如何使用Druid连接池
com.alibaba
druid
1.2.9
修改了程序内部数据库连接的获取方式
public void createPoolConnection() throws Exception {
if (dataSource == null) {
dataSource = new DruidDataSource();
JMXUtils.register("com.alibaba:type=DruidDataSource", dataSource);
String jdbcUrl = fdbConfig.getDBConn();
String user = fdbConfig.getDBuser();
String password = fdbConfig.getDBpasswd();
String driverClass = fdbConfig.getDBDriver();
int initialSize = fdbConfig.getInitNum();
int minPoolSize = fdbConfig.getInitNum();
int maxPoolSize = fdbConfig.getMaxNumber();
int maxActive = fdbConfig.getMaxNumber();
dataSource.setInitialSize(initialSize);
dataSource.setMaxActive(maxActive);
dataSource.setMinIdle(minPoolSize);
dataSource.setMaxIdle(maxPoolSize);
dataSource.setPoolPreparedStatements(true);
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(jdbcUrl);
dataSource.setPoolPreparedStatements(true);
dataSource.setUsername(user);
dataSource.setPassword(password);
//dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
dataSource.setUseGlobalDataSourceStat(true);
dataSource.setFilters("mergeStat,wall");
}
fConnect = dataSource.getConnection();
fConnect.setAutoCommit(true);
}在web.xml文件中添加如下配置:页面访问路径(http://ip:port/项目根路径/druid/index.html)
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
resetEnable
true
loginUsername
druid
loginPassword
druid
DruidStatView
/druid/*
org.apache.tomcat.maven
tomcat8-maven-plugin
3.0-r1756463
10084
/${context.basepath}
tomcat7
admin
admin
target/${finalPkgName}.jar
tomcat-run
exec-war-only
package
${domainId}-${version}-war-exec.jar
20005
/${context.basepath}
我本地有一个profile为dev的配置
mvn clean package -DskipTests -P dev完成上述过程可以获得一个xxxxxx-war-exec.jar的文件,直接通过java命令运行即可。
上述方式其实也是用了springboot打包时内嵌一个中间件tomcat的方式,springboot中可以通过application.yaml文件指定tomcat运行时的一些自定义配置,上述这种方式也可以通过增加一个server.xml文件配置的方式修改tomcat启动时的一些参数。可满足生产运行要求。
| 留言与评论(共有 0 条评论) “” |