Dubbo之——與高版本的Spring整合
阿新 • • 發佈:2019-02-07
使用Dubbo時,因為要與4.x版本的spring,所以,我們需要做如下修改。
一,與4.x版本的Spring共存
首先需要排除掉dubbo自帶的2.5的Spring,所以我使用下面這個配置,前面都是正常的引用Spring 4.1.6.RELEASE。
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <artifactId>netty</artifactId> <groupId>org.jboss.netty</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
二,打包成本地APP
需要使用Shade這個外掛來打包,POM配置如下,其中最關鍵是那兩個transformer,保證將spring.handlers和spring.schemas檔案能合併起來,否則執行時會報找不到dubbo:application錯誤,或者說無法處理dubbo:application錯,都是因為在預設的打包模式下,META-INF目錄下同名檔案是隻取第一個,而不會多個合併起來,導致找不到dubbo.xsd以及對應的handler類:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>cn.chinaunicom.woplus.analysis.MongoStatisticServer.App</Main-Class> <X-Compile-Source-JDK>1.7</X-Compile-Source-JDK> <X-Compile-Target-JDK>1.7</X-Compile-Target-JDK> </manifestEntries> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> </transformers> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin>