其他分享
首页 > 其他分享> > dubbox

dubbox

作者:互联网

什么是Dubbox:

Dubbo是一个被国内很多互联网公司广泛使用的开源分布式服务框架,即使从国际视野来看应该也是一个非常全面的SOA基础框架。作为一个重要的技术研究课题,在当当网根据自身的需求,为Dubbo实现了一些新的功能,并将其命名为Dubbox(即Dubbo eXtensions)。

主要的新功能包括:

支持REST风格远程调用(HTTP + JSON/XML):基于非常成熟的JBoss RestEasy框架,在dubbo中实现了REST风格(HTTP + JSON/XML)的远程调用,以显著简化企业内部的跨语言交互,同时显著简化企业对外的Open API、无线API甚至AJAX服务端等等的开发。事实上,这个REST调用也使得Dubbo可以对当今特别流行的“微服务”架构提供基础性支持。 另外,REST调用也达到了比较高的性能,在基准测试下,HTTP + JSON与Dubbo 2.x默认的RPC协议(即TCP + Hessian2二进制序列化)之间只有1.5倍左右的差距,详见下文的基准测试报告。

上面很多功能已在当当网内部稳定的使用,现在开源出来,供大家参考和指正。也希望感兴趣的朋友也来为Dubbo贡献更多的改进。

注:dubbox和dubbo 2.x是兼容的,没有改变dubbo的任何已有的功能和配置方式(除了升级了Spring之类的版本)。

 

在idea中创建一个简单的dubbox案例:

所需的依赖:

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    //这个依赖是自己创建的,在当当网中下载dubbox的包,然后导入到本地仓库
    <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>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.5.RELEASE</version>
      <scope>test</scope>
    </dependency>    

 

provider端:

service层:

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("/dosomeService")
public interface DoSomeService {


    @Path("/doSome/{userName}")
    @GET
    @Consumes({ MediaType.APPLICATION_JSON })
    public String doSome(@PathParam("userName") String userName);
}

serviceimpl层:

package com.dubbox.service.impl;


import com.dubbox.service.DoSomeService;

public class DoSomeServiceImpl implements DoSomeService {
    @Override
    public String doSome(String userName) {
        System.out.println("dubbox 发布的DoSomeService 服务   doSome方法\t"+userName);
        return "bubbox";
    }
}


applicationContext-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"
       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-provider"/>
    <!--注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--dubbo服务端口-->
    <dubbo:protocol name="rest" port="8081"/>


    <!--服务注册-->
    <dubbo:service interface="com.dubbox.service.DoSomeService" ref="doSomeService"/>
    <bean id="doSomeService" class="com.dubbox.service.impl.DoSomeServiceImpl"/>




</beans>





启动服务:
package com.dubbox;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    public static void main(String[] args) throws IOException {
        //加载配置文件:配置文件中通过SPring将Dubbo服务注册到注册中心当中去
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-provider.xml");

        System.out.println("dubbox服务已经发布!!!!!");

        //阻塞
        System.in.read();
    }
}

 

consumer层:

 

service层:

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("/dosomeService")
public interface DoSomeService {


    @Path("/doSome/{userName}")
    @GET
    @Consumes({ MediaType.APPLICATION_JSON })
    public String doSome(@PathParam("userName") String userName);
}

applicationContext-consumer.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"
       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.DoSomeService" id="doSomeService"/>





</beans>


启动服务:
package com.dubbox;

import com.dubbox.service.DoSomeService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppTest {

        public static void main(String[] args) {
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-consumer.xml");

            DoSomeService doSomeService = (DoSomeService)ctx.getBean("doSomeService");
            doSomeService.doSome("李四");

    }
}

标签:dubbox,org,resteasy,import,com,javax
来源: https://www.cnblogs.com/Chencheno/p/12012530.html