Tomcat+Dubbo安裝
1.將tomcat的webapps目錄下的所有文件清空,講Dubbo管理控制台的程序dubbo-admin-2.5.3.war放
到webapps中,並且解壓命名為ROOT。
unzip dubbo.war -d ROOT
2.配置dubbo.properties
將以下地址改為你Zookeeper注冊的地址。前提:已經安裝過Zookeeper。
dubbo.registry.address=zookeeper://192.168.137.128:2181
3.啟動tomcat
瀏覽:http://192.168.137.128:8080/出現以下界面
PS:JDK最好使用1.7版本,因為dubbo使用的Spring版為2.X 具體原因也說不清楚,我最早部署時也是使用JDK1.8 啟動tomcat時報錯:
RROR context.ContextLoader - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'uriBrokerService': Cannot create inner bean '(inner bean)' of type [com.alibaba.citrus.service.uribroker.impl.URIBrokerServiceImpl$URIBrokerInfo] while setting bean property 'brokers' with key [0]; nested excepti on is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#25': Cannot create inner bean 'server' of type [com.alibaba.citrus.service.uribroker.uri.GenericURIBroker] while setting constructor argument; nested exception is org.springframework.beans.fact ory.BeanCreationException: Error creating bean with name 'server': Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'URIType' of bean class [com.alibaba.citrus.service.uribroker.uri.GenericURIBroker]: Bean property 'URIType' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
解決方案可以參考:https://github.com/alibaba/dubbo/issues/50
為了省事我是將JDK降到1.7 。
dubbo官網com.alibaba.dubbo.container.Main 啟動服務配置
相應的配置已經配置好了,接下來介紹使用dubbo官網com.alibaba.dubbo.container.Main 啟動服務實現優雅關機
pop.xml打包配置
<build> <finalName>pay-service-bank</finalName> <!--包名稱 --> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes><!-- 配置文件信息 --> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources/spring</directory> <filtering>true</filtering> <includes> <include>spring-context.xml</include> </includes> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <versionRange>[2.0,)</versionRange> <goals> <goal>copy-dependencies</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <!-- 打包jar文件時,配置manifest文件,加入lib包的jar依賴 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <classesDirectory>target/classes/</classesDirectory> <archive> <manifest> <mainClass>com.alibaba.dubbo.container.Main</mainClass> <!-- 打包時 MANIFEST.MF文件不記錄的時間戳版本 --> <useUniqueVersions>false</useUniqueVersions> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <type>jar</type> <includeTypes>jar</includeTypes> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
ps :spring-context.xml文件中對其他xml的引入地址要寫成:
<import resource="classpath:spring/spring-mybatis.xml" />
<import resource="classpath:spring/applicationContext.xml" />
原因是我們在打包配置時將spring-context.xml文件復制到META-INF/spring下,如果還是使用相對地址配置就出錯了找不到文件
OK打包部署。
自定義Dubbo服務端維護的shell腳本
#! /bin/bash usage(){ echo "----------USAGE:--------------------" echo "provider start" echo "provider stop" echo "provider start xx" echo "provider stop xx" echo "-------------------------------------" } stopAll(){ procids=`ps -fe|grep jar |grep -v grep |grep -v tomcat| awk '{print $2}'` if [ -z "$procids" ]; then echo tip: no process found else for procid in $procids do kill $procid done fi } startAll(){ echo '....' jars=`ls *.jar` if [ -z "$jars" ]; then echo top: no jar found,please check if you are in correct directory... else for jarName in $jars do #logName=`echo ${jarName/jar/log}` logName='all.log' nohup java $JAVA_OPTS -jar $jarName >>$logName 2>1 & done fi; } cmd=$1 module=$2 JAVA_OPTS="-server -Xms4096m -Xmx4096m -verbose:gc -XX:+PrintGCDetails -XX:PermSize=256M -XX:MaxPermSize=512m" if [ $# -gt 2 ] || [ $# -lt 1 ]; then usage; elif [ $cmd != 'start' ] && [ $cmd != 'stop' ]; then usage; else case $cmd in 'start') if [ $# -eq 1 ] ; then echo start all provider ............ procids=`ps -fe|grep jar |grep -v grep |grep -v tomcat| awk '{print $2}'` if [ -z "$procids" ]; then echo '' else stopAll fi; startAll else echo start $module ........ echo if [ -n $module".jar" ]; then procid=`ps -fe|grep $module".jar" |grep -v grep | awk '{print $2}'` echo $procid if [ -z "$procid" ]; then #nohup java -jar $module".jar" >$module".log" & nohup java $JAVA_OPTS -jar $module".jar" >>all.log 2>1 & else echo tip: $module".jar" has started already. don not start again! fi else echo $module".jar not exists" fi echo fi ;; 'stop') if [ $# -eq 1 ] ; then echo stop all provider ............ stopAll else echo stop $module ........ echo procid=`ps -fe|grep $module".jar" |grep -v grep | awk '{print $2}'` if [ -z "$procid" ]; then echo tip: java $module".jar" process not exists; else kill $procid fi echo fi ;; esac fi
ps:Dubbo是通過JDK的ShutdownHook來完成優雅停機的,所以如果用戶使用"kill -9 PID"等強制關閉指令,是不會執行優雅停機的,只有通過"kill PID"時,才會執行。
Maven內置變量說明:
- ${basedir} 項目根目錄
- ${project.build.directory} 構建目錄,缺省為target
- ${project.build.outputDirectory} 構建過程輸出目錄,缺省為target/classes
- ${project.build.finalName} 產出物名稱,缺省為${project.artifactId}-${project.version}
- ${project.packaging} 打包類型,缺省為jar
- ${project.xxx} 當前pom文件的任意節點的內容