Dubbo之使用Maven构建服务的可执行jar包
Dubbo服务的运行方式,1、使用Servlet容器运行(Tomcat、Jetty等) 1.1 修改项目的打包方式,应该使用war的形式 1.2 新增文件目录如下 1.3 web.xml的内容如下,在Servlet容器启动的时候,加载相关的Spring配置文件<web-app xmlns:xsi=
Dubbo服务的运行方式,
1、使用Servlet容器运行(Tomcat、Jetty等)
1.1 修改项目的打包方式,应该使用war的形式
1.2 新增文件目录如下
1.3 web.xml的内容如下,在Servlet容器启动的时候,加载相关的Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="gw-web-boss" version="2.5">
<display-name>Demo</display-name>
<!--用于加载Spring的配置文件,包含服务提供者的信息-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.4 启动Servlet容器,发布服务
2、自建Main方法类运行(Spring容器)
本地调式可用,因为自己编写的启动类有缺陷,Dubbo本省的高级特性没用上
2.1 自己编写的启动类
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboProvider {
public static void main(String[] args) {
try {
// 用于加载Spring的配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"/spring/spring-*.xml");
context.start();
} catch (Exception exception) {
throw new RuntimeException("DubboProvider context start error: "
+ exception);
}
synchronized (DubboProvider.class) {
while (true) {
try {
DubboProvider.class.wait();
} catch (InterruptedException e) {
throw new RuntimeException("synchronized error: " + e);
}
}
}
}
}
3、使用Dubbo框架提供的Main方法类来运行(Spring容器)
框架本身提供(com.alibaba.dubbo.container.Main),可实现优雅关机。
注意:com.alibaba.dubbo.container.Main,默认加载META-INF\spring下的配置文件
4、关于Maven打包的配置文件的介绍
4.1 在parent中添加的maven插件如下
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.2 在provider项目的pom文件里面,添加的maven插件信息如下
<!-- 相关插件的管理 -->
<build>
<finalName>mydubbo-provider</finalName>
<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,因为dubbo自带的Main需要读取classes/META-INF/spring下的spring配置信息 -->
<resource>
<targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>
<directory>src/main/resources/spring</directory>
<filtering>true</filtering>
<includes>
<!--将spring相关的配置文件拷贝到classes/META-INF/spring下 -->
<include>spring-*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 配置manifest文件,以及服务jar的启动类 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!--执行Jar文件的启动类,使用dubbo自带的启动类 -->
<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>
<!-- 将运行服务的jar文件,依赖的其它jar文件放在lib文件夹下 -->
<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>
<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>
</build>
5、运行mavne命令,执行结果如下
6、查看mydubbo-provider\META-INF\MANIFEST.MF的配置信息,如下
7、运行服务
java -jar mydubbo-provider.jar &
更多推荐
所有评论(0)