dubbo框架提供Main方法運行容器的幾種方式(轉)


  本文使用的是dubbo提供的主類com.alibaba.dubbo.container.Main啟動容器。
主要區別是提供不同插件的的啟動方式。

目錄

一、項目內容
 1.1、目錄結構圖
 1.2、相關文件配置
二、運行容器方式
 2.1 使用Java命令啟動(手動)
  2.1.1 使用maven-shade-plugin 插件打包運行
  2.1.2 使用 maven-jar-plugin 插件和 maven-dependency-plugin 插件打包運行
 2.2 使用腳本啟動
  2.2.1 windows啟動腳本start.bat
  2.2.2 linux啟動腳本start.sh
  2.2.3 linux停止腳本stop.sh
  2.2.4 linux重啟腳本restart.sh

一、項目內容

1.1 目錄結構如圖:


 
 

1.2 相關文件的配置

  • DemoService
package com.test.provider; /** * Created by Administrator on 2018/2/1. */ public interface DemoService { String sayHello(String word); } 
  • DemoServiceImpl
package com.test.provider; import org.springframework.stereotype.Service; /** * Created by Administrator on 2018/2/1. */ @Service("demoService") public class DemoServiceImpl implements DemoService { public String sayHello(String word) { return "hello " + word + ",I'm provider \r\n"; } } 
  • assemble-descriptor.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd"> <id>bin</id> <formats> <format>tar.gz</format> <format>zip</format> </formats> <includeBaseDirectory>true</includeBaseDirectory> <fileSets> <fileSet> <directory>src/main/resources/bin</directory> <outputDirectory>bin</outputDirectory> <includes> <include>*</include> </includes> <fileMode>0755</fileMode> <filtered>true</filtered> </fileSet> <fileSet> <directory>src/main/resources/cconf</directory> <outputDirectory>cconf</outputDirectory> <includes> <include>*</include> </includes> <fileMode>0755</fileMode> <filtered>true</filtered> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <useProjectArtifact>true</useProjectArtifact> <unpack>false</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> </assembly> 
  • application.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="classpath:cconf/dubbo-provider.xml"/> </beans> 
  • dubbo-provider.xml
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.test.provider"/> <!--IP 為zookeeper 地址--> <dubbo:registry protocol="zookeeper" address="47.93.200.10:2181"/> <dubbo:service interface="com.test.provider.DemoService" ref="demoService" /> </beans> 
  • dubbo.properties
#把屬行放在該文件主要是為了方便使用腳本啟動時,獲取以下的信息 dubbo.application.name=hello-provider dubbo.protocol.name=dubbo dubbo.protocol.port=20880 
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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> <groupId>com.jay.test.dubbo-zookeeper</groupId> <artifactId>hello-provider</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <spring.version>4.3.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.9</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <!--dubbo注冊中心--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> <!--zookeeper客戶端--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> <!--dubbo 容器配置--> <!-- 把 directory 下的 include 文件 復制到 targetPath--> <resource> <!--dubbo 默認指定的容器啟動目錄--> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources/cconf</directory> <filtering>true</filtering> <includes> <include>application.xml</include> </includes> </resource> </resources> <plugins> <!--java命令啟動:第一種打包方式--> <!--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.alibaba.dubbo.container.Main</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin>--> <!--分————————————割——————————————————線--> <!--java命令:第二種打包方式--> <!--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> &lt;!&ndash; <classesDirectory>target/classes/</classesDirectory>&ndash;&gt; <archive> <manifest> <mainClass>com.alibaba.dubbo.container.Main</mainClass> &lt;!&ndash; 打包時 MANIFEST.MF文件不記錄的時間戳版本 &ndash;&gt; <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>--> <!--分————————————割——————————————————線--> <!--腳本啟動使用的插件--> <!--該插件可以做額外的文件打包,比如bin目錄,conf配置文件夾。使用java 命令啟動可忽略該插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>make-binary-pacakge</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <!--打包后的報名--> <finalName>deploy-jar</finalName> <!-- 包名是否追加assemble-descriptor.xml中的id--> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/resources/assemble/assemble-descriptor.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> 

二、運行容器方式

2.1、使用java命令啟動(手動)

2.1.1 方法一:使用maven-shade-plugin 插件打包運行

pom主要使用如下配置

