1. 程式人生 > >使用maven多模組構建dubbo第一個分散式HelloWorld

使用maven多模組構建dubbo第一個分散式HelloWorld

至於為什麼要用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);
    }
}
執行改類,結果如下:



可以看到,遠端服務呼叫成功。