Dubbo生产者和消费者

Posted 听风忆雪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo生产者和消费者相关的知识,希望对你有一定的参考价值。

生产者

引入依赖

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javaee</groupId>
      <artifactId>javaee-api</artifactId>
      <version>5</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.22</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.2.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.1.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.1.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.9.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.2</version>
    </dependency>

    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>

    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.17.1-GA</version>
    </dependency>

    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>2.2.2</version>
    </dependency>

    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.31</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
      <exclusions>
        <exclusion>
          <artifactId>spring</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.6</version>
      <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>

service接口

public interface DoSomeService {
    public String doSome();
}

service实现类

@Service("doSomeService")
public class DoSomeServiceImpl implements DoSomeService {
    @Override
    public String doSome() {
        System.out.println("生产者提供的服务方法");
        return "provider";
    }
}

配置

<context:component-scan base-package="com.dubbo"/>

    <!--声明服务提供方-->
    <dubbo:application name="provider"/>
    <!--注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--dubbo服务端口-->
    <dubbo:protocol name="dubbo" port="20880"/>


    <!--服务注册-->
    <dubbo:service interface="com.dubbo.service.DoSomeService" ref="doSomeService"/>

测试类

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

        System.out.println("服务已经发布!!!!!");
        //阻塞

        System.in.read();
    }
}

 消费者

service接口

public interface DoSomeService {
    public String doSome();
}

配置

    <context:component-scan base-package="com.dubbo"/>

    <!--声明服务提供方-->
    <dubbo:application name="consumer"/>
    <!--注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>



    <!--服务消费-->
    <dubbo:reference interface="com.dubbo.service.DoSomeService" id="doSomeService"/>

测试类

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


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

        UserService userService = (UserService)ctx.getBean("userService");
        userService.userServicePro();
    }
}

 

 

 

 

 

 

以上是关于Dubbo生产者和消费者的主要内容,如果未能解决你的问题,请参考以下文章

dubbo中拦截生产者或消费者服务方法调用

dubbo生产者和消费者

[Dubbo开发]Dubbo日志插件实现(未打包)

[dubbo学习] 简单的dubbo功能

[dubbo学习] 简单的dubbo功能

Dubbo生产者和消费者