将dubbo provider打包成jar包

来源:互联网 发布:删除内置软件 编辑:程序博客网 时间:2024/06/07 05:30

在使用dubbo框架搭建集群时,如果我们将dubbo打包成可运行的jar包,就能很方便的构架起微服务集群
本文将示例使用maven-shade-plugin插件,将dubbo provider项目打包成一个整体的可运行jar包

1.使用maven创建一个新dubbo provider项目

Eclipse中,点File->New Project…菜单,选择Maven Project,创建artifactId为dubbo-provider-to-jar的项目,修改pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>alun</groupId>    <artifactId>dubbo-provider-to-jar</artifactId>    <version>0.0.1-SNAPSHOT</version>    <properties>        <!-- spring版本号 -->        <spring.version>4.3.7.RELEASE</spring.version>    </properties>    <dependencies>        <!-- spring -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context-support</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- dubbo依赖 -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>            <exclusions>                <exclusion>                    <artifactId>spring</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.6</version>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>0.10</version>        </dependency>    </dependencies>    <build>        <finalName>${artifactId}-${version}</finalName>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>            <!-- 打成jar包 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-shade-plugin</artifactId>                <version>2.4.1</version>                <configuration>                    <!-- 是否生成缩减的pom文件,默认不配置是true -->                    <createDependencyReducedPom>false</createDependencyReducedPom>                </configuration>                <executions>                    <execution>                        <phase>package</phase>                        <goals>                            <goal>shade</goal>                        </goals>                        <configuration>                            <transformers>                                <transformer                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                                    <!-- 启动类 -->                                    <mainClass>mytest.dubbo.Main</mainClass>                                </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>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>

2.新建启动入口程序mytest.dubbo.Main,内容如下:

package mytest.dubbo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    private static boolean running=true;    @SuppressWarnings("resource")    public static void main(String[] args) throws Exception {        final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {                "classpath:dubbo-provider.xml"        });        Runtime.getRuntime().addShutdownHook(new Thread() {            public void run() {                context.stop();                System.out.println("dubbo service has stop");                synchronized (Main.class) {                    running = false;                    Main.class.notify();                }            }        });        context.start();        System.out.println("dubbo service has start");        synchronized (Main.class) {            while (running) {                try {                    Main.class.wait();                } catch (Throwable e) {}            }        }    }}

3.src/main/resources下新建dubbo-provider.xml配置,如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd              http://code.alibabatech.com/schema/dubbo     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="dubbo-service" />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 用dubbo协议在20880端口暴露服务 -->    <dubbo:protocol name="dubbo" port="20880" />    <!-- <dubbo:protocol name="rmi" codec="spring"/> -->    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="mytest.dubbo.service.TestService" ref="testService" />    <!-- 和本地bean一样实现服务 -->    <bean id="testService" class="mytest.dubbo.service.impl.TestServiceImpl" /></beans>

4.创建对外提供服务接口及实现类,内容如下:

mytest.dubbo.service.TestService:

package mytest.dubbo.service;public interface TestService {    String sayHello(String name);}

mytest.dubbo.service.impl.TestServiceImpl:

package mytest.dubbo.service.impl;import mytest.dubbo.service.TestService;public class TestServiceImpl implements TestService {    public String sayHello(String name) {        return "Hello "+name;    }}

5.使用maven打包项目:

Eclipse项目上点Run As -> Maven build ,Goals栏填package,编译完成后,将在target目录中看到两个jar包及lib目录:
dubbo-provider-to-jar-0.0.1-SNAPSHOT.jar 为包含了第三方类库的完整jar包,可单独部署
original-dubbo-provider-to-jar-0.0.1-SNAPSHOT.jar 为不包含第三方类库的jar包,结合lib目录可部署。

cd到target目录,执行java -jar dubbo-provider-to-jar-0.0.1-SNAPSHOT.jar,就能看到已经能成功运行了。

查看源码 https://github.com/hylun/SpringWeb/tree/master/dubbo-provider-to-jar 或 https://gitee.com/hylun/SpringWeb/tree/master/dubbo-provider-to-jar