将dubbo provider打包成jar包

在使用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-jarhttps://gitee.com/hylun/SpringWeb/tree/master/dubbo-provider-to-jar

Dubbo 2.5.3 全部jar包下载 [INFO] dubbo-parent ...................................... SUCCESS [1.042s] [INFO] Hessian Lite(Alibaba embed version) ............... SUCCESS [4.438s] [INFO] dubbo-common ...................................... SUCCESS [9.153s] [INFO] dubbo-container ................................... SUCCESS [0.019s] [INFO] dubbo-container-api ............................... SUCCESS [1.557s] [INFO] dubbo-container-spring ............................ SUCCESS [1.378s] [INFO] dubbo-container-jetty ............................. SUCCESS [1.448s] [INFO] dubbo-container-log4j ............................. SUCCESS [1.566s] [INFO] dubbo-container-logback ........................... SUCCESS [1.775s] [INFO] dubbo-remoting .................................... SUCCESS [0.151s] [INFO] dubbo-remoting-api ................................ SUCCESS [6.705s] [INFO] dubbo-remoting-netty .............................. SUCCESS [5.750s] [INFO] dubbo-remoting-mina ............................... SUCCESS [2.109s] [INFO] dubbo-remoting-grizzly ............................ SUCCESS [0.989s] [INFO] dubbo-remoting-p2p ................................ SUCCESS [2.322s] [INFO] dubbo-remoting-http ............................... SUCCESS [3.506s] [INFO] dubbo-remoting-zookeeper .......................... SUCCESS [1.279s] [INFO] dubbo-rpc ......................................... SUCCESS [0.015s] [INFO] dubbo-rpc-api ..................................... SUCCESS [3.413s] [INFO] dubbo-rpc-default ................................. SUCCESS [4.105s] [INFO] dubbo-rpc-injvm ................................... SUCCESS [3.112s] [INFO] dubbo-rpc-rmi ..................................... SUCCESS [1.349s] [INFO] dubbo-rpc-hessian ................................. SUCCESS [1.721s] [INFO] dubbo-rpc-http .................................... SUCCESS [1.199s] [INFO] dubbo-rpc-webservice .............................. SUCCESS [0.997s] [INFO] dubbo-cluster ..................................... SUCCESS [6.085s] [IN
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值