一:、好言
偶尔对自己好些,偷个小懒,抽点小疯,花点小钱,不算伤天害理。
二、内容
2.1:在看dubbo的demo的时候,我们会发现在打包的时候用到assembly插件来做操作,并且会把相应的命令打入,解压后可以直接运行sh来启动dubbo.所以想自己尝试配置然后跑一跑试试。
2.2:配置
img_1.png先看看项目简单的模块。
只说配置吧,先看看provider的配置
资源放在src/main/resources/META-INF/spring/*下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<bean id="userService" class="com.mouse.moon.provide.UserServiceImpl"></bean>
<dubbo:service ref="userService" interface="com.mouse.moon.facade.UserFacade"></dubbo:service>
</beans>
图片.png
assembly.xml配置:
<assembly>
<id>assembly</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/resources/META-INF/spring/bin</directory>
<outputDirectory>/bin</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/assembly/conf</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
其实我们看一搬配置里面会加个熟悉
<fileMode>0755</fileMode>
我的现在没配置的原因是因为我配置这个之后,打包解压后的start.sh是不可执行的,然后去掉之后是好的,个人认为可能是跟电脑权限有关,我的mac下配置是需要去掉该属性。
pom文件配置:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>make-zip</id>
<!-- 绑定到package生命周期阶段上 -->
<phase>package</phase>
<goals>
<!-- 绑定到package生命周期阶段上 -->
<goal>single</goal>
</goals>
<configuration>
<descriptors> <!--描述文件路径-->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
打完包可以看到生存一个*.tar.gz的包
图片.png
解压后可以看到三个文件
图片.png进入bin目录,执行start.sh
图片.png遇到问题:这里我启动的时候本身是报异常的
ERROR: The demo-provide already started!
这个看了看start.sh里面的写法如下:
PIDS=`ps -f | grep java | grep "$CONF_DIR" |awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "PID: $PIDS"
exit 1
fi
这里竟然是按照路径来查找进程是否存在的,这个你随便写个不存在的路径也能查找到pid,这个pid就是本身这次查找的。
然后stop:
我的这个项目遇到这个问题后,我把项目打包给朋友测试,在他的电脑就可以成功,所以我很纳闷。最后才发现是我的终端问题,这里竟然会出现终端这个ps自己的进程
图片.png后面我在网上找了一个启动程序,然后用这个命令竟然是可以启动的
这个参考文章
#!/bin/bash
cd `dirname $0`
#当前路径
BIN_DIR=`pwd`
#向上一层路径
cd ..
DEPLOY_DIR=`pwd`
echo $DEPLOY_DIR
#配置文件路径
CONF_DIR=$DEPLOY_DIR/conf
#日志输出路径
LOGS_DIR=$DEPLOY_DIR/logs
# 如果JDK环境变量没有写到全局要添加如下几行
# JAVA_HOME=/opt/java/jdk1.6.0_45
# PATH=$JAVA_HOME/bin:$PATH
# export JAVA_HOME
# export PATH
#从dubbo.properties取得应用名、端口号,端口名
SERVER_NAME=`sed '/dubbo.application.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
SERVER_PROTOCOL_NAME=`sed '/dubbo.protocol.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
SERVER_PROTOCOL_PORT=`sed '/dubbo.protocol.port/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
#应用名为空的话就取当前系统名
if [ -z "$SERVER_NAME" ]; then
echo "SERVER_NAME is empty"
SERVER_NAME=`hostname`
fi
#根据配置文件路径去查找当前是否已有dubbo应用启动起来
APP_PID=`ps -ef -ww | grep "java" | grep " -DappName=$SERVER_NAME " | awk '{print $2}'`
echo "SERVER_NAME: $SERVER_NAME"
echo "SERVER_PROTOCOL_NAME: $SERVER_PROTOCOL_NAME"
echo "SERVER_PROTOCOL_PORT: $SERVER_PROTOCOL_PORT"
echo "APP_PID: $APP_PID"
#APP_PID不为空,说明应用已启动,直接退出
if [ -n "$APP_PID" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "PID: $APP_PID"
exit 1
fi
#检查端口是否被占用
if [ -n "$SERVER_PROTOCOL_PORT" ]; then
SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PROTOCOL_PORT | wc -l`
if [ $SERVER_PORT_COUNT -gt 0 ]; then
echo "ERROR: The $SERVER_NAME port $SERVER_PROTOCOL_PORT already used!"
exit 1
fi
fi
#如果logs目录不存在,就创建一个
if [ ! -d $LOGS_DIR ]; then
mkdir $LOGS_DIR
fi
echo "LOGS_DIR :$LOGS_DIR"
#控制台日志输出收集位置
STDOUT_FILE=$LOGS_DIR/stdout.log
#依赖jar包目录
LIB_DIR=$DEPLOY_DIR/lib
#将上面的jar文件名称,拼接上lib的路径然后输出
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`
#-DappName指定应用名
JAVA_OPTS="-DappName=$SERVER_NAME -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true"
#调试模式
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
#首先将java版本号信息输出到标准输出,然后查找’64-bit’信息,目的就是判断jdk版本是否为64位
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
#JVM启动基本参数,这里根据应用自行调整
JAVA_MEM_SIZE_OPTS="-Xmx768m -Xms378m -Xmn256m -XX:PermSize=64m -XX:MaxPermSize=256M -Xss256k"
#根据32位和64位配置不同的启动java垃圾回收参数,根据应用自行调整
if [ -n "$BITS" ]; then
JAVA_MEM_OPTS=" -server $JAVA_MEM_SIZE_OPTS -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
JAVA_MEM_OPTS=" -server $JAVA_MEM_SIZE_OPTS -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
echo -e "Starting the $SERVER_NAME ...\c"
echo "启动参数:java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $JAVA_PROPERTIES_OPTS -classpath $CONF_DIR:$LIB_JARS com.alibaba.dubbo.container.Main"
#通过java命令启动服务,同时将其作为后台任务执行。
nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS com.alibaba.dubbo.container.Main > $STDOUT_FILE 2>&1 &
#睡眠一下再检查应用是否启动,下面这里注释打开的话,就把最下面的那一段注释掉
#sleep 1
#APP_PID=`ps -f | grep java | grep "$CONF_DIR" |awk '{print $2}'`
#if [ -z "$APP_PID" ]; then
#echo "START APP FAIL!"
#echo "STDOUT: $STDOUT_FILE"
#exit 1
#fi
#echo "START SUCCESSED APP_PID: $APP_PID"
#echo "STDOUT: $STDOUT_FILE"
#grep -c 阻止正常的结果输出,转而输出匹配的结果数量,这里就是输出OK的个数。
COUNT=0
while [ $COUNT -lt 1 ]; do
echo -e ".\c"
sleep 1
if [ -n "$SERVER_PROTOCOL_PORT" ]; then
if [ "$SERVER_PROTOCOL_NAME" == "dubbo" ]; then
COUNT=`echo status | nc -i 1 127.0.0.1 $SERVER_PROTOCOL_PORT | grep -c OK`
else
COUNT=`netstat -an | grep $SERVER_PROTOCOL_PORT | wc -l`
fi
else
COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`
fi
if [ $COUNT -gt 0 ]; then
break
fi
done
echo "OK!"
APP_PID=`ps -ef -ww | grep "java" | grep " -DappName=$SERVER_NAME " | awk '{print $2}'`
echo "START SUCCESSED APP_PID: $APP_PID"
echo "STDOUT: $STDOUT_FILE"
停止脚本:
#!/bin/bash
cd `dirname $0`
#当前路径
BIN_DIR=`pwd`
#向上一层路径
cd ..
DEPLOY_DIR=`pwd`
echo $DEPLOY_DIR
#配置文件路径
CONF_DIR=$DEPLOY_DIR/conf
#日志输出路径
LOGS_DIR=$DEPLOY_DIR/logs
# 如果JDK环境变量没有写到全局要添加如下几行
# JAVA_HOME=/opt/java/jdk1.6.0_45
# PATH=$JAVA_HOME/bin:$PATH
# export JAVA_HOME
# export PATH
#从dubbo.properties取得应用名、端口号,端口名
SERVER_NAME=`sed '/dubbo.application.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
SERVER_PROTOCOL_NAME=`sed '/dubbo.protocol.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
SERVER_PROTOCOL_PORT=`sed '/dubbo.protocol.port/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
#应用名为空的话就取当前系统名
if [ -z "$SERVER_NAME" ]; then
echo "SERVER_NAME is empty"
SERVER_NAME=`hostname`
fi
#根据配置文件路径去查找当前是否已有dubbo应用启动起来
APP_PID=`ps -ef -ww | grep "java" | grep " -DappName=$SERVER_NAME " | awk '{print $2}'`
echo "SERVER_NAME: $SERVER_NAME"
echo "SERVER_PROTOCOL_NAME: $SERVER_PROTOCOL_NAME"
echo "SERVER_PROTOCOL_PORT: $SERVER_PROTOCOL_PORT"
echo "APP_PID: $APP_PID"
#APP_PID不为空,说明应用已启动,直接退出
if [ -n "$APP_PID" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "PID: $APP_PID"
exit 1
fi
#检查端口是否被占用
if [ -n "$SERVER_PROTOCOL_PORT" ]; then
SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PROTOCOL_PORT | wc -l`
if [ $SERVER_PORT_COUNT -gt 0 ]; then
echo "ERROR: The $SERVER_NAME port $SERVER_PROTOCOL_PORT already used!"
exit 1
fi
fi
#如果logs目录不存在,就创建一个
if [ ! -d $LOGS_DIR ]; then
mkdir $LOGS_DIR
fi
echo "LOGS_DIR :$LOGS_DIR"
#控制台日志输出收集位置
STDOUT_FILE=$LOGS_DIR/stdout.log
#依赖jar包目录
LIB_DIR=$DEPLOY_DIR/lib
#将上面的jar文件名称,拼接上lib的路径然后输出
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`
#-DappName指定应用名
JAVA_OPTS="-DappName=$SERVER_NAME -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Ddubbo.shutdown.hook=true"
#调试模式
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
#首先将java版本号信息输出到标准输出,然后查找’64-bit’信息,目的就是判断jdk版本是否为64位
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
#JVM启动基本参数,这里根据应用自行调整
JAVA_MEM_SIZE_OPTS="-Xmx768m -Xms378m -Xmn256m -XX:PermSize=64m -XX:MaxPermSize=256M -Xss256k"
#根据32位和64位配置不同的启动java垃圾回收参数,根据应用自行调整
if [ -n "$BITS" ]; then
JAVA_MEM_OPTS=" -server $JAVA_MEM_SIZE_OPTS -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
JAVA_MEM_OPTS=" -server $JAVA_MEM_SIZE_OPTS -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
echo -e "Starting the $SERVER_NAME ...\c"
echo "启动参数:java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $JAVA_PROPERTIES_OPTS -classpath $CONF_DIR:$LIB_JARS com.alibaba.dubbo.container.Main"
#通过java命令启动服务,同时将其作为后台任务执行。
nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS com.alibaba.dubbo.container.Main > $STDOUT_FILE 2>&1 &
#睡眠一下再检查应用是否启动,下面这里注释打开的话,就把最下面的那一段注释掉
#sleep 1
#APP_PID=`ps -f | grep java | grep "$CONF_DIR" |awk '{print $2}'`
#if [ -z "$APP_PID" ]; then
#echo "START APP FAIL!"
#echo "STDOUT: $STDOUT_FILE"
#exit 1
#fi
#echo "START SUCCESSED APP_PID: $APP_PID"
#echo "STDOUT: $STDOUT_FILE"
#grep -c 阻止正常的结果输出,转而输出匹配的结果数量,这里就是输出OK的个数。
COUNT=0
while [ $COUNT -lt 1 ]; do
echo -e ".\c"
sleep 1
if [ -n "$SERVER_PROTOCOL_PORT" ]; then
if [ "$SERVER_PROTOCOL_NAME" == "dubbo" ]; then
COUNT=`echo status | nc -i 1 127.0.0.1 $SERVER_PROTOCOL_PORT | grep -c OK`
else
COUNT=`netstat -an | grep $SERVER_PROTOCOL_PORT | wc -l`
fi
else
COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`
fi
if [ $COUNT -gt 0 ]; then
break
fi
done
echo "OK!"
APP_PID=`ps -ef -ww | grep "java" | grep " -DappName=$SERVER_NAME " | awk '{print $2}'`
echo "START SUCCESSED APP_PID: $APP_PID"
echo "STDOUT: $STDOUT_FILE"
sh stop.sh dump
#!/bin/bash
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf
# 如果JDK环境变量没有写到全局要添加如下几行
# JAVA_HOME=/opt/java/jdk1.6.0_45
# PATH=$JAVA_HOME/bin:$PATH
# export JAVA_HOME
# export PATH
SERVER_NAME=`sed '/^app.process.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'`
if [ -z "$SERVER_NAME" ]; then
SERVER_NAME=`hostname`
fi
PIDS=`ps -ef -ww | grep "java" | grep " -DappName=$SERVER_NAME " | awk '{print $2}'`
if [ -z "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME does not started!"
exit 1
fi
LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d "$LOGS_DIR" ]; then
mkdir -p "$LOGS_DIR"
fi
DUMP_DIR=$LOGS_DIR/dump
if [ ! -d $DUMP_DIR ]; then
mkdir $DUMP_DIR
fi
DUMP_DATE=`date +%Y%m%d%H%M%S`
DATE_DIR=$DUMP_DIR/$DUMP_DATE
if [ ! -d $DATE_DIR ]; then
mkdir $DATE_DIR
fi
echo -e "Dumping the $SERVER_NAME ...\c"
for PID in $PIDS ; do
jstack $PID > $DATE_DIR/jstack-$PID.dump 2>&1
echo -e ".\c"
jinfo $PID > $DATE_DIR/jinfo-$PID.dump 2>&1
echo -e ".\c"
jstat -gcutil $PID > $DATE_DIR/jstat-gcutil-$PID.dump 2>&1
echo -e ".\c"
jstat -gccapacity $PID > $DATE_DIR/jstat-gccapacity-$PID.dump 2>&1
echo -e ".\c"
jmap $PID > $DATE_DIR/jmap-$PID.dump 2>&1
echo -e ".\c"
jmap -heap $PID > $DATE_DIR/jmap-heap-$PID.dump 2>&1
echo -e ".\c"
jmap -histo $PID > $DATE_DIR/jmap-histo-$PID.dump 2>&1
echo -e ".\c"
if [ -r /usr/sbin/lsof ]; then
/usr/sbin/lsof -p $PID > $DATE_DIR/lsof-$PID.dump
echo -e ".\c"
fi
done
if [ -r /bin/netstat ]; then
/bin/netstat -an > $DATE_DIR/netstat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/iostat ]; then
/usr/bin/iostat > $DATE_DIR/iostat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/mpstat ]; then
/usr/bin/mpstat > $DATE_DIR/mpstat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/vmstat ]; then
/usr/bin/vmstat > $DATE_DIR/vmstat.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/free ]; then
/usr/bin/free -t > $DATE_DIR/free.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/sar ]; then
/usr/bin/sar > $DATE_DIR/sar.dump 2>&1
echo -e ".\c"
fi
if [ -r /usr/bin/uptime ]; then
/usr/bin/uptime > $DATE_DIR/uptime.dump 2>&1
echo -e ".\c"
fi
echo "OK!"
echo "DUMP: $DATE_DI
网友评论