搭建简单的Dubbo实例

简单的Dubbo框架搭建过程,包括Dubbo所依赖的zookeeper安装。

一.安装zookeeper

1.下载zookeeper的安装包

下载地址为:https://mirrors.cnnic.cn/apache/zookeeper/

可从链接内选择一个合适的版本下载,我所使用的版本是zookeeper-3.4.10

2.解压下载的压缩包,修改配置文件

修改目录下zookeeper-3.4.10confzoo_sample.cfg文件内容,并将文件名修改为zoo.cfg。

实际上只需要修改dataDir和dataLogDir两个地方即可,dataDir为Zookeeper 保存数据的目录,dataLogDir为Zookeeper 保存日志文件的目录。

以下为我的配置文件,可以参考。

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 需要更改的地方
dataDir=W:/Dubbo/zookeeper/data
dataLogDir=W:/Dubbo/zookeeper/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
3.启动zookeeper

找到目录zookeeper-3.4.10in,双击zkServer.cmd,直接运行;这种方法遇到问题会闪退。

打开cmd,进入到zookeeper-3.4.10in目录下,运行zkServer.cmd。

4.启动zookeeper时的一些问题。

若双击zkServer.cmd出现闪退,可以使用第二种启动方法,或者在编辑zkServer.cmd在文末添加pause,来查找问题。

1.报错,缺失zoo.cfg

这个错是因为没有将conf/zoo_sample.cfg改名为zoo.cfg,改名之后即可解决。

2.JAVA_HOME没有设置

需要在环境变量的用户变量里添加JAVA_HOME,添加方法就不赘述了。

二.创建maven项目

1.一共分三个模块,每个模块都有独立的pom文件。

稍微说明一下三个模块的作用。api内为公用接口,consumer调用远程服务,provider提供远程服务。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BMYEWiGU-1661829592016)(//img-blog.csdn.net/20180320140927427?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2xqbHNibG9n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)]

2.在consumer和provider的pom中添加依赖

????<dependencies>
    	<dependency>
    		<groupId>com.alibaba</groupId>
    		<artifactId>dubbo</artifactId>
    		<version>2.6.0</version>
    	</dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.9</version>
      ??</dependency>
????</dependencies>

3.在dubbo-demo-api内添加接口

public interface DemoService {

    String sayHello(String name);

}

4…在dubbo-demo-provider内实现接口

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }

}

修改配置文件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O4yiHYSO-1661829592017)(//img-blog.csdn.net/20180320142939684?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2xqbHNibG9n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)]

配置文件的内容为:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>

    <!-- use multicast registry center to export service 
    <dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

</beans>

启动服务,注意要先启动zookeeper

public class Provider {

    public static void main(String[] args) throws Exception {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
//        System.setProperty("java.net.preferIPv4Stack", "true");         
    	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.out.println("----------------服务已启动,按任意键结束···········--------------------");
        System.in.read(); // press any key to exit
    }

}

5.在dubbo-demo-consumer内调用服务

public class Consumer {

    public static void main(String[] args) {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy

        while (true) {
            try {
                Thread.sleep(1000);
                String hello = demoService.sayHello("world"); // call remote method
                System.out.println(hello); // get result

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

        }

    }
}

修改配置文件

下面是配置文件内容,注意registry的地址,按照需求修改。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/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-2.5.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>

    <!-- use multicast registry center to discover service 
    <dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>

</beans>

三.运行项目

先启动zookeeper,启动成功后启动provider,最后启动consumer

运行结果部分截图如下:

我只是写了一下我的搭建过程,如果想要更详细的了解请去看大神写的博文。

参考:http://blog.csdn.net/noaman_wgs/article/details/70214612/

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值