dubbox入门

dubbox定义:

dubbox和dubbo差不多,dubbox由当当网维护,使用http协议和rest编码风格

在maven仓库中添加Doubbox依赖

因为:Maven不支持dubbox直接从中央仓库导入

  1. 我们需要一个dubbox-dubbox.zip(github上拿)

  ① 博客操作指南:

  ② https://blog.csdn.net/try_and_do/article/details/83383861

2.解压后需要将文件打成jar

  ① 命令:Jar -cf 文件.jar 文件

  3.将jar文件导成maven库的依赖

    (将alibaba文件(放入maven库中)替换E:\Maven项目管理模型\maven_re\com\alibaba)

编码

一,导入dubbox依赖(默认从maven库中找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>
      <scope>test</scope>
    </dependency>

1.服务者

service接口

@Path("/dosomeService")
public interface DoSomeService {

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

service实现

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

aplicationContext-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.dubbo.service.DoSomeService" ref="doSomeService"/>
    <bean id="doSomeService" class="com.dubbo.service.impl.DoSomeServiceImpl"/>

</beans>

  Apptest

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

2.消费者

  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.dubbo.service.doSomeService" id="doSomeService"/>





</beans>

  testControll

public class testControll {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-consumer.xml");
        doSomeService doSomeService = (doSomeService)ctx.getBean("doSomeService");
        doSomeService.doSome("李四");
    }
}