springboot项目通过ssh实现远程自动部署

  • springboot项目通过ssh实现远程自动部署
  • 在项目pom.xml文件中引入ssh上传jar包
  • 引入wagon-maven-plugin插件
  • 远程启动服务脚本
  • 监控服务脚本
  • 定时任务脚本
  • 打包上传
  • 通过命令打包上传
  • 通过idea 打包上传
  • 执行启动命令
  • 通过命令行启动
  • 通过idea执行maven插件启动服务
  • 打包错误
  • 执行上传命令错误
  • 执行shell脚本错误integer expression expected
  • 执行脚本错误nohup: failed to run command 'java': No such file or directory
  • 远程启动不能链接服务器Failed to execute goal org.codehaus.mojo:wagon-maven-plugin:1.0:upload-single (default-cli) on project daison-server: Unable to create a Wagon instance for scp://root:123456@8.135.42.12:22/home/daison/admin: Cannot connect. Reason: SSH_MSG_DISCONNECT: 2 Too many authentication failures -> [Help 1]


springboot项目通过ssh实现远程自动部署

在项目中,经常需要发布jar包、war包到服务器,通常我们使用本地编译,打包,然后将打成的包手动上传到服务器,操作非常麻烦。该文章记录windows本地通过命令执行打包后直接上传服务器.

在项目pom.xml文件中引入ssh上传jar包

<!--配置wagon-ssh连接服务器 ssh 依赖包-->
 <groupId>org.apache.maven.wagon</groupId>
 <artifactId>wagon-ssh</artifactId>
 <version>2.8</version>

引入wagon-maven-plugin插件

<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0</version>

详细配置

<properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--公用jar-->
        <jsch.version>0.1.54</jsch.version>
        <log-elk.version>1.6</log-elk.version>
        <javamelody.version>1.72.0</javamelody.version>
        <itext.version>2.1.7</itext.version>

        <!--wagon plugin 配置-->
        <service-path>/home/daison</service-path>
        <pack-name>${project.artifactId}-${project.version}.jar</pack-name>
        <remote-addr>192.168.1.110:22</remote-addr>
        <remote-username>root</remote-username>
        <remote-passwd>123456</remote-passwd>
<!--
        <remote-addr>192.168.1.150:22</remote-addr>
        <remote-username>root</remote-username>
        <remote-passwd>123456</remote-passwd>
-->
    </properties>


