Idea 打war包并部署tomcat9运行

2019-07-31  本文已影响0人  CoderLJW

1、修改项目pom

##### 1 修改打包方式
    <groupId>com.ljw.xd</groupId>
    <artifactId>wechatpay</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>wechatpay</name>
    <!--默认打jar包-->
    <!--<packaging>war</packaging>-->
    <description>Demo</description>
##### 2 移除内置的tomcat插件
# 方式1
        <!--部署成war包时开启↓↓↓↓
        scope的其他参数如下:
        compile:默认的scope,表示 dependency 都可以在生命周期中使用。而且,
            这些dependencies 会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布
        provided:跟compile相似,但是表明了dependency 由JDK或者容器提供,
            例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性
        runtime:表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段
        test:表示dependency作用在测试时,不作用在运行时。 只在测试时使用,用于编译和运行测试代码。不会随项目发布。
        system:跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>compile</scope>
        </dependency>
        <!--部署成war包时开启↑↑↑↑-->
# 方式2
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
##### 3 修改打包的名称(war和jar)
    <build>
        <!--修改打包的名称(war和jar)-->
        <finalName>wechatpay</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
##### 这里pom配置结束

##### 需要再修改项目主类WechatpayApplication
1、继承 SpringBootServletInitializer
2、重写 configure

@SpringBootApplication
// 限定扫描位置,提高效率,可以省去写@Mapper注解。默认的是都扫描,哪个带有@Mapper就加载进来
@MapperScan(value = {"com.ljw.xd.wechatpay.mapper"})
public class WechatpayApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(WechatpayApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WechatpayApplication.class);
    }
}

2、配置项目File--->ProjectStructure

image.png

一方法,直接配置成war包的Exploded方式

二种打包方式,这个出来就是xxx.war包

运行在tomcat9下。

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/usr/local/java/qqq_war" reloadable="true"></Context>

tomcat介绍如下

image.png
http://192.168.31.220:8080/xx/wares/data/test
http://192.168.31.220:8888/xx/wares/data/test
image.png
# 添加
127.0.0.1 www.ljw.com

配置如下

    <Service name="Catalina">
    <Connector port="9000" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="www.ljw.com"  appBase="xxxx"
            unpackWARs="true" autoDeploy="true">
        <Context reloadable="true" path="/" docBase="/Users/paycloud110/JianWei/BaseProjectBackup/java/function/wechatpay/out/artifacts/wechatpay_war_exploded1234567"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
    <Service name="Catalina">
    <Connector port="9090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="www.ljw.com"  appBase="xxxx"
            unpackWARs="true" autoDeploy="true">
        <Context reloadable="true" path="/" docBase="/Users/paycloud110/JianWei/BaseProjectBackup/java/function/wechatpay/out/artifacts/wechatpay_war_exploded1234567"/>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>

Tomcat部署多个Sring Boot项目时出现org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean

简单来说,是JMX出现了问题。Spring 默认JMX是开启的,所以解决方法之一就是在application.properties或者application.yml里配置spring.jmx.enabled=false;

还有一种解决方法便是为spring.jmx.default-domain赋值;
这样就能解决重名问题啦;

添加nginx后的访问

使用ip:port访问就是如下
# 使用ip:port访问就是如下
http://192.168.31.220:8080/qqq/wares/data/test
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/usr/local/java/qqq_war" reloadable="true"></Context>
{
"code": 0,
"msg": "success",
"data": "test 就是 tomcat"
}
使用nginx的访问
# nginx的配置如下

server {
        listen       80;
        server_name  15a7d9a6a585.ngrok.io;
location /m80 {
                proxy_pass http://127.0.0.1:8080/;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 5000M;
        }
location /m88 {
                proxy_pass http://127.0.0.1:8888/;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                client_max_body_size 5000M;
        }       
location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
http://15a7d9a6a585.ngrok.io/m80/qqq/wares/data/test
http://15a7d9a6a585.ngrok.io/m88/aaa/wares/data/test
上一篇下一篇

猜你喜欢

热点阅读