使用maven集成Dubbo

标签  dubbo  maven  idea  栏目  Maven
原文   http://blog.csdn.net/gemuandyou/article/details/50763653

最近项目要使用Dubbo来实现微服务,简单来说就是将你写好的服务用在其它模块中,当然是同一个业务流程中的多个模块。

使用的开发工具是IntelliJ IDEA。需要的库是zookeeper。

准备工作是这个样子的。New Project -> Maven -> Create from archetype (maven-archetype-webapp) -> 项目名为DubboDemo。

完成后,新建一个module。在DubboDemo项目上右键, New -> Module -> Maven -> Create from archetype (maven-archetype-webapp) -> 模块名为DubboService。

同样的步骤,再新建一个名为DubboConsumer的模块。

在DubboService和DubboConsumer的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.gemugroupId>
    <artifactId>dubbo-demoartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>DubboServicemodule>
        <module>DubboConsumermodule>
    modules>
    <name>dubbo-demo Maven Webappname>
    <url>http://maven.apache.orgurl>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>

        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.4.9version>
        dependency>

        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.3.3version>
        dependency>

        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.7version>
        dependency>

    dependencies>
    <build>
        <finalName>dubbo-demofinalName>
    build>
project>
其中有spring、zookeeper和dubbo的依赖包。 
 在DubboService中编写提供服务的代码。结构和内容如下:

gemu
ps: 请允许我用我蹩脚的英语装个B,截图软件我这个中文乱码。实属无奈。。。

appCtx.xml是spring的配置文件(内容是抄过来的,文章最后有地址)。内容如下:


<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">

       
       <bean id="demoService" class="com.gemu.DemoServiceImpl">bean>

       
       <dubbo:application name="demo_provider" />

       
       

       
       <dubbo:registry address="zookeeper://localhost:2181" />

       
       <dubbo:protocol name="dubbo" port="20880" />

       
       <dubbo:service interface="com.gemu.intf.DemoService" ref="demoService" />

beans>


DemoServiceImpl.java 内容如下,提供服务的逻辑。

package com.gemu;

import com.gemu.intf.DemoService;

/**
 * Created by Gemu on 2/27/2016 0027.
 */
public class DemoServiceImpl implements DemoService {


    public String build() {
        System.out.println("build success");
        return "build success and return success";
    }
}

Provider.java 内容如下, 启动dubbo。

package com.gemu;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created by Gemu on 2/27/2016 0027.
 */
public class Provider {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appCtx.xml");
        ctx.start();
        System.out.println("我咋这么帅呢");
        System.in.read();
    }
}

那么现在服务端已经准备好了,接下来就是消费端了。 

DubboConsumer中的 appCtx.xml spring配置如下:


<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="demo_consumer" />

       
       
       <dubbo:registry address="zookeeper://127.0.0.1:2181" />

       
       <dubbo:reference id="demoService"
                        interface="com.gemu.intf.DemoService" />
beans>
PS:有报错的地方就 alt + enter 一下就好了,小灯泡还是很给力的。 

Consumer.java 内容如下:

package com.gemu;

import com.gemu.intf.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Gemu on 2/29/2016 0029.
 */
public class Consumer {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appCtx.xml");
        DemoService demoService = (DemoService) ctx.getBean("demoService");
        String retStr = demoService.build();
        System.out.println(retStr);
    }
}
这样我们的准备工作就都做好了。接下来就是测试了。

首先你得启动zookeeper吧,不然还扯什么蛋。然后运行DubboService中的Provider.java(过程有时候慢,不清楚为啥,可能zookeeper的连接有些问题吧),控制台会出现如下内容:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
我咋这么帅呢

恭喜你,启动成功了。接下来运行DubboConsumer中的Consumer.java,测试消费者是否可以调用此服务吧。如果Provider的控制台和Consumer的控制台分别输入如下内容,那么恭喜你成功了。

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
我咋这么帅呢
build success

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
build success and return success

Process finished with exit code 0
PS: Consumer调用服务的过程有点儿慢,不知道是不是我的配置有问题啊?所以仅供参考了,如果神知道啥原因的话就给我留个言哈。 参考:http://my.oschina.net/superman158/blog/466637 http://www.ziliao1.com/Article/Show/7B9AC403C03106FD10EDCE9C35D40123.html

你可能感兴趣的:(MAVEN)