<build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <extensions>
            <extension>
                <!--配置wagon-ssh连接服务器 ssh 依赖包-->
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.8</version>
            </extension>
        </extensions>
        <!--maven打包插件-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <!-- 跳过单元测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!--上传插件-->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>wagon-maven-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <!-- serverId即在settings.xml中配置的service的id -->
                   <!-- <serverId>linux-server-dev</serverId>-->
                    <!-- 需要部署的文件 -->
                    <fromFile>target/${pack-name}</fromFile>
                    <!-- 部署目录  用户:密码@ip+部署地址:端口 -->
                    <url><![CDATA[ scp://${remote-username}:${remote-passwd}@${remote-addr}${service-path} ]]></url>
                    <!--shell 执行脚本 -->
                   <!-- <commands>
                        <!– 停止服务–>
                        <command>sh /home/daison/admin/script/beacon-server.sh stop</command>
                        <!– 启动服务 –>
                        <command>sh /home/daison/admin/script/beacon-server.sh start</command>
                    </commands>-->
                    <commands>
                        <!-- Kill Old Process -->
                        <command>kill -9 `ps -ef |grep ${project.build.finalName}.jar|grep -v "grep" |awk '{print $2}'`</command>
                        <!-- Restart jar package,write result into renren.log -->
                        <command>
                            <![CDATA[nohup java -jar ${service-path}/${pack-name} --spring.profiles.active=prod > ${service-path}/server.log 2>&1 & ]]></command>
                        <command><![CDATA[netstat -nptl]]></command>
                        <command><![CDATA[ps -ef | grep java | grep -v grep]]></command>
                    </commands>
                    <!-- 运行命令 mvn clean package wagon:upload-single wagon:sshexec-->
                    <!-- 显示运行命令的输出结果 -->
                    <displayCommandOutputs>true</displayCommandOutputs>
                </configuration>
            </plugin>
            <!--docker 打包插件-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>0.4.14</version>
                <!--<executions>-->
                <!--<execution>-->
                <!--<phase>package</phase>-->
                <!--<goals>-->
                <!--<goal>build</goal>-->
                <!--</goals>-->
                <!--</execution>-->
                <!--</executions>-->
                <configuration>
                    <imageName>beacon/image</imageName>
                    <dockerDirectory>${project.basedir}</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
                <!-- 运行命令 mvn clean package docker:build 打包并生成docker镜像 -->
            </plugin>
        </plugins>


        <!-- 资源目录 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <!-- 资源根目录排除各环境的配置,防止在生成目录中多余其它目录 -->
            </resource>
            <!--激活指定文件-->
            <!--<resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application-${profileActive}.properties</include>
                    <include>application.properties</include>
                    <include>bootstrap-${profileActive}.yml</include>
                    <include>bootstrap.yml</include>
                </includes>
            </resource>-->
            <!--打包java目录-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>

远程启动服务脚本

登录远程服务器,编写脚本,我这里将脚本放在/home/daison/admin目录下

vim beacon-server.sh
#!/bin/sh
#chkconfig: 2345 80 05
#description: admin-server

#jdk
export JAVA_HOME=/usr/local/jdk1.8.0_192
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
source /etc/profile

#根目录
DIR=/home/daison/admin

#启动类
SERVER_NAME=daison-server
JAR_HOME=$DIR/$SERVER_NAME-1.0.1-SNAPSHOT.jar
if [ ! -f "$JAR_HOME" ];then
   echo "$JAR_HOME is not exist!"
   exit 1
fi
echo "start class jar dir: $JAR_HOME"

#日志目录
LOGS_DIR=$DIR/logs
#if [ ! -d ${path} ];then
if [ -d ${LOGS_DIR} ];then
    echo dir ${LOGS_DIR} exist!
else
    mkdir $LOGS_DIR
    chmod 777 -R $LOGS_DIR
fi
#log file 
LOG_NAME=$LOGS_DIR/$SERVER_NAME.log
echo "log file : $LOG_NAME"

#监控服务目录
SKYWALKING_HOME=/home/daison/skywalking/skywalking-6.5
echo "skywalking monitor server dir : $SKYWALKING_HOME"

#java agent jar
JAVA_AGENT=-javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar
echo "javaagent cmd : $JAVA_AGENT"

#service name
SERVICE_NAME=-Dskywalking.agent.service_name=$SERVER_NAME
echo "service name : $SERVICE_NAME"

#backend service
BACKEND_SERVICE=-Dskywalking.collector.backend_service=8.135.42.12:11800
echo "backend service : $BACKEND_SERVICE"


#启动服务
#nohup java -jar $JAR_HOME -javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar -Dskywalking.agent.service_name=daison-websocket -Dskywalking.collector.backend_service=localhost:11800 > $DIR/logs/admin.log 2>&1 &

#nohup java -jar $JAR_HOME $JAVA_AGENT $SERVICE_NAME -Dskywalking.collector.backend_service=localhost:11800 > $LOG_NAME 2>&1 &
#nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar $JAR_HOME > $LOG_NAME 2>&1 &


case "$1" in
	start)
		ps -ef | grep $SERVER_NAME | grep -v grep
		if [ $? -eq 0 ];then
		    echo "server is running......."   
		else
		    echo "start server $SERVER_NAME"
		    nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar -Xms512m -Xmx2048m $JAR_HOME > $LOG_NAME 2>&1 &
		    echo "staring server success !"
		    ps -ef | grep $SERVER_NAME | grep -v grep
		fi
		;;
	stop)
		echo "stop server $SERVER_NAME ......."
		param=`ps -ef | grep $SERVER_NAME | grep -v grep | awk  '{print $2}'`
		if [ "$param" = "" ];then
		   echo "servr $SERVER_NAME not running"
		   exit 1
		fi

		if [ "$param" -gt 0 ];then
		    kill -9 $param
		    echo "stop $SERVER_NAME succes !"
		else
		    echo "stop $SERVER_NAME fail!"
		fi
		;;	
	restart|force-reload)
		${0} stop
		${0} start
		;;
	  *)
		echo "usage: /home/daison/admin/beacon-server.sh {start|stop|restart}" >&2
		exit 1
esac


#查询启动进程 ,结果 0表示存在; $? -ne 0 不存在,$? -eq 0 存在
#ps -ef | grep $SERVER_NAME | grep -v grep
#if [ $? -eq 0 ] 
#then
#    echo "start server success......."
#    ps -ef | grep $SERVER_NAME
#else
#    echo "start server fail......."
#fi

注意:脚本在windows上编写,上传到linux不识别,需要安装插件进行转换,相对麻烦,所以我这里都是直接在linux上编写运行,排除查语法问题方便。

