dubbo+zookeeper+jenkins从打包开始
一.jenkins中maven构建
有如下图构建设置
解释说明:
pom.xml
此处jenkins打包依赖的主要配置文件(规则)-- 项目根目录下的pom.xml文件。
注:如果该文件中没有对应的打包参数(qa、dev、product等),那就应该在该项目依赖的公共项目的pom.xml中
settings.xml
全局配置文件,主要用于配置maven的运行环境等一系列通用的属性
clean package -Pqa -DskipTests=true
maven 打包命令
上述命令解释:清class文件,打包构建,传参‘qa’打包( -Pqa ,此处的qa为配置文件中设置的测试环境参数,-P选项 maven 会激活项目下的pom.xml配置的<profiles>标签下id为qa,如下图为公共依赖项目的pom.xml文件配置),以及最后的跳过测试。
pom.xml文件,目录关系如下,vancloud-cloud-parent为公共配置目录,配置有多环境参数变量。
此项目打包时通过自己的pom.xml调用parent的pom.xml文件达到多环境打包目的(更简单的多环境传参在本项目的 pom.xml中配置即可,因为用到多环境配置文件的项目不止一个,所以创建一个公共配置文件)
公共配置文件pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.danny.vancloud</groupId> <artifactId>vancloud-cloud-dependencies</artifactId> <version>1.2.0-SNAPSHOT</version> </parent> <artifactId>vancloud-cloud-parent</artifactId> <version>1.2.0-SNAPSHOT</version> <name>vancloud-parent</name> <url>http://maven.apache.org</url> <packaging>pom</packaging> <properties> <main.basedir>..</main.basedir> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <scm> <connection>scm:svn:https://192.168.1.244/svn/vancloud/tags/server/打包发布/vancloud-build-all-1.2.0/server/公共依赖</connection> <developerConnection>scm:svn:https://192.168.1.244/svn/vancloud/tags/server/打包发布/vancloud-build-all-1.2.0/server/公共依赖</developerConnection> </scm> <profiles> <profile> <id>dev</id> <properties> <env>dev</env> <zookeeper.address>localhost:2181</zookeeper.address> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>qa</id> <properties> <env>qa</env> <zookeeper.address>192.168.3.243:2181,192.168.3.152:2181,192.168.3.244:2181</zookeeper.address> </properties> </profile> <profile> <id>uat</id> <properties> <env>uat</env> <zookeeper.address>192.168.3.162:2181,192.168.3.161:2181,192.168.3.158:2181</zookeeper.address> </properties> </profile> <profile> <id>product</id> <properties> <env>product</env> <zookeeper.address>10.51.183.161:2181,10.51.181.181:2181,10.51.180.146:2181</zookeeper.address> </properties> </profile> </profiles> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <tagBase>https://192.168.1.244/svn/vancloud/tags/server/公共依赖/</tagBase> </configuration> </plugin> </plugins> </build> </project>
本项目(oa)配置文件pom.xml
<parent> <groupId>com.danny.vancloud</groupId> <artifactId>vancloud-cloud-parent</artifactId> <version>1.2.0-SNAPSHOT</version> <relativePath>../vancloud-cloud-parent</relativePath> //通过groupID调用相对路径下的配置文件pom.xml </parent>
//其他配置略...
解释:jenkins通过maven打包时,通过-P传入参数qa,maven通过pom.xml打包并调用公共配置得到qa环境zookeeper.address值,并将返回值打入相应war或tar.gz包文件中。
此时可对比看下
dubbo的web端
对于consumer-ems-beans.xml文件
打包前:
[root@Jenkins3 resources]# pwd /data/jenkins/workspace/QA-vancloud-oa-web/src/main/resources [root@Jenkins3 resources]# ls consumer-ems-beans.xml jetty-contexts.xml spring-redis.xml spring-web-beans.xml dubbo.properties logback.xml spring-vancloud.xml vancloud-shiro.xml
[root@Jenkins3 resources]# vim consumer-ems-beans.xml
打包后:
[root@develop3 classes]# pwd /opt/source/vancloud-oa-web/WEB-INF/classes [root@develop3 classes]# ls com dubbo.properties logback.xml spring-vancloud.xml vancloud-shiro.xml consumer-ems-beans.xml jetty-contexts.xml spring-redis.xml spring-web-beans.xml [root@develop3 classes]# vim consumer-ems-beans.xml
dubbo的service端
对于dubbo.properties
打包前:
[root@Jenkins3 resources]# pwd /data/jenkins/workspace/QA-vancloud-oa-service/src/main/resources [root@Jenkins3 resources]# ls dao dubbo.properties ehcache.xml imagesettings.properties logback.xml META-INF [root@Jenkins3 resources]# vim dubbo.properties
打包后:
[root@develop3 conf]# pwd /opt/service/vancloud-oa-service/conf [root@develop3 conf]# ls dubbo.properties [root@develop3 conf]# vim dubbo.properties
可以看出都能成功获取到相应环境下的变量值
二.dubbo+zookeeper+spring整合介绍
dubbo采用全spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用spring加载dubbo的配置即可,dubbo基于spring的schema扩展进行加载。
1.zookeeper安装 略
2.service端启动,向zookeeper上注册URL对象,注册信息可见spring目录下文件
3.web端启动,向zookeeper获取对象
示例图:
其中service端启动脚本(官方脚本)如下:略有修改
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/conf SERVER_NAME=`sed '/dubbo.application.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'` SERVER_PROTOCOL=`sed '/dubbo.protocol.name/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'` SERVER_PORT=`sed '/dubbo.protocol.port/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'` LOGS_FILE=`sed '/dubbo.log4j.file/!d;s/.*=//' conf/dubbo.properties | tr -d '\r'` if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`hostname` fi 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 if [ -n "$SERVER_PORT" ]; then SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PORT | wc -l` if [ $SERVER_PORT_COUNT -gt 0 ]; then echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!" LOGS_DIR="" if [ -n "$LOGS_FILE" ]; then LOGS_DIR=`dirname $LOGS_FILE` else if [ ! -d $LOGS_DIR ]; then mkdir $LOGS_DIR fi STDOUT_FILE=$LOGS_DIR/stdout.$(date +%Y-%m-%d).log LIB_DIR=$DEPLOY_DIR/lib LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"` JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=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_MEM_OPTS="" BITS=`java -version 2>&1 | grep -i 64-bit` if [ -n "$BITS" ]; then JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -DenableProxy=true -DproxyIp=10.51.183.234 -DproxyPort=8888" else JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC " fi echo -e "Starting the $SERVER_NAME ...\c" nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -Ddubbo.properties.file=dubbo.properties -classpath $CONF_DIR:$LIB_JARS com.alibaba.dubbo.container.Main 2>&1 | /usr/sbin/cronolog $LOGS_DIR/stdout.%Y-%m-%d.log > /dev/null 2>&1 & echo "OK!" PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'` echo "PID: $PIDS" echo "STDOUT: $STDOUT_FILE"
web端启动脚本如下:
#!/bin/sh cd `dirname $0` BIN_DIR=`pwd` cd .. # 设置项目代码路径 CODE_HOME=`pwd` zookeeper_address='10.51.183.161:2181,10.51.181.181:2181,10.51.180.146:2181' IPADD=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"` #日志路径 export LOG_PATH=$CODE_HOME/logs mkdir -p $LOG_PATH # 设置依赖路径 export CLASSPATH="$CODE_HOME/WEB-INF/classes:$CODE_HOME/WEB-INF/lib/*" # JVM启动参数 export JAVA_OPTS="-server -Xms128m -Xmx1g -Xss256k -XX:MaxDirectMemorySize=128m" #JMX export JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=17083 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=$IPADD" # 服务端端口、上下文、项目根配置 export SERVER_INFO="-Dserver.port=7083 -Dserver.contextPath=/ -Dserver.docBase=$CODE_HOME -Ddubbo.registry.address=$zookeeper_address -Dzookeeper.address=$zookeeper_address -DenableProxy=true -DproxyIp=10.51.183.234 -DproxyPort=8888" # 启动类 export MAIN_CLASS=com.vgtech.vancloud.web.startup.TomcatBootstrap #判断服务是否运行 PIDS=`ps -ef | grep java | grep "server.port=7083" |awk '{print $2}'` if [ -n "$PIDS" ]; then echo "ERROR: The Service already started!" echo "PID: $PIDS" exit 1 fi #启动服务 nohup java $JMX $JAVA_OPTS -classpath $CLASSPATH $SERVER_INFO $MAIN_CLASS 2>&1 | /usr/sbin/cronolog $LOG_PATH/stdout.%Y-%m-%d.log > /dev/null 2>&1 & #判断服务是否启动成功 echo "Service startup success" PIDS=`ps -ef | grep java | grep "server.port=7083" | awk '{print $2}'` echo "PID: $PIDS" echo "STDOUT: $LOG_PATH/stdout.$(date +%Y-%m-%d).log"