Dubbo入门

作者: AmeeLove | 来源:发表于2018-04-10 10:19 被阅读21次

    Dubbo

    Dubbo是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

    Dubbo简介

    1.1、Dubbo是什么?

        Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbo这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架
    

    其核心部分包含:

      1. 远程通讯: 提供对多种基于长连接的NIO框架抽象封装,包括多种线程模型,序列化,以及“请求-响应”模式的信息交换方式。
      1. 集群容错: 提供基于接口方法的透明远程过程调用,包括多协议支持,以及软负载均衡,失败容错,地址路由,动态配置等集群支持。
      1. 自动发现: 基于注册中心目录服务,使服务消费方能动态的查找服务提供方,使地址透明,使服务提供方可以平滑增加或减少机器。

    1.2. Dubbo能做什么?

    • 1.透明化的远程方法调用,就像调用本地方法一样调用远程方法,只需简单配置,没有任何API侵入。
    • 2.软负载均衡及容错机制,可在内网替代F5等硬件负载均衡器,降低成本,减少单点。
      1. 服务自动注册与发现,不再需要写死服务提供方地址,注册中心基于接口名查询服务提供者的IP地址,并且能够平滑添加或删除服务提供者。

    1.3. dubbo的架构

    dubbo架构图如下所示:

    dubbo-degin
    节点角色说明:
    • Provider: 暴露服务的服务提供方。
    • Consumer: 调用远程服务的服务消费方。
    • Registry: 服务注册与发现的注册中心。
    • Monitor: 统计服务的调用次调和调用时间的监控中心。
    • Container: 服务运行容器。
    调用关系说明:
    • 0 服务容器负责启动,加载,运行服务提供者。
      1. 服务提供者在启动时,向注册中心注册自己提供的服务。
      1. 服务消费者在启动时,向注册中心订阅自己所需的服务。
      1. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
      1. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
      1. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

    1.4. dubbo使用方法

    Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。如果不想使用Spring配置,而希望通过API的方式进行调用(不推荐)
    Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。

    提供者

    image.png

    service

    package com.ghgcn.dubbo_provide.service;
    /**
     * 服务提供者接口
     * @author Administrator
     *
     */
    public interface ProviderService {
        /**
         * 说话方法
         * @param name
         * @return
         */
        public String sayHello(String name);
    }
    
    
    package com.ghgcn.dubbo_provide.service.imp;
    
    import com.ghgcn.dubbo_provide.service.ProviderService;
    
    public class ProviderServiceImp implements ProviderService {
    
        public String sayHello(String name) {
            
            return "Hello "+name+" 好好了 !";
        }
    
    }
    
    

    pom

    <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>com.ghgcn</groupId>
        <artifactId>dubbo_provide</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <spring.version>4.3.12.RELEASE</spring.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <jdk.version>1.8</jdk.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.7</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    
        <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.9</version>
            </dependency> 
            <!-- <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency> -->
    
            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.2</version>
                        <configuration>
                            <source>${jdk.version}</source>
                            <target>${jdk.version}</target>
                            <encoding>${project.build.sourceEncoding}</encoding>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.6</version>
                    </plugin>
                </plugins>
            </pluginManagement>
    
        </build>
    </project>
    

    xml

    spring.xml

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
       <context:annotation-config/>
       <!-- <aop:aspectj-autoproxy /> -->
         <context:component-scan base-package="com.ghgcn.dubbo_provide">
         </context:component-scan>
        <import resource="dubbo_provider.xml"/>
       </beans>
    

    dubbo_provider.xml

    <?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" />
        <!-- 使⽤multicast⼴播注册中⼼暴露服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
        <!-- ⽤dubbo协议在20880端⼝暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
        <!-- 使用zookeeper注册中心暴露服务地址 -->
         <dubbo:registry address="zookeeper://127.0.0.1:2181" /> 
        <!-- 声明需要暴露的服务接⼝ -->
        <dubbo:service interface="com.ghgcn.dubbo_provide.service.ProviderService"
            ref="providerService" />
        <!-- 和本地bean⼀样实现服务 -->
        <bean id="providerService" class="com.ghgcn.dubbo_provide.service.imp.ProviderServiceImp" />
    </beans>
    

    测试类

    package com.ghgcn.dubbo_provide;
    
    import java.io.IOException;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] {"spring.xml"});    //,"dubbo_provider.xml"
                context.start();   
                System.out.println("提供者服务已注册成功");    
                System.out.println("请按任意键取消提供者服务");    
                try {  
                    System.in.read();//让此程序一直跑,表示一直提供服务  
                } catch (IOException e) {         
                    e.printStackTrace();  
                }    
    
        }
    
    }
    
    
    image.png

    不要关闭


    image.png image.png

    消费者

    image.png

    pom

    <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>com.ghgcn</groupId>
        <artifactId>dubbo_consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <properties>
            <spring.version>4.3.12.RELEASE</spring.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <jdk.version>1.8</jdk.version>
        </properties>
        <dependencies>
    
            <dependency>
                <groupId>com.ghgcn</groupId>
                <artifactId>dubbo_provide</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.7</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    
            <dependency>
    
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.9</version>
            </dependency>
            <!-- <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> 
                <version>0.1</version> </dependency> -->
    
            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.2</version>
                        <configuration>
                            <source>${jdk.version}</source>
                            <target>${jdk.version}</target>
                            <encoding>${project.build.sourceEncoding}</encoding>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>2.6</version>
                    </plugin>
                </plugins>
            </pluginManagement>
    
        </build>
    </project>
    
    

    dubbo-client.xml

    <?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="dubbo_consumer" />
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
        <dubbo:consumer timeout="5000"
            check="false" />
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="providerService" interface="com.ghgcn.dubbo_provide.service.ProviderService" />   
        
    </beans> 
    

    测试

    package com.ghgcn.dubbo_consumer;
    
    import java.io.IOException;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.ghgcn.dubbo_provide.service.ProviderService;
    
    public class ConsumerServiceTest {
    
        @SuppressWarnings("resource")
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                    new String[] { "dubbo-client"
                            + ".xml" });  
              
            context.start();  
            ProviderService providerService = (ProviderService) context.getBean("providerService");  
            
            System.out.println(providerService.sayHello("刘楠  ooo"));  
            System.out.println(providerService.sayHello("张三"));  
            
            System.out.println(providerService.sayHello("QQ"));  
            System.out.println(providerService.sayHello("RRR"));  
            System.out.println("==============执行完成===================");    
            try {  
                System.in.read();  
            } catch (IOException e) {         
                e.printStackTrace();  
            }    
        }
    }
    
    

    结果

    image.png image.png

    相关文章

      网友评论

        本文标题:Dubbo入门

        本文链接:https://www.haomeiwen.com/subject/xmsghftx.html