监控服务脚本

监控服务脚本,主要用于检查服务运行状态,如果服务挂了,会自动重启服务,通过linux定时任务进行检查。
检查服务脚本:

vim check-server.sh
#!/bin/sh
#chkconfig: 2345 80 05
#description: check-server

#jdk
#export JAVA_HOME=/usr/local/jdk1.8.0_192
#export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin

#设置格式
:set fileformat=unix

#根目录
DIR=/home/daison/admin/script


#1、检查admin服务
function check_admin(){
	SERVER_NAME=daison-admin-server
	ps -ef | grep '$SERVER_NAME' | grep -v grep
        if [ $? -eq 0 ];then
	   echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
	   sh $DIR/admin-server.sh start
	fi
}


#2、检查beacon-server服务
function check_server(){
	SERVER_NAME=daison-server
        ps -ef | grep '$SERVER_NAME' | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sh $DIR/beacon-server.sh start
        fi
}

#3、检查gateway服务
function check_gateway(){
        SERVER_NAME=daison-gateway
        ps -ef | grep '$SERVER_NAME' | grep -v grep
        if [ $? -eq 0 ];then
           echo "$SERVER_NAME is running......."   
        else
           echo "$SERVER_NAME is stop....."
           sh $DIR/gateway-server.sh start
        fi
}


#检查服务开启
t=$(date "+%Y-%m-%d %H:%M:%S")
echo "check server start ............"
echo "check time: $t"

#while true
#do
cat <<EOF
****************************************
	the flowing is optional
*****************************************
	1) daison-admin
	2) daison-server
	3) daison-gateway
	97)
	    check_admin
        check_server
        check_gateway

	99) 全部服务(1-10)
****************************************
EOF

#read -p "please enter your chioce:" option
case "$1" in
		1)
		   check_admin
		   ;;
		2)
		   check_server
		   ;;
		3)
		   check_gateway
		   ;;
		4)
		   ;;
		97)
		   check_admin
		   check_server
		   check_gateway
		   ;;
		98)
		  ;;
		*)
		   echo "option is inviald."
		   echo "usage: /home/daison/admin/script/check-server.sh {1-99}" >&2
		   exit 1
esac
#done

定时任务脚本

定时任务脚本如下:

vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed


#minute|hour|day-of-month|month|day-of-week|full-path-to-shell-script
# minute: 区间为 0 – 59 
# hour: 区间为0 – 23 
# day-of-month: 区间为0 – 31 
# month: 区间为1 – 12. 1 是1月. 12是12月. 
# Day-of-week: 区间为0 – 7. 周日可以是0或7.
# full-path-to-shell-script: 脚本全路径

#每小时执行一次服务检查命令
* */1 * * * /home/daison/admin/script/check-server.sh 2

编写保存后需要重启定时任务。
crontab /etc/crontab
systemctl restart crond

打包上传

通过命令打包上传

进入到项目根目录中,执行如下命令:
mvn clean package wagon:upload-single

D:\WorkSpace\daison-cloud-new\daison-business\daison-server>
D:\WorkSpace\daison-cloud-new\daison-business\daison-server>
D:\WorkSpace\daison-cloud-new\daison-business\daison-server>mvn clean package wagon:upload-single
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.goo:daison-server >------------------------
[INFO] Building daison-server 1.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ daison-server ---
[INFO] Deleting D:\WorkSpace\daison-cloud-new\daison-business\daison-server\target
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ daison-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 49 resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ daison-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 254 source files to D:\WorkSpace\daison-cloud-new\daison-business\daison-server\target\classes
[INFO] /D:/WorkSpace/daison-cloud-new/daison-business/daison-server/src/main/java/com/goo/mongodb/controller/RadarCollectController.java: 某些输入
文件使用了未经检查或不安全的操作。
[INFO] /D:/WorkSpace/daison-cloud-new/daison-business/daison-server/src/main/java/com/goo/mongodb/controller/RadarCollectController.java: 有关详细
信息, 请使用 -Xlint:unchecked 重新编译。
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ daison-server ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\daison-cloud-new\daison-business\daison-server\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ daison-server ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 21 source files to D:\WorkSpace\daison-cloud-new\daison-business\daison-server\target\test-classes
[INFO] /D:/WorkSpace/daison-cloud-new/daison-business/daison-server/src/test/java/com/goo/tunnel/IntelligenceMonitorServiceTest.java: D:\WorkSpace
\daison-cloud-new\daison-business\daison-server\src\test\java\com\goo\tunnel\IntelligenceMonitorServiceTest.java使用或覆盖了已过时的 API。
[INFO] /D:/WorkSpace/daison-cloud-new/daison-business/daison-server/src/test/java/com/goo/tunnel/IntelligenceMonitorServiceTest.java: 有关详细信息
, 请使用 -Xlint:deprecation 重新编译。
[INFO] /D:/WorkSpace/daison-cloud-new/daison-business/daison-server/src/test/java/com/goo/tunnel/TunnelTest.java: D:\WorkSpace\daison-cloud-new\da
ison-business\daison-server\src\test\java\com\goo\tunnel\TunnelTest.java使用了未经检查或不安全的操作。
[INFO] /D:/WorkSpace/daison-cloud-new/daison-business/daison-server/src/test/java/com/goo/tunnel/TunnelTest.java: 有关详细信息, 请使用 -Xlint:unch
ecked 重新编译。
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ daison-server ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ daison-server ---
[INFO] Building jar: D:\WorkSpace\daison-cloud-new\daison-business\daison-server\target\daison-server-1.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.7.RELEASE:repackage (default) @ daison-server ---
[INFO]
[INFO] -----------------------< com.goo:daison-server >------------------------
[INFO] Building daison-server 1.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- wagon-maven-plugin:1.0:upload-single (default-cli) @ daison-server ---
[INFO] Uploading: D:\WorkSpace\daison-cloud-new\daison-business\daison-server\target\daison-server-1.0.1-SNAPSHOT.jar scp://8.135.19.154:22/home/d
aison/admin/daison-server-1.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  35.556 s
[INFO] Finished at: 2021-01-26T13:56:43+08:00
[INFO] ------------------------------------------------------------------------

D:\WorkSpace\daison-cloud-new\daison-business\daison-server>

通过idea 打包上传

在idea maven页签plugins中找到wagon,选择wagon:upload-single.

srpingboot使用spring cloud consul springboot ssh_jar


执行具体信息如下:

srpingboot使用spring cloud consul springboot ssh_ssh_02

D:\devtools\Java\jdk1.8.0_92\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\WorkSpace\daison-cloud-new\daison-business\daison-server "-Dmaven.home=D:\devtools\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=D:\devtools\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=D:\devtools\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\devtools\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=59710:D:\devtools\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\devtools\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar" org.codehaus.classworlds.Launcher -Didea.version2019.3.3 org.codehaus.mojo:wagon-maven-plugin:1.0:upload-single
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.goo:daison-server >------------------------
[INFO] Building daison-server 1.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- wagon-maven-plugin:1.0:upload-single (default-cli) @ daison-server ---
[INFO] Uploading: D:\WorkSpace\daison-cloud-new\daison-business\daison-server\target\daison-server-1.0.1-SNAPSHOT.jar scp://8.135.19.154:22/home/daison/daison-server-1.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.169 s
[INFO] Finished at: 2021-01-26T11:42:26+08:00
[INFO] ------------------------------------------------------------------------

上传jar包如下:

srpingboot使用spring cloud consul springboot ssh_maven_03

执行启动命令

通过命令行启动

进入到项目根目录,执行命令:mvn wagon:sshexec

D:\WorkSpace\daison-cloud-new\daison-business\daison-server>
D:\WorkSpace\daison-cloud-new\daison-business\daison-server>
D:\WorkSpace\daison-cloud-new\daison-business\daison-server>mvn wagon:sshexec
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.goo:daison-server >------------------------
[INFO] Building daison-server 1.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- wagon-maven-plugin:1.0:sshexec (default-cli) @ daison-server ---
[INFO] sshexec: sh /home/daison/admin/script/beacon-server.sh stop
start class jar dir: /home/daison/admin/daison-server-1.0.1-SNAPSHOT.jar
dir /home/daison/admin/logs exist!
log file : /home/daison/admin/logs/daison-server.log
skywalking monitor server dir : /home/daison/skywalking/skywalking-6.5
javaagent cmd : -javaagent:/home/daison/skywalking/skywalking-6.5/agent/skywalking-agent.jar
service name : -Dskywalking.agent.service_name=daison-server
backend service : -Dskywalking.collector.backend_service=8.135.42.12:11800
stop server daison-server .......
stop daison-server succes !


