dubbo提供者服务使用dubbo框架提供的Main方法类来运行:
dubbo框架提供:com.alibaba.dubbo.container.Main(可实现优雅关机ShutdownHook)
在提供者的pom.xml中配置以下maven插件:
<build>
<finalName>dubbo-provider</finalName> <!-- jar包名,一般设置为提供者服务名 -->
<resources>
<!-- 把resource目录下所有文件和文件夹导入到jar包内 -->
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<!-- 可直接写为<include>**/*</include>包含所有文件 -->
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<!-- 使用com.alibaba.dubbo.container.Main启动spring容器,会默认加载META-INF/spring目录下的所有spring配置 -->
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources/spring</directory>
<filtering>true</filtering>
<!-- 把src/main/resources/spring路径下的spring配置文件拷贝到${project.build.directory}/classes/META-INF/spring下 -->
<includes>
<include>spring-application.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>
<!-- maven打包成jar的插件 -->
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 把target/classes/下的文件导入到jar内 -->
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!-- 指定程序入口 -->
<mainClass>com.alibaba.dubbo.container.Main</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<!-- 服务依赖的jar包放在lib目录下 -->
<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>
<!-- 存放服务依赖的jar包,存放在服务相同目录的lib文件夹下 -->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意在spring-application.xml中引入其他配置文件时要写为:
<import resource="classpath:spring/spring-mybatis.xml" />
<import resource="classpath:spring/dubbo-provider.xml" />
因为spring-application.xml会被加载到${project.build.directory}/classes/META-INF/spring下,而不与引入的配置文件在同一目录下。
在对dubbo-provider服务打包成jar之前(maven install),需要将dubbo-provider服务依赖的工程先打包,在本地maven仓库生成相应的jar,这样dubbo-provider服务才会打包成功。最终在项目target目录下会生成lib文件夹和dubbo-provider.jar。lib文件夹存放的是dubbo-provider服务依赖的jar。
注:dubbo-provider.jar为什么能引用到lib文件夹内的jar,是因为dubbo-provider.jar的META-INF/MANIFEST.MF配置文件内说明了dubbo-provider服务依赖的jar。
运行dubbo-provider.jar:打开命令控制器,在dubbo-provider.jar所在的目录下输入java -jar dubbo-provider.jar &
,回车,出现下面的界面即运行成功:
管控台上成功显示提供者: