Dubbo-使用Maven构建Dubbo服务的可执行jar包
一、为什么要构建Dubbo服务的可执行jar包?
1.1 Dubbo服务运行方式比较
✎使用Servlet容器运行(Tomcat、Jetty等) ---不可取
--缺点:增加复杂性(多了容器的端口)
浪费内存资源,Servlet运行要占用一定的内存
✎自建main()方法来运行Spring容器 ---不可取 (本地调试可用)
--缺点:Dubbo本身提供的高级特性没用上
自己编写启动类可能会有缺陷
✎使用Dubbo框架提供main()方法来运行Spring容器 ---建议使用
--优点:框架本身提供(com.alibaba.dubbo.container.Main)
可实现优雅关机(ShutdownHook)
1.2 官方文档查看
http://dubbo.io/books/dubbo-user-book/demos/service-container.html
“6.40 容器服务”部分
综上所述所以要构建Dubbo服务的可执行jar包(说了句废话,偷笑!!)
二、怎么构建?
2.1 看看工程目录
这个就是我们要打包成Dubbo服务的工程
2.2 maven插件
<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> <!-- 结合com.alibaba.dubbo.container.Main --> <resource>
<!--把resources/spring目录下的文件打包的时候打到类路径下METAINF/spring目录下--> <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> <!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 --> <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> <useUniqueVersions>false</useUniqueVersions> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> </plugins>
2.3 maven 打包
mvn clean package
然后再项目的target目录下就看到
打开edu-service-user.jar目录看一下结构
2.4 运行jar包:
在jar文件的目录下运行 java -jar edu-service-user.jar
2.4 注意点:
在import引入其他xml配置文件的地方要加 "classpath:"