[INFO] sshexec: source /etc/profile; sh /home/daison/admin/script/beacon-server.sh start
start class jar dir: /home/daison/admin/daison-server-1.0.1-SNAPSHOT.jar
dir /home/daison/admin/logs exist!
log file : /home/daison/admin/logs/daison-server.log
skywalking monitor server dir : /home/daison/skywalking/skywalking-6.5
javaagent cmd : -javaagent:/home/daison/skywalking/skywalking-6.5/agent/skywalking-agent.jar
service name : -Dskywalking.agent.service_name=daison-server
backend service : -Dskywalking.collector.backend_service=8.135.42.12:11800
start server daison-server
staring server success !

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.914 s
[INFO] Finished at: 2021-01-26T15:24:03+08:00
[INFO] ------------------------------------------------------------------------

D:\WorkSpace\daison-cloud-new\daison-business\daison-server>

登陆服务器查看:

srpingboot使用spring cloud consul springboot ssh_ssh_04


以上表示服务已经启动。

通过idea执行maven插件启动服务

打开idea,在maven页面plugins中,找到wagon, 选择wagon:sshexec 执行。

srpingboot使用spring cloud consul springboot ssh_java_05


执行结果如下:

srpingboot使用spring cloud consul springboot ssh_java_06


具体详细信息:

D:\WorkSpace\daison-cloud-new\daison-business\daison-server>
D:\WorkSpace\daison-cloud-new\daison-business\daison-server>mvn wagon:sshexec
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.goo:daison-server >------------------------
[INFO] Building daison-server 1.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- wagon-maven-plugin:1.0:sshexec (default-cli) @ daison-server ---
[INFO] sshexec: sh /home/daison/admin/script/beacon-server.sh stop
start class jar dir: /home/daison/admin/daison-server-1.0.1-SNAPSHOT.jar
dir /home/daison/admin/logs exist!
log file : /home/daison/admin/logs/daison-server.log
skywalking monitor server dir : /home/daison/skywalking/skywalking-6.5
javaagent cmd : -javaagent:/home/daison/skywalking/skywalking-6.5/agent/skywalking-agent.jar
service name : -Dskywalking.agent.service_name=daison-server
backend service : -Dskywalking.collector.backend_service=8.135.42.12:11800
stop server daison-server .......
stop daison-server succes !


[INFO] sshexec: source /etc/profile; sh /home/daison/admin/script/beacon-server.sh start
start class jar dir: /home/daison/admin/daison-server-1.0.1-SNAPSHOT.jar
dir /home/daison/admin/logs exist!
log file : /home/daison/admin/logs/daison-server.log
skywalking monitor server dir : /home/daison/skywalking/skywalking-6.5
javaagent cmd : -javaagent:/home/daison/skywalking/skywalking-6.5/agent/skywalking-agent.jar
service name : -Dskywalking.agent.service_name=daison-server
backend service : -Dskywalking.collector.backend_service=8.135.42.12:11800
start server daison-server
staring server success !


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.914 s
[INFO] Finished at: 2021-01-26T15:24:03+08:00
[INFO] ------------------------------------------------------------------------

D:\WorkSpace\daison-cloud-new\daison-business\daison-server>

参考文档:

打包错误

执行上传命令错误

Failure to find org.eclipse.m2e:lifecycle-mapping:pom:1.0.0 in http://maven.aliyun.com/nexus/content/groups/public/

解决办法:

  1. 下载 :https://github.com/BINGOcoder1998/dummy-lifecycle-mapping-plugin.git ,随便找个合适的目录,右击 git bash here 执行

git clone https://github.com/BINGOcoder1998/dummy-lifecycle-mapping-plugin.git

2.进入到dummy-lifecycle-mapping-plugin目录下执行:maven 命令安装到本地仓库:
mvn clean install

执行shell脚本错误integer expression expected

Shell命令之“integer expression expected”

该问题主要是比较时字符串和数字之间不能直接比较导致,将脚本修改,先判断是否为空,然后再执行判断比较即可。

srpingboot使用spring cloud consul springboot ssh_maven_07

执行脚本错误nohup: failed to run command ‘java’: No such file or directory

阿里云服务执行脚本,远程shell登陆执行java -jar xxx.jar 没问题,检查了jdk配置无任何问题,远程服务器执行命令也没问题,但是idea中通过插件wagon-maven-plugin 执行找不到java命令。

查询了一些网络资料,大家都使用/etc/profile进行远程命令执行可行,通过测试,该种方式无效。
根据错误分析,突然灵机一动,错误不是说找不到java命令吗, 那我就把jdk导入到shell命令中不就可以了。
shell脚本修改如下:

#!/bin/sh
#chkconfig: 2345 80 05
#description: admin-server

#导入jdk库
export JAVA_HOME=/usr/local/jdk1.8.0_192
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
source /etc/profile

#根目录
DIR=/home/daison/admin

#启动类
SERVER_NAME=daison-server
JAR_HOME=$DIR/$SERVER_NAME-1.0.1-SNAPSHOT.jar
if [ ! -f "$JAR_HOME" ];then
   echo "$JAR_HOME is not exist!"
   exit 1
fi
echo "start class jar dir: $JAR_HOME"

#日志目录
LOGS_DIR=$DIR/logs
#if [ ! -d ${path} ];then
if [ -d ${LOGS_DIR} ];then
    echo dir ${LOGS_DIR} exist!
else
    mkdir $LOGS_DIR
    chmod 777 -R $LOGS_DIR
fi
#log file 
LOG_NAME=$LOGS_DIR/$SERVER_NAME.log
echo "log file : $LOG_NAME"

#监控服务目录
SKYWALKING_HOME=/home/daison/skywalking/skywalking-6.5
echo "skywalking monitor server dir : $SKYWALKING_HOME"

#java agent jar
JAVA_AGENT=-javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar
echo "javaagent cmd : $JAVA_AGENT"

#service name
SERVICE_NAME=-Dskywalking.agent.service_name=$SERVER_NAME
echo "service name : $SERVICE_NAME"

#backend service
BACKEND_SERVICE=-Dskywalking.collector.backend_service=8.135.42.12:11800
echo "backend service : $BACKEND_SERVICE"


#启动服务
#nohup java -jar $JAR_HOME -javaagent:$SKYWALKING_HOME/agent/skywalking-agent.jar -Dskywalking.agent.service_name=daison-websocket -Dskywalking.collector.backend_service=localhost:11800 > $DIR/logs/admin.log 2>&1 &

#nohup java -jar $JAR_HOME $JAVA_AGENT $SERVICE_NAME -Dskywalking.collector.backend_service=localhost:11800 > $LOG_NAME 2>&1 &
#nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar $JAR_HOME > $LOG_NAME 2>&1 &


case "$1" in
	start)
		ps -ef | grep $SERVER_NAME | grep -v grep
		if [ $? -eq 0 ];then
		    echo "server is running......."   
		else
		    echo "start server $SERVER_NAME"
		    nohup java $JAVA_AGENT $SERVICE_NAME $BACKEND_SERVICE -jar -Xms512m -Xmx2048m $JAR_HOME > $LOG_NAME 2>&1 &
		    echo "staring server success !"
		    ps -ef | grep $SERVER_NAME | grep -v grep
		fi
		;;
	stop)
		echo "stop server $SERVER_NAME ......."
		param=`ps -ef | grep $SERVER_NAME | grep -v grep | awk  '{print $2}'`
		if [ "$param" = "" ];then
		   echo "servr $SERVER_NAME not running"
		   exit 1
		fi

		if [ "$param" -gt 0 ];then
		    kill -9 $param
		    echo "stop $SERVER_NAME succes !"
		else
		    echo "stop $SERVER_NAME fail!"
		fi
		;;	
	restart|force-reload)
		${0} stop
		${0} start
		;;
	  *)
		echo "usage: /home/daison/admin/beacon-server.sh {start|stop|restart}" >&2
		exit 1
esac


#查询启动进程 ,结果 0表示存在; $? -ne 0 不存在,$? -eq 0 存在
#ps -ef | grep $SERVER_NAME | grep -v grep
#if [ $? -eq 0 ] 
#then
#    echo "start server success......."
#    ps -ef | grep $SERVER_NAME
#else
#    echo "start server fail......."
#fi

然后通过idea插件执行,启动服务成功。

远程启动不能链接服务器Failed to execute goal org.codehaus.mojo:wagon-maven-plugin:1.0:upload-single (default-cli) on project daison-server: Unable to create a Wagon instance for scp://root:123456@8.135.42.12:22/home/daison/admin: Cannot connect. Reason: SSH_MSG_DISCONNECT: 2 Too many authentication failures -> [Help 1]

1、检查配置用户名、密码、ip地址是否正确
2、该服务器通过远程是否有权限可以登录
3、登录用户账号、密码不能包含@符号
本人就是由于登录用户密码中包含@符号导致登录异常,因为远程登录拷贝jar上传时命令 scp://user:password@ip:port/目录 中是通过@区分的,所以登录失败。
4、检查登录用户是否有权限执行操作命令,例如创建文件,执行启动服务命令等等。