Dubbo HelloWord

Posted jhxxb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo HelloWord相关的知识,希望对你有一定的参考价值。

通过 Dubbo,使 A(消费者:Consumer) 项目代码调用 B(提供者:Provider) 项目的代码

 

注册中心使用 zookeeper

 

一、接口(Interface)

项目之间通过接口进行调用,抽出为一个单独的项目,方便复用

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>dubbo</groupId>
    <artifactId>demo-interface</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

接口

package org.apache.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}

项目结构

技术图片

二、ROOT

由于消费者和提供者依赖相同,把 pom 文件抽出为一个父文件

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>dubbo</groupId>
    <artifactId>root</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>../demo-xml-consumer</module>
        <module>../demo-xml-provider</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>dubbo</groupId>
            <artifactId>demo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 引入dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.1</version>
        </dependency>
        <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

项目结构

技术图片

三、提供者(Provider)

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>root</artifactId>
        <groupId>dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../root/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-xml-provider</artifactId>
</project>

dubbo 配置,provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="demo-provider">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.port" value="22222"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
    </dubbo:application>

    <!-- 2、指定注册中心的位置 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="curator"/>

    <!-- 3、指定通信规则(通信协议?通信端口) -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>

    <!-- 服务的实现 -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- 连接监控中心 -->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>

接口实现类

package org.apache.dubbo.demo.provider;

import org.apache.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        System.out.println("Hello " + name + ", request from consumer: ");
        return "Hello " + name + ", response from provider: ";
    }
}

启动类,提供服务

package org.apache.dubbo.demo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read();
    }
}

项目结构

技术图片

四、消费者(Consumer)

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>root</artifactId>
        <groupId>dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../root/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-xml-consumer</artifactId>

</project>

dubbo 配置,consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.port" value="33333"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
    </dubbo:application>

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

    <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService"/>

    <!-- 连接监控中心 -->
    <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor>
</beans>

启动类,调用服务

package org.apache.dubbo.demo.consumer;

import org.apache.dubbo.demo.DemoService;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Consumer {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        DemoService demoService = context.getBean("demoService", DemoService.class);
        String hello = demoService.sayHello("world");
        System.out.println("result: " + hello);
        System.in.read();
    }
}

项目结构

技术图片

五、测试

先启动服务提供者,再启动服务消费者,即可看到消费者和提供者的控制台打印。

一开始启动老是报端口被占用,查阅后发现 qos 端口不配置情况下每个项目是一样的,要配置一下。

 


http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

http://dubbo.apache.org/zh-cn/docs/user/references/registry/zookeeper.html

http://dubbo.apache.org/zh-cn/docs/user/references/qos.html

以上是关于Dubbo HelloWord的主要内容,如果未能解决你的问题,请参考以下文章

HelloWord

HelloWord

—solidity中的HelloWord

Java入门程序HelloWord

用java编写HelloWord

Mybatis_HelloWord