• 【Dubbo】Dubbo+ZK基础入门以及简单demo


    参考文档:

    官方文档:http://dubbo.io/

    duboo中文:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/background.html

     

    1、是什么:

    Dubbo |ˈdʌbəʊ| is a high-performance, java based RPC framework open-sourced by Alibaba. As in many RPC systems, dubbo is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a dubbo server to handle client calls. On the client side, the client has a stub that provides the same methods as the server.

    大体意思:Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,特点是:可以和Spring框架无缝集成。。。。

    首先说说ZK是做什么的:

            ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

            在dubbo中,dubbo的服务提供者会在zookeeper上面创建一个临时节点,表明自己的ip和端口,当消费者需要使用服务时,会先在zookeeper上面查询,找到服务提供者,做一些负载的选择(比如随机、轮流),然后按照这些信息,访问服务提供者。

    ZK的安装:https://blog.csdn.net/the_fool_/article/details/80774500

           

    2、官方文档中的解释:

             场景1、当服务越来越多时,服务 URL 配置管理变得非常困难,F5 硬件负载均衡器的单点压力也越来越大。

          此时需要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。并通过在消费方获取服务提供方地址列表,实现软负载均衡和 Failover,降低对 F5 硬件负载均衡器的依赖,也能减少部分成本。

           场景2、当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系.

           这时 需要自动画出应用间的依赖关系图,以帮助架构师理清理关系

           场景3、接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?

              为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容                                     

    =================================================================================================

     

    3、架构(这个图刚看时不是很理解,但是结合组件来看还是很清晰的):


    ==========================================================================

    4、官方demo:

    1、项目结构:

    2、导包(pom.xml):

    <!--
     - Copyright 1999-2011 Alibaba Group.
     -  
     - Licensed under the Apache License, Version 2.0 (the "License");
     - you may not use this file except in compliance with the License.
     - You may obtain a copy of the License at
     -  
     -      http://www.apache.org/licenses/LICENSE-2.0
     -  
     - Unless required by applicable law or agreed to in writing, software
     - distributed under the License is distributed on an "AS IS" BASIS,
     - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     - See the License for the specific language governing permissions and
     - limitations under the License.
    -->
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo-demo</artifactId>
            <version>2.5.8</version>
        </parent>
        <artifactId>dubbo-demo-provider</artifactId>
        <packaging>jar</packaging>
        <name>${project.artifactId}</name>
        <description>The demo provider module of dubbo project</description>
        <properties>
            <skip_maven_deploy>false</skip_maven_deploy>
        </properties>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo-demo-api</artifactId>
                <version>${project.parent.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${project.parent.version}</version>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.4</version>
            </dependency>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.0.35.Final</version>
            </dependency>
        </dependencies>
    </project>

    3、生产者:

    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();
        }
    
    }

    提供服务:

    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.in.read(); // press any key to exit
        }
    
    }

    配置文件dubbo-demo-provider.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
     - Copyright 1999-2011 Alibaba Group.
     -  
     - Licensed under the Apache License, Version 2.0 (the "License");
     - you may not use this file except in compliance with the License.
     - You may obtain a copy of the License at
     -  
     -      http://www.apache.org/licenses/LICENSE-2.0
     -  
     - Unless required by applicable law or agreed to in writing, software
     - distributed under the License is distributed on an "AS IS" BASIS,
     - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     - See the License for the specific language governing permissions and
     - limitations under the License.
    -->
    <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">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="demo-provider"/>
    
        <!-- 使用multicast广播注册中心暴露服务地址 -->
        <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
        <!-- 这是我自己的注册中心,安装启动即可,dubbo会自动创建节点。-->
        <dubbo:registry address="zookeeper://192.168.0.17:2181" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880"/>
    
        <!-- 和本地bean一样实现服务 -->
        <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
    
    </beans>

    4、消费者:

    public class Consumer {
    
        public static void main(String[] args) {
            //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-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();
                }
    
    
            }
    
        }
    }

    配置文件dubbo-demo-consumer.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
     - Copyright 1999-2011 Alibaba Group.
     -  
     - Licensed under the Apache License, Version 2.0 (the "License");
     - you may not use this file except in compliance with the License.
     - You may obtain a copy of the License at
     -  
     -      http://www.apache.org/licenses/LICENSE-2.0
     -  
     - Unless required by applicable law or agreed to in writing, software
     - distributed under the License is distributed on an "AS IS" BASIS,
     - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     - See the License for the specific language governing permissions and
     - limitations under the License.
    -->
    <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">
    
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="demo-consumer"/>
    
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
        <!-- 去我自己安装的注册中心索取服务 -->
        <dubbo:registry address="zookeeper://192.168.0.17:2181" />
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
    
    </beans>

    与cloud的区别?如何选择?

    个人观点:博主了解的并不深入,个人觉得springcloud更加全面,使用起来更顺手一点。等熟练用过后会考虑做个对比。

     
     
  • 相关阅读:
    67家基金子公司背景脉络梳理
    港股奇葩术语知多少
    68家信托公司7大派系股东分食图谱
    港股术语
    流动性陷阱
    ORACLE归档模式和非归档模式的利与弊
    肥尾效应
    DVP
    金融衍生品如何定价
    绿鞋机制
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054195.html
Copyright © 2020-2023  润新知