zoukankan      html  css  js  c++  java
  • 中间件——dubbo

    DUBBO初探-搭建DUBBO开发环境

    我所理解的DUBBO

    相对于传统web开发框架,dubbo更加适合于并行系统开发,分布式,模块化。将server和client都注册到zookeeper注册中心上,然后由最外层客户端发起请求到相应client上,client再调用server。所谓模块化,举例说明,将一个电商系统分隔成用户,商品,进销存等模块,不同的服务模块,client同样也可以这样区分,这样分布完成之后,我们还可以建立多个相同的client端,用nginx来进行相应的集群服务。这也就是我所理解的nginx+dubbo的集群分布式系统

    一副自己理解的请求图

    这里写图片描述

    安装zookeeper

    zookeeper下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/ 
    启动之前将/home/conf下的zoo_sample.cfg 改名为 zoo.cfg

    zoo.cfg解析:

    tickTime:这个时间是作为Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。 
    •dataDir:顾名思义就是 Zookeeper保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。 
    •dataLogDir:顾名思义就是Zookeeper 保存日志文件的目录 
    •clientPort:这个端口就是客户端连接Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求

    相关服务
    1. 启动ZK服务: sh bin/zkServer.sh start
    2. 查看ZK服务状态: sh bin/zkServer.sh status
    3. 停止ZK服务: sh bin/zkServer.sh stop
    4. 重启ZK服务: sh bin/zkServer.sh restart

    server code

    public interface DubboInter {
        public String saveUsers();
        public void hgetUsers();
    }
    
    
    public class DubboInterImpl implements DubboInter{
        @Override
        public String saveUsers() {
            System.out.println("save user");
            return "test";
        }
    
        @Override
        public void hgetUsers() {
            System.out.println("dubbo getUsers");
        }
    }
    
    public class Provider {
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "spring-mvc.xml" });
            context.start();
            System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    <?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
            ">
        <!--具体的实现bean-->
        <bean id="dubboService" class="com.dubbo.imp.DubboInterImpl"></bean>
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="xs_provider" />
        <!-- 使用zookeeper注册中心暴露服务地址 即zookeeper的所在服务器ip地址和端口号 -->
        <dubbo:registry address="zookeeper://ip:port" />
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.dubbo.inter.DubboInter"
                       ref="dubboService" />
    </beans>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    client code

    public interface DubboInter {
        public String saveUsers();
        public void hgetUsers();
    }
    
    public class Customer {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "spring-mvc.xml" });
            context.start();
            DubboInter dubboInter = (DubboInter) context.getBean("dubboService");
            String aa = dubboInter.saveUsers();
            System.out.println(aa);
            System.in.read();
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    <?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="hjy_consumer" />
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://ip:port" />
        <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
        <dubbo:reference id="dubboService"
                         interface="com.dubbo.inter.DubboInter" />
    </beans>
    • 1
    • 2
    • 3 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    简单说明

    服务端编写服务接口和实现类,并且将实现类暴露出去,将zookeeper启动,然后运行服务端,将服务按照配置文件的配置注册到zookeeper上面,客户端为接口声明类,配置文件以及调用代码,客户端同样从zookeeper上调用相应的服务。整个流程就是这样了。

    dubbo控制台(该war包兼容jdk8)

    下载地址:http://pan.baidu.com/s/1eRB6YhO 
    配置文件在内部的dubbo.properties,可以配置root和guest的密码。 
    效果如下: 
    这里写图片描述

    源码地址

    http://pan.baidu.com/s/1kVS7XIn

  • 相关阅读:
    运行.bat批处理,CMD窗口隐藏,并制作为EXE文件
    TinyXML:一个优秀的C++ XML解析器(转载)
    2013编程之美资格赛【传话游戏】
    linux GTK教程(消息机制/标签/按钮/图像/文本/对话框/菜单/容器)
    c++强制类型转换(总结)
    string与char*的转换(转载)
    网络数据包捕获函数库Libpcap安装与使用(非常强大)
    Linux 高级Socket编程
    linux GTK 安装
    .dll和.lib文件的生成和使用 c++
  • 原文地址:https://www.cnblogs.com/bestmoment/p/9761464.html
Copyright © 2011-2022 走看看