前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个简单的案例带你入门Dubbo分布式框架

一个简单的案例带你入门Dubbo分布式框架

作者头像
江南一点雨
发布2018-04-02 15:41:50
6120
发布2018-04-02 15:41:50
举报
文章被收录于专栏:玩转JavaEE玩转JavaEE玩转JavaEE

相信有很多小伙伴都知道,dubbo是一个分布式、高性能、透明化的RPC服务框架,提供服务自动注册、自动发现等高效服务治理方案,dubbo的中文文档也是非常全的,中文文档可以参考这里dubbo.io。由于官网的介绍比较简洁,我这里打算通过Maven多模块工程再给小伙伴们演示一下用法。


环境:IntelliJ IDEA2017.1


关于如何在IntelliJ IDEA中创建Maven多模块项目,小伙伴们可以参考之前的博客 IntelliJ IDEA中创建Web聚合项目(Maven多模块项目) ,这里我就不再赘述。 这里我还是以dubbo官方文档中的例子作为基准,我们来详细的看看运行过程。

创建一个Maven工程

IntelliJ中创建Maven工程的方式我这里就不再多说了,这里只说一点,工程创建成功之后,将src目录删除,因为我们不需要在这个工程下面写代码,我们将以这个工程为父工程,然后给它创建多个模块。

向创建好的工程中添加模块

当我们第一步成功创建了要给Maven工程之后,第二步我们就向这个Maven工程中添加三个模块,分别是common,provider和consumer三个模块,添加完成之后效果如下:

provider将作为我们的服务提供者,consumer将作为服务消费者,这两个好理解,除了这两个之外我们还需要要给common模块,common模块主要是提供公共接口,供服务提供者和服务消费者使用。

向common模块中添加接口

在common模块中,添加一个SayHello接口,如下:

provider模块依赖common并提供服务

1.首先打开provider的pom.xml文件,在其中添加依赖,要添加的依赖有如下四个小类:

1.添加对common模块的依赖 2.添加对spring的依赖 3.添加对dubbo的依赖 4.添加对zookeeper的依赖

如下:

<dependencies>
        <dependency>
            <groupId>org.sang</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>netty</artifactId>
                    <groupId>org.jboss.netty</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>

然后在provider中实现common模块的接口,如下:

public class SayHelloImpl implements SayHello {
    public String sayHello(String name) {
        return "Hello "+name;
    }
}

然后我们需要在provider的spring配置文件中暴露服务,如下:

<?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-world-app"></dubbo:application>
    <!--<dubbo:registry address="multicast://224.5.6.7:2181"/>-->
    <dubbo:registry address="zookeeper://192.168.248.128:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:service interface="org.sang.SayHello" ref="sayHelloImpl"/>
    <bean id="sayHelloImpl" class="org.sang.SayHelloImpl"/>
</beans>

这里我采用了dubbo推荐的注册中心zookeeper,关于Linux上zookeeper的安装小伙伴们可以参考Linux上安装Zookeeper以及一些注意事项。 注册地址就是你安装zookeeper的服务器地址,然后将服务的接口暴露出来即可。

最后我们采用一个main方法将provider跑起来,如下:

public class Main {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ctx.start();
        System.in.read();
    }
}

OK,如此之后我们的provider模块就算开发完成了。

在consumer模块中消费服务

首先在consumer模块中添加相关依赖,要依赖的东西和provider的依赖一样,这里我就不再重复贴出代码。 然后我们在consumer的spring配置文件中订阅服务,订阅方式如下:

<?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="consumer-of-helloworld-app"/>
    <!--<dubbo:registry address="multicast://224.5.6.7:2181" check="false"/>-->
    <dubbo:registry address="zookeeper://192.168.248.128:2181" check="false"/>
    <dubbo:reference id="sayHello" interface="org.sang.SayHello" check="false"/>
</beans>

首先订阅地址依然是zookeeper的地址,然后注册一个SayHello的bean,这个bean可以直接在我们的工程中使用。 一样,我们还是通过一个main方法来启动服务消费端:

public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SayHello sayHello = (SayHello) ctx.getBean("sayHello");
        String s = sayHello.sayHello("张三");
        System.out.println(s);
    }
}

运行结果如下:

Ok,至此,一个简单的案例就完成了,有问题欢迎小伙伴留言讨论。

案例下载:http://pan.baidu.com/s/1kVR30Rd

参考资料:http://dubbo.io

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-08-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 江南一点雨 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建一个Maven工程
  • 向创建好的工程中添加模块
  • 向common模块中添加接口
  • provider模块依赖common并提供服务
  • 在consumer模块中消费服务
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档