阿里云(二)nginx代理绑定域名以及gradle项目cargo远程部署tomcat9

0.前提:
在上一篇文章的3.5处提到 cargo远程部署tomcat9 和通过修改server.xml消除端口项目名 两个操作会冲突,所以使用nginx代理来实现域名直接访问项目,然后cargo实现远程部署tomcat9。

1.nginx代理实现域名直接访问项目
项目中这样写的:
在这里插入图片描述
正常情况访问是这样:域名:端口/项目名/
想要的效果是这样:
在这里插入图片描述

1.1 首先在阿里云服务器中安装nginx
(转)
这边照着操作到这一步就完成了
在这里插入图片描述
然后将nginx服务添加到service
实现以下命令启动关闭

service nginx start
service nginx stop

1.2 修改nginx.conf
如果安装时 直接./configure没有修改默认配置的话,路径在/usr/local/nginx/conf
修改红框里的内容
在这里插入图片描述
增加upstream节点,名称随便
service_name用域名吧,之后再tomcat配置中要用到(service_name为虚拟服务器的识别路径)
location中增加

proxy_pass http://upstream名称/项目名/

这边阿里云安全组要设置80和8088(tomcat改过)端口

1.3 修改tomcat9 server.xml
在这里插入图片描述
在host处配置
name为nginx中的service_name
appBase为空(有文章写过,会重启两次)
增加context节点

1.4 启动
重启tomcat9
启动nginx (有错误会提示,遇到的都是url写的不规范的情况)
浏览器输入域名成功

2.gradle + cargo远程部署tomcat9
用的是gradle3.3

2.1tomcat-users.xml
配置manager权限
在这里插入图片描述

2.2 build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        //gradle脚本需要使用的资源
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
        classpath("com.bmuschko:gradle-cargo-plugin:2.3")
    }
}

repositories {
    mavenCentral()
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
apply plugin: 'com.bmuschko.cargo'

cargo {
    containerId = 'tomcat9x'
    port = 8088

    deployable  {
        context = 'gmweb'
    }
    remote {
        hostname = 'ip'
        username = 'admin'
        password = 'admin123'
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    cargo "org.codehaus.cargo:cargo-core-uberjar:1.5.1",
            "org.codehaus.cargo:cargo-ant:1.5.1"
    // web 和 test
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    // 设置内嵌tomcat
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    // spring boot:run
    runtime('org.springframework.boot:spring-boot-devtools')
    // mysql8需要高版本的
    providedRuntime('mysql:mysql-connector-java:5.1.46')
    // druid
    compile('com.alibaba:druid-spring-boot-starter:1.1.10')
    // mybatis
//    compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1')
    // fastjson
    compile('com.alibaba:fastjson:1.2.5')
    // mybatis-plus
    compile('com.baomidou:mybatis-plus-boot-starter:3.1.1')
    // mp3.0.3版本后需要手动添加生成器和模板
    compile('com.baomidou:mybatis-plus-generator:3.1.1')
    compile('org.apache.velocity:velocity-engine-core:2.1')
    // aop
    compile('org.springframework.boot:spring-boot-starter-aop')
    // lombok
    compile('org.projectlombok:lombok')
}

cargo版本要1.5.1,containerId = tomcat9x,这个可以再cargo官网上看到有个对照表
cargo --remote中的用户密码就是tomcat-users.xml中设置的

2.3context.xml
位置在
/webapps/manager/META-INF

在这里插入图片描述
注释掉value,以便放开访问

2.4 启动
重启tomcat9
浏览器登录manager(用ip)确认OK
在这里插入图片描述
远程部署,先打war包,再redeploy
在这里插入图片描述
在这里插入图片描述

2.5 启动日志发现mysql驱动注册失败
在这里插入图片描述
不影响启动,但是看着不爽
意思是:web 容器重启一个叫Abandoned connection cleanup thread的线程失败。是因为创建了一个内存泄露。
堆栈往上是说:mysql驱动注册失败,原因是为了阻止内存泄露。

目前的解决办法是:
将mysql包的scope修改为provided

 providedRuntime('mysql:mysql-connector-java:5.1.46')

然后将mysql包放到tomcat9的lib下
在这里插入图片描述
重启tomcat9

补充网上的解释:

root cause is that Tomcat have problems to garbage collect the driver because it is registered in a singleton common to several applications. 
Closing one application does not allow Tomcat to release the driver. 

JDBC drivers register themselves in the JVM-wide singleton DriverManager which is shared by all web apps. If you have the same (as in class name) JDBC driver register twice from two different web apps, this might cause your problem. This is even more problematic if your web apps use different versions of the same JDBC driver.

Also, putting JDBC drivers into Tomcat's lib folder will help prevent memory leaks when you redeploy your web app without restarting Tomcat, e.g. if you just put a new WAR file into Tomcat's webapps folder:

The class DriverManager gets loaded by the bootstrap classloader and thereby "lives" globally in the JVM, while Tomcat loads all web apps in their own classloaders. So if a JDBC driver from a web app's WEB-INF/lib folder registers itself in DriverManager, it pins that web app's classloader in memory (and thereby all the classes of that web app), preventing its garbage collection.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章