dubbo服務使用maven部署(支援profile多環境配置)
部署dubbo服務通常有三種方式:
1、打war包,放在tomcat中執行,
缺點:增加複雜性(埠、管理) 浪費資源(記憶體)
官方:服務容器是一個standalone的啟動程式,因為後臺服務不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去載入服務提供方,增加複雜性,也浪費資源。
2、打jar包,自定義啟動類使用main函式啟動,(適合本地除錯使用)
缺點: Dobbo本身提供的高階特性沒用上 自已編寫啟動類可能會有缺陷
官方:服務容器只是一個簡單的Main方法,並載入一個簡單的Spring容器,用於暴露服務。
3、打jar包,使用dubbo提供的com.alibaba.dubbo.container.Main 方法執行,
優點:框架本身提供(com.alibaba.dubbo.container.Main)
可實現優雅停機(ShutdownHook)
官方:服務容器的載入內容可以擴充套件,內建了spring, jetty, log4j等載入,可通過Container擴充套件點進行擴充套件
Dubbo是通過JDK的ShutdownHook來完成優雅停機的,所以如果使用者使用"kill -9 PID"等強制關閉指令,是不會執行優雅停機的,只有通過"kill PID"時,才會執行。
原理:
服務提供方
停止時,先標記為不接收新請求,新請求過來時直接報錯,讓客戶端重試其它機器。
然後,檢測執行緒池中的執行緒是否正在執行,如果有,等待所有執行緒執行完成,除非超時,則強制關閉。
服務消費方
停止時,不再發起新的呼叫請求,所有新的呼叫在客戶端即報錯。
然後,檢測有沒有請求的響應還沒有返回,等待響應返回,除非超時,則強制關閉。
使用自定義啟動類:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.0</version> <executions><execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <useUniqueVersions>false</useUniqueVersions> <mainClass>com.***.Main自己定義的啟動類 </mainClass> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> </configuration> </plugin>
使用這種方式打包後 應用程式依賴的jar包放在lib目錄下,lib與應用程式在同級目錄,直接java -jar ***.jar即可執行
但是若使用jenkins自動部署或者其他原因需要把依賴跟應用程式的jar包打在一起,上面那個方法就不適用了,可以使用一個maven外掛:maven-shade-plugin,具體配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>com.*****.Main自定義的啟動類</Main-Class> <Class-Path>.</Class-Path> </manifestEntries> </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> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin>
這樣在目標資料夾下面就會有兩個jar包,一個應用程式原本的jar包original*****.jar,還有一個包含了所有依賴的jar包,就是不帶original的jar包,這個jar包就是可以直接執行的 不需要依賴的包,附屬可以直接把這個jar包放在伺服器上,然後java -jar ***.jar執行。
上面就是使用自定義啟動類啟動dubbo服務的maven配置檔案,
下面看下使用dubbo提供的啟動類如何配置maven:
基本與上面的方式2類似,有幾點注意:需要將spring配置檔案拷貝到/classes/META-INF/spring目錄下,
<resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <!-- 結合com.alibaba.dubbo.container.Main,需要重點掌握--> <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>
我的專案使用maven profile配置了多環境,配置如下:(如果使用了下面配置就不用用上面的資原始檔配置了):
<profiles> <profile> <!-- 開發環境 --> <id>dev</id> <properties> <env>dev</env> <skipTests>true</skipTests> </properties> <activation> <!-- 預設啟用該profile節點--> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources_env/dev</directory> </resource> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> </resource> <resource> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources_env/dev</directory> </resource> <resource> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources</directory></resource></resources> </build> </profile> <profile> <!-- 生產環境 --> <id>online</id> <properties> <env>online</env> <skipTests>true</skipTests> </properties> <build> <resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources_env/online</directory> </resource> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> </resource> <resource> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources_env/online</directory> </resource> <resource> <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath> <directory>src/main/resources</directory></resource></resources> </build> </profile> </profiles>
然後再配置下maven打包外掛,與方式2的一樣,只要改下啟動類就好,具體程式碼貼下如下:
<!-- 打包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> <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> <version>3.0.0</version> <executions> <execution> <id>copy</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>
同2 這個也是把依賴放在應用程式的jar包同級lib目錄下,如果需要依賴打包到一起,跟方式2一樣使用maven的maven-shade-plugin外掛:程式碼貼如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>com.alibaba.dubbo.container.Main</Main-Class> </manifestEntries> </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> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin>
同方式2 直接一個jar包執行就好。
多環境maven執行命令如下:clean install -Ponline -Dmaven.test.skip=true
參考文章:https://www.cnblogs.com/jeesml/p/5104270.html
感謝!還有看了一些部落格找不到了,這裡就不列出了。