<build> <resources> <!--主要是把src/main/resources下的所有的文件中的變量替換--> <!--如:dubbo.properties 中的prop.log.dir=${pom.log.dir},pom.log.dir配置在pom.xml文件中--> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> <!--dubbo 容器配置--> <!-- 把 directory 下的 include 文件 復制到 targetPath--> <resource> <!--dubbo 默認指定的容器啟動目錄--> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources/cconf</directory> <filtering>true</filtering> <includes> <include>application.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.alibaba.dubbo.container.Main</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> 

打包完成后target如下

 
 

在jar包的目錄打開cmd,運行命令 java -jar hello-provider-1.0-SNAPSHOT.jar,如下圖即啟動成功

 
 

在dubbo-ammin 也能看到對應的注冊信息

 
 
2.1.2 使用 maven-jar-plugin 插件和 maven-dependency-plugin 插件

pom主要使用如下配置

<build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> <!--dubbo 容器配置--> <!-- 把 directory 下的 include 文件 復制到 targetPath--> <resource> <!--dubbo 默認指定的容器啟動目錄--> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources/cconf</directory> <filtering>true</filtering> <includes> <include>application.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <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> 

打包完成后target如下圖

 
 

打開target目錄,運行 java -jar hello-provider-1.0-SNAPSHOT.jar 即可,是否注冊成功參照2.1.1圖
注:該方式jar包和lib包必須在同一目錄下運行java命令才能啟動spring容器

2.2 使用腳本啟動

dubbo也提供腳本啟動方式,在dubbo-2.5.9.jar中的META-INF/assembly.bin下有提供模板文件。

 
 

使用腳本啟動容器只需要使用 maven-assembly-plugin 插件即可。主要配置如下:

<build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.*</include> </includes> </resource> <!--dubbo 容器配置--> <!-- 把 directory 下的 include 文件 復制到 targetPath--> <resource> <!--dubbo 默認指定的容器啟動目錄--> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources/cconf</directory> <filtering>true</filtering> <includes> <include>application.xml</include> </includes> </resource> </resources> <plugins> <!--腳本啟動使用的插件--> <!--該插件可以做額外的文件打包,比如bin目錄,conf配置文件夾。使用java 命令啟動可忽略該插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>make-binary-pacakge</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <!--打包后的報名--> <finalName>deploy-jar</finalName> <!-- 包名是否追加assemble-descriptor.xml中的id--> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/resources/assemble/assemble-descriptor.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> 

重新編譯打包項目,在target下會出現兩個文件:deploy-jar.zip和deploy-jar.tar.gz。windows使用deploy-jar.zip,linux使用deploy-jar.tar.gz。再上傳到對應服務器上解壓內容如下:


 
 

進入bin目錄,windows啟動使用bat,linux使用sh腳本

2.2.1 windows啟動腳本start.bat

內容如下,雙擊start.bat即可運行,驗證方式如方法一

@echo off & setlocal enabledelayedexpansion set LIB_JARS=..\cconf cd ..\lib for %%i in (*) do set LIB_JARS=!LIB_JARS!;..\lib\%%i cd ..\bin echo Starting the server [服務名] ...... java -classpath %LIB_JARS% com.alibaba.dubbo.container.Main endlocal 
2.2.2 linux啟動腳本start.sh

進入bin目錄,執行./start.sh,驗證方式如方法一

