使用Maven构建Dubbo服务的可运行jar包 支持优雅关机

1.Dubbo项目结构如图

使用Maven构建Dubbo服务的可运行jar包 支持优雅关机_第1张图片


2. pom文件配置

#注意:此处只展示打包所需的配置

<build>
        <finalName>dubbo-user-servicefinalName> 
        <resources>
            <resource>
                <targetPath>${project.build.directory}/classestargetPath>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
            <resource>
                <targetPath>${project.build.directory}/classes/META-INF/springtargetPath>
                <directory>src/main/resources/springdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>*.xmlinclude>
                includes>
            resource>
        resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2egroupId>
                    <artifactId>lifecycle-mappingartifactId>
                    <version>1.0.0version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.pluginsgroupId>
                                        <artifactId>maven-dependency-pluginartifactId>
                                        <versionRange>[2.0,)versionRange>
                                        <goals>
                                            <goal>copy-dependenciesgoal>
                                        goals>
                                    pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    action>
                                pluginExecution>
                            pluginExecutions>
                        lifecycleMappingMetadata>
                    configuration>
                plugin>
            plugins>
        pluginManagement>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <version>2.5version>
                <configuration>
                    <classesDirectory>target/classes/classesDirectory>
                    <archive>
                        <manifest>
                            <mainClass>com.alibaba.dubbo.container.MainmainClass>
                            
                            <useUniqueVersions>falseuseUniqueVersions>
                            <addClasspath>trueaddClasspath>
                            <classpathPrefix>lib/classpathPrefix>
                        manifest>
                        <manifestEntries>
                            <Class-Path>.Class-Path>
                        manifestEntries>
                    archive>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-dependency-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-dependenciesid>
                        <phase>packagephase>
                        <goals>
                            <goal>copy-dependenciesgoal>
                        goals>
                        <configuration>
                            <type>jartype>
                            <includeTypes>jarincludeTypes>
                            <useUniqueVersions>falseuseUniqueVersions>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            outputDirectory>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>

3.生成可执行jar文件

  • 生成jar包,项目右键:Run As –> Maven install

    使用Maven构建Dubbo服务的可运行jar包 支持优雅关机_第2张图片

  • 拷贝jar包及lib运行库到服务器上: dubbo-user-service.jar && lib
    使用Maven构建Dubbo服务的可运行jar包 支持优雅关机_第3张图片


4.创建可运行脚本并配置成服务

  • 目录结构(目录及文件名最好统一,便于脚本维护)
    目录结构

  • 创建shell脚本:vim dubbo-user-service.sh

#!/bin/sh
# chkconfig: 2345 20 80
## 配置jdk路径
export JAVA_HOME=/opt/jdk
export JRE_HOME=$JAVA_HOME/jre

## service name
APP_NAME=dubbo-user-service

SERVICE_DIR=/opt/$APP_NAME
SERVICE_NAME=$APP_NAME
JAR_NAME=$SERVICE_NAME\.jar
PID=$SERVICE_NAME\.pid

cd $SERVICE_DIR

case "$1" in

    start)
        nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME >/dev/null 2>&1 &
        echo $! > $SERVICE_DIR/$PID
        echo "=== start $SERVICE_NAME"
        ;;

    stop)
        kill `cat $SERVICE_DIR/$PID`
        rm -rf $SERVICE_DIR/$PID
        echo "=== stop $SERVICE_NAME"

        sleep 5
                ##
                ## edu-service-aa.jar
                ## edu-service-aa-bb.jar
        P_ID=`ps -ef | grep -w "$SERVICE_NAME" | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "=== $SERVICE_NAME process not exists or stop success"
        else
            echo "=== $SERVICE_NAME process pid is:$P_ID"
            echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"
            kill -9 $P_ID
        fi
        ;;

    restart)
        $0 stop
        sleep 2
        $0 start
        echo "=== restart $SERVICE_NAME"
        ;;

    *)
        ## restart
        $0 stop
        sleep 2
        $0 start
        ;;

esac
exit 0
  • 软链到init.d目录下
# cd /etc/init.d/

# ln -s /opt/dubbo-user-service/dubbo-user-service.sh /etc/init.d/dubbo-user-service
  • 设置执行权限
# chmod a+x ./dubbo-user-service
  • 注册成服务
# chkconfig --add dubbo-user-service
# chkconfig --list #可查看配置的服务
  • 设置开机启动
# chkconfig dubbo-user-service on
  • 启动
# service dubbo-user-service start
  • 停止
# service dubbo-user-service stop
  • 重启
# service dubbo-user-service restart

你可能感兴趣的:(Dubbo,maven,dubbo,jar,优雅关机,打包)