至于为什么要用maven多模块构建项目,和dubbo是做什么的就不多说了,直接开始。

首先创建一个maven项目作为root模块 命名为mydubbo,并删除其中的src目录(不需要)


除外我们需要Spring,zookeeper的依赖

mydubbo->pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com</groupId>
    <artifactId>mydubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring.version>3.2.4.RELEASE</spring.version>
    </properties>
    <modules>
        <module>myProvider</module>
        <module>myConsumer</module>
        <module>myService</module>
    </modules>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--dubbo注册中心-->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!--zookeeper客户端-->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>


然后创建三个maven子模块分别为myService、myProvider、myConsumer

myService:功能接口模块

myProvider:dubbo提供者

myConsumer:dubbo消费者

因为消费者和提供者的功能接口一致,所以都依赖myService模块(myService的项目packing 为 jar)


myProvider->pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>mydubbo</artifactId>
        <groupId>com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>myProvider</artifactId>
    <dependencies>
        <dependency>
            <groupId>com</groupId>
            <artifactId>myService</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

myConsumer->pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>mydubbo</artifactId>
        <groupId>com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>myConsumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>com</groupId>
            <artifactId>myService</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

myService->pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>mydubbo</artifactId>
        <groupId>com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <name>myService</name>
    <artifactId>myService</artifactId>
    <packaging>jar</packaging>
</project>

在myService中建立一个Interface,命名为HelloService,具体如下


HelloService.java

package com.service;

/**
 * Created by yuyufeng on 2016/11/16.
 */
public interface HelloService {
    String speakHello(String name);
}

现在,我们需要install myService这个项目,产生一个可以依赖的jar供其它引用者使用。

myProvider的依赖就有myService产生的jar



现在,开始编写dubbo提供者myProvider的代码

首先,需要提供HelloService的实现


HelloServiceImpl.java

package com.service.impl;

import com.service.HelloService;

/**
 * Created by yuyufeng on 2016/11/16.
 */
public class HelloServiceImpl implements HelloService {
    public String speakHello(String name) {
        return "你好:" + name;
    }
}
然后使 用Spring配置声明暴露服务,建立一个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="hello-provider"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.service.HelloService" ref="helloService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="helloService" class="com.service.impl.HelloServiceImpl" />

</beans>

本次dubbo使用的注册中心为zookeeper,所以需要在本地启动一个zookeeper服务。
最后,我们就可以启动提供者服务了,启动类如下:

package com.service.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created by yuyufeng on 2016/11/16.
 */
public class ProviderServer{
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
	//按任意键退出
        System.in.read();

    }
}

启动之后我们可以在zookeeper注册中心看到我们的服务提供者已经注册在上面


然后,就编写个消费者通过dubbo来调以下远程的服务吧

消费者通过Spring配置引用远程服务spring配置文件如下:

consumer.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="hello-consumer"  />
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="helloService" interface="com.service.HelloService" />
</beans>
消费者启动类

ConsumerClient.java

import com.service.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by yuyufeng on 2016/11/16.
 */
public class ConsumerClient {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        HelloService helloService = (HelloService) context.getBean("helloService");
        String result = helloService.speakHello("yyf");
        System.out.println(result);
    }
}
运行改类,结果如下:



可以看到,远程服务调用成功。


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