#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/cconf # ======================================================================================= # 檢測操作系統類型 # ======================================================================================= OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]` case "$OS" in 'sunos') # OS="solaris" ;; 'hp-ux' | 'hp-ux64') # 未經過驗證 # OS="linux" ;; 'darwin') # Mac OSX OS="unix" ;; 'unix_sv') OS="unix" ;; esac # 該腳本目前只支持linux、Mac OSX if [ "$OS" != "linux" ] && [ "$OS" != "unix" ]; then echo "Unsupported OS: $OS" exit 1 fi # ======================================================================================= # 檢測服務是否已經啟動,或者端口號是否已經被占用 # Mac OSX支持: ps -e -o 'pid=,command=',但linux必須寫成: ps -e -o 'pid=' -o 'command=' # ======================================================================================= PIDS=`ps -e -o 'pid=' -o 'command='|grep java|grep "$CONF_DIR"|awk '{print $1}'` if [ -n "$PIDS" ]; then # 服務已經啟動 echo "ERROR: The $SERVER_NAME already started!" echo "PID: $PIDS" exit 1 fi if [ -n "$SERVER_PORT" ]; then # 端口號是否被占用 # netstat的輸出格式: # linux: 192.168.169.1:10050 # Mac OSX: 192.168.169.2.56508 if [ "$OS" == "unix" ]; then SERVER_PORT_COUNT=`netstat -ant -p tcp|tail -n +3|awk '{print $4}'|grep '[.:]$SERVER_PORT' -c` else SERVER_PORT_COUNT=`netstat -ant|tail -n +3|awk '{print $4}'|grep '[.:]$SERVER_PORT' -c` fi if [ $SERVER_PORT_COUNT -gt 0 ]; then echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!" exit 1 fi fi # ======================================================================================= # 啟動服務 # ======================================================================================= # dubbo服務配置參數 SERVER_NAME=`sed '/^#/d;/dubbo.application.name/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`hostname` fi SERVER_PORT=`sed '/^#/d;/dubbo.protocol.port/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` SERVER_HOST=`sed '/^#/d;/dubbo.protocol.host/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` if [ -z "$SERVER_HOST" ]; then SERVER_HOST=127.0.0.1 fi # 日志:log4j.xml文件路徑、日志路徑、stdout日志文件名 LOG4J_XML=`sed '/^#/d;/prop.log.log4j-xml/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` LOG_DIR=`sed '/^#/d;/prop.log.dir/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` if [ -n "$LOG_DIR" ]; then LOG_DIR=`dirname $LOG_DIR/stdout.log` else LOG_DIR=$DEPLOY_DIR/logs fi if [ ! -d $LOG_DIR ]; then # 日志目錄不存在,創建這個目錄 mkdir -p $LOG_DIR fi LOG_STDOUT=`sed '/^#/d;/prop.log.stdout-file/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` if [ -z "$LOG_STDOUT" ]; then LOG_STDOUT=$LOG_DIR/stdout.log else OG_STDOUT=$LOG_DIR/$LOG_STDOUT fi # classpath設置 LIB_DIR=$DEPLOY_DIR/lib LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"` CLASS_PATH=$CONF_DIR:$LIB_JARS JAVA_OPTS=" -Dfile.encoding=utf-8 -Duser.language=en -Duser.country=US -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dlog4j.configuration=$LOG4J_XML " echo "Starting the $SERVER_NAME, $SERVER_HOST:$SERVER_PORT" nohup java $JAVA_OPTS -classpath $CLASS_PATH com.alibaba.dubbo.container.Main > $LOG_STDOUT 2>&1 & # ======================================================================================= # 檢測服務狀態,服務啟動狀態OK之后再退出 # ======================================================================================= echo -e " Waiting for service [$SERVER_HOST $SERVER_PORT] status OK ...\c" COUNT=0 while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 # 能夠連通服務端口號,則服務啟動完成 COUNT=`echo status | nmap $SERVER_HOST -p $SERVER_PORT | grep -c open` done echo "OK!" # 下面ps命令參數兼容linux、Mac OSX(Free BSD) PIDS=`ps -e -o 'pid=' -o 'command='|grep java|grep "$CONF_DIR"|awk '{print $1}'` echo " PID: $PIDS" echo " STDOUT: $LOG_STDOUT" 
2.2.3 linux停止腳本stop.sh
#!/bin/bash cd `dirname $0` BIN_DIR=`pwd` cd .. DEPLOY_DIR=`pwd` CONF_DIR=$DEPLOY_DIR/cconf SERVER_NAME=`sed '/^#/d;/dubbo.application.name/!d;s/.*=//' cconf/dubbo.properties | tr -d '\r'` if [ -z "$SERVER_NAME" ]; then SERVER_NAME=`hostname` fi PIDS=`ps -e -o 'pid=' -o 'command='|grep java|grep "$CONF_DIR"|awk '{print $1}'` if [ -z "$PIDS" ]; then echo "ERROR: The $SERVER_NAME does not started!" exit 1 fi echo "Stopping the $SERVER_NAME ..." for PID in $PIDS ; do kill $PID > /dev/null 2>&1 echo " PID: $PID" done echo -e " Waiting PIDS to quit ...\c" COUNT=0 while [ $COUNT -lt 1 ]; do echo -e ".\c" sleep 1 COUNT=1 for PID in $PIDS ; do PID_EXIST=`ps -p $PID|tail -n +2|wc -l` if [ "$PID_EXIST" -gt 0 ]; then COUNT=0 break fi done done echo "OK!" 
2.2.4 linux重啟腳本restart.sh
#!/bin/bash cd `dirname $0` ./stop.sh ./start.sh




原文鏈接:https://www.jianshu.com/p/34464216a293
來源:簡書


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM