zoukankan      html  css  js  c++  java
  • dubbox

    Dubbox是什么

    Dubbox是一个分布式服务框架,前身是阿里巴巴的开源项目Dubbo,后来阿里不再维护此框架;进而当当网进行了进一步维护,为了和Dubbo区分就取名为Dubbox。

    简单而言,在Dubbox中主要存在三种角色:注册中心(Registry)、 提供者(Provider)、消费者(Customer)。

    而作为分布式框架之一的Dubbox就能够实现消费方和提供方之间的远程调用,即对分别部署在不同服务器端的服务提供了一个相互交互的平台。

      

      节点角色说明:   

          Provider: 暴露服务的提供方

          Consumer: 调用远程服务的服务消费方

          Registry: 服务注册与发现的注册中心

          Monitor: 统计服务调用次数和调用时间的监控中心

          Container: 服务运行容器

      调用关系说明: 

          服务容器负责启动、加载,运行服务提供者

          服务提供者在启动时,向注册中心注册自己提供的服务

          服务消费者在启动时,向注册中心订阅自己所需的服务

          注册中心返回返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

          服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台

          服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

    Dubbox实例

      生产者

        导入依赖:

    <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.8.4</version>
            </dependency>
            <!-- 添加zk客户端依赖 -->
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>3.0.7.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-client</artifactId>
                <version>3.0.7.Final</version>
            </dependency>
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>1.0.0.GA</version>
            </dependency>
    
            <!-- 如果要使用json序列化 -->
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jackson-provider</artifactId>
                <version>3.0.7.Final</version>
            </dependency>
    
            <!-- 如果要使用xml序列化 -->
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxb-provider</artifactId>
                <version>3.0.7.Final</version>
            </dependency>
    
            <!-- 如果要使用netty server -->
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-netty</artifactId>
                <version>3.0.7.Final</version>
            </dependency>
    
            <!-- 如果要使用Sun HTTP server -->
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jdk-http</artifactId>
                <version>3.0.7.Final</version>
            </dependency>
    
            <!-- 如果要使用tomcat server -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>8.0.11</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-logging-juli</artifactId>
                <version>8.0.11</version>
            </dependency>
            <dependency>
                <groupId>com.esotericsoftware.kryo</groupId>
                <artifactId>kryo</artifactId>
                <version>2.24.0</version>
            </dependency>
            <dependency>
                <groupId>de.javakaffee</groupId>
                <artifactId>kryo-serializers</artifactId>
                <version>0.26</version>
            </dependency>
            <dependency>
                <groupId>de.ruedigermoeller</groupId>
                <artifactId>fst</artifactId>
                <version>1.55</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.3.3</version>
            </dependency>
            <dependency>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty</artifactId>
                <version>7.0.0.pre5</version>
            </dependency>
        </dependencies>

        接口:

    package com.dubbox.service;
    
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.core.MediaType;
    
    @Path("/doService")
    public interface DoService {
    
        @GET
        @Path("/getOne/{username}")
        @Consumes({MediaType.APPLICATION_JSON})
        public String getOne(@PathParam("username") String username);
    
    }

        接口实现类:

    package com.dubbox.serviceimpl;
    
    import com.dubbox.service.DoService;
    
    public class DoServiceImpl implements DoService {
    
        @Override
        public String getOne(String username) {
            System.out.println("你是傻子!!!"+username);
            return "dubbox";
        }
    }

        配置文件:

    <?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="dubbox-provider"/>
        <!--注册中心地址-->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
        <!--dubbo服务端口-->
        <dubbo:protocol name="rest" port="20888"/>
    
        <!--服务注册-->
        <dubbo:service interface="com.dubbox.service.DoService" ref="doService"/>
    
        <bean id="doService" class="com.dubbox.serviceimpl.DoServiceImpl"/>
    
    </beans>

        测试:

    public class TestDo {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            context.start();
            System.out.println("生产者服务已经注册成功!");
            try {
                System.in.read();//让此程序一直跑,表示一直提供服务
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

        结果:

              

      消费者

        导入依赖:

          依赖与生产者相同;

        接口:

    package com.dubbox.service;
    
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.core.MediaType;
    
    @Path("/doService")
    public interface DoService {
    
        @GET
        @Path("/getOne/{username}")
        @Consumes({MediaType.APPLICATION_JSON})
        public String getOne(@PathParam("username") String username);
    
    }

        配置文件:

    <?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"
           xmlns:context="http://www.springframework.org/schema/context"
           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
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
    
        <!--声明服务提供方-->
        <dubbo:application name="dubbox-consumer"/>
        <!--注册中心地址-->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
        <!--服务消费-->
        <dubbo:reference interface="com.dubbox.service.DoService" id="doService"/>
    
    </beans>

        测试:

    package com.dubbox.test;
    
    import com.dubbox.service.DoService;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestDo {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
            DoService doService = (DoService)context.getBean("doService");
            doService.getOne("呵呵");
        }
    }

        结果:

          

  • 相关阅读:
    【L.M.W.Y.D】Scrum Meeting 2
    【L.M.W.Y.D】Scrum Meeting 1
    L.M.W.Y.D 实验八 团队作业4—团队项目需求建模与系统设计
    L.M.W.Y.D 实验七 团队作业3:团队项目需求分析与原型设计
    L.M.W.Y.D 实验六 团队作业2:健康管理系统
    L.M.W.Y.D 实验五 团队作业1:软件研发团队组建与软件案例分析
    多喝热水 [Alpha] Scrum Meeting 3
    多喝热水 [Alpha] Scrum Meeting 2
    多喝热水 [Alpha] Scrum Meeting 1
    多喝热水 实验八 团队作业4:团队项目需求建模与系统设计
  • 原文地址:https://www.cnblogs.com/wnwn/p/12011681.html
Copyright © 2011-2022 走看看