How to package springboot into a war package and publish it

1. Scenario: Distributed testing and development use large memory consumption and use unified publishing. Tomcat core configuration reference

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener"/>
    <!-- APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on"/>
    <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    <GlobalNamingResources>
        <Resource name="UserDatabase" auth="Container"
                  type="org.apache.catalina.UserDatabase"
                  description="User database that can be updated and saved"
                  factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                  pathname="conf/tomcat-users.xml"/>
    </GlobalNamingResources>
    <!-- A "Service" is a collection of one or more "Connectors" that share
         a single "Container" Note:  A "Service" is not itself a "Container",
         so you may not define subcomponents such as "Valves" at this level.
         Documentation at /docs/config/service.html
     -->
    <!--  <Service name="Catalina">-->
    <!--        <Connector port="8080" protocol="HTTP/1.1"-->
    <!--                   connectionTimeout="20000"-->
    <!--                   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="localhost"  appBase="webapps"-->
    <!--                unpackWARs="true" autoDeploy="true">-->
    <!--            <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>-->


    <!--  schedule-service-->
    <Service name="CatalinaScheduleService">
        <Connector port="9009" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"/>
        <Engine name="CatalinaScheduleService" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
            </Realm>
            <Host name="localhost" appBase="scheduleService"
                  unpackWARs="true" autoDeploy="true">
                <Context path="/app"
                         docBase="D:\ProductDoc\apache-tomcat-9.0.63-windows-x64\apache-tomcat-9.0.63\wars\schedule-service-1.0.1.war"
                         debug="0"
                         privileged="true"/>
                <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>

    <!--  common-message-service-->
    <Service name="CatalinaCommonMessageService">
        <Connector port="9015" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"/>
        <Engine name="CatalinaCommonMessageService" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
            </Realm>
            <Host name="localhost" appBase="commonMessageService"
                  unpackWARs="true" autoDeploy="true">
                <Context path=""
                         docBase="D:\ProductDoc\apache-tomcat-9.0.63-windows-x64\apache-tomcat-9.0.63\wars\common-message-service-0.0.1.war"
                         debug="0"
                         privileged="true"/>
                <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>

    <!--  crms-service-->
    <Service name="CatalinaCrmsService">
        <Connector port="9010" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"/>
        <Engine name="CatalinaCrmsService" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
                <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                       resourceName="UserDatabase"/>
            </Realm>
            <Host name="localhost" appBase="crmsService"
                  unpackWARs="true" autoDeploy="true">
                <Context path="/v1"
                         docBase="D:\ProductDoc\apache-tomcat-9.0.63-windows-x64\apache-tomcat-9.0.63\wars\crms-service-1.0.war"
                         debug="0"
                         privileged="true"/>
                <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>




</Server>

2. Springboot is packaged into a war package

1. Specify the packaging as war type in the pom configuration file, and remove the tomcat that comes with the web   

<packaging>war</packaging>


<exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
</exclusions>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <scope>provided</scope>
</dependency>

2. Modify the build configuration

 <build>
        <!-- 打包后的服务名称 -->
        <finalName>base-service-1.0.1</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--可以不用添加web.xml-->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            <plugin>
                <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-deploy-plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <!--deploy 时忽略 model-->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

 3. Modify the startup class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }

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


}

4. Add Tomcat registration to nacos configuration

import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Component
public class NacosListener implements ApplicationListener<ApplicationReadyEvent> {
    @Resource
    private NacosRegistration registration;

    @Resource
    private NacosAutoServiceRegistration nacosAutoServiceRegistration;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        String property = event.getApplicationContext().getEnvironment().getProperty("server.port");
        if (!StringUtils.isEmpty(property)) {
            registration.setPort(Integer.parseInt(property));
        }

        nacosAutoServiceRegistration.start();
    }

    @EventListener(ApplicationReadyEvent.class)
    public void onWebServerReady(ApplicationReadyEvent event) {
        String property = event.getApplicationContext().getEnvironment().getProperty("server.port");
        if (!StringUtils.isEmpty(property)) {
            registration.setPort(Integer.parseInt(property));
        }
        nacosAutoServiceRegistration.start();
    }
}

3. Package deployment

1. Clean up the project and package it

 2. Copy the jar package to the webapps directory of tomcat and modify the package name ( the package name is the route . If you directly create the jar package, it has a port, but the war can only access the service through the tomcat port + package name)

 3. Enter the bin directory of tomcat and directly enter cmd in the file address bar and press Enter.

4. Enter startup (shutdown command)

 5. To check the application, enter localhost:8080 and then enter the user name admin and password admin to check whether the app starts normally.

6. The page access interface is successfully reached. The simple work of packaging springboot into war is completed.

4. Configure tomcat in idea2021

1. Click Edit Configuration

2. In step 4, select the directory where the tomcat you just downloaded is decompressed.

 3. Click Arfact... to add a runnable war package project

 4. Direct start

 5. Tomcat download and simple configuration (jdk configuration is omitted. This article uses jdk1.8)

1. Download the Tomcat related version from the official website ( Apache Tomcat® - Welcome! ). Here I downloaded tomcat9+win64 for demonstration purposes. Of course, you can download it yourself according to your own system and tomcat version requirements.

2. Demonstration account configuration. Because I downloaded the unzipped version, I directly entered the bin directory of tomcat after unzipping. You can also configure environment variables. Let’s just add an administrator account first and don’t need to configure anything else.

6. Warm reminder:

1. When centos starts, "-bash: ./startup.sh: Permission denied" appears. You can run the (chmod u+x *.sh) command to grant execution permissions.

2. When tomcat reads the nacos configuration, garbled characters appear and cannot be published. Please add set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dfile.encoding=UTF- in tomcat/bin/catalina.bat (.sh changes the script according to the system where tomcat is located). 8"

3. If the war package cannot be started, you can check the reason through log analysis: for example, if the referenced jar package version conflicts, you can delete the lib-related conflicting jar package in WEB-INF under the project that cannot be started.

Guess you like

Origin blog.csdn.net/qq_29653373/article/details/125095171