消费Dubbo服务介绍

Posted 心和梦的方向

tags:

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

前面我们搞了发布Dubbo服务,发布的服务就是用来消费的,所以我们这里来调用服务,消费下;

 

创建maven项目 dubbo-demo-consumer

pom.xml配置下:

技术分享图片
 1 <dependencies>
 2     <dependency>
 3         <groupId>com.alibaba</groupId>
 4         <artifactId>dubbo</artifactId>
 5         <version>2.6.0</version>
 6     </dependency>
 7     <dependency>
 8         <groupId>com.101tec</groupId>
 9         <artifactId>zkclient</artifactId>
10         <version>0.10</version>
11     </dependency>
12     <dependency>
13         <groupId>org.apache.curator</groupId>
14         <artifactId>curator-framework</artifactId>
15         <version>4.0.1</version>
16     </dependency>
17     <dependency>
18         <groupId>com.alibaba</groupId>
19         <artifactId>fastjson</artifactId>
20         <version>1.2.46</version>
21     </dependency>
22     <dependency>
23         <groupId>log4j</groupId>
24         <artifactId>log4j</artifactId>
25         <version>1.2.17</version>
26     </dependency>
27     <dependency>
28         <groupId>org.slf4j</groupId>
29         <artifactId>slf4j-api</artifactId>
30         <version>1.7.25</version>
31     </dependency>
32     <dependency>
33         <groupId>org.apache.commons</groupId>
34         <artifactId>commons-lang3</artifactId>
35         <version>3.4</version>
36     </dependency>
37     <dependency>
38         <groupId>io.netty</groupId>
39         <artifactId>netty-all</artifactId>
40         <version>4.0.35.Final</version>
41     </dependency>
42 </dependencies>
View Code

再搞个dubbo-demo-consumer.xml

技术分享图片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 4        xmlns="http://www.springframework.org/schema/beans"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 6        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 7  
 8     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
 9     <dubbo:application name="demo-consumer"/>
10  
11     <!-- 使用zookeeper注册中心暴露服务地址 -->
12     <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
13  
14     <!-- 生成远程服务代理,可以和本地bean一样使用demoProviderService  check属性,启动的时候是否检查 一般设置为false 启动的时候不检查-->
15     <dubbo:reference id="demoProviderService" check="false" interface="com.java1234.service.DemoProviderService"/>
16  
17 </beans>
View Code

创建接口DemoProviderService.java

技术分享图片
 1 package com.java1234.service;
 2  
 3 /**
 4  * 服务提供者接口
 5  * @author Administrator
 6  *
 7  */
 8 public interface DemoProviderService {
 9  
10     public String sayHello(String name);
11 }
View Code

搞个测试类ConsumerTest:

技术分享图片
 1 import java.io.IOException;
 2  
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4  
 5 import com.java1234.service.DemoProviderService;
 6  
 7 public class ConsumerTest {
 8  
 9     public static void main(String[] args) {
10         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-demo-consumer.xml"});
11         context.start();
12         DemoProviderService demoProviderService=(DemoProviderService) context.getBean("demoProviderService");
13         String result=demoProviderService.sayHello("你好");
14         System.out.println("远程调用结果:"+result);
15         try {
16             System.in.read();
17         } catch (IOException e) {
18             // TODO Auto-generated catch block
19             e.printStackTrace();
20         }
21         context.close();
22     }
23 }
View Code

运行结果 远程调用结果:服务001

 

说明远程调用成功;

 

 

以上是关于消费Dubbo服务介绍的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo服务提供端与消费端应用的搭建

Dubbo服务者消费者提供者案例实现

Dubbo搭建HelloWorld-搭建服务提供者与服务消费者并完成远程调用(附代码下载)

Dubbo记录-基本原理

SpringCloud 整合 Dubbo

dubbo总结:dubbo介绍