(五)消费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>

再搞个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.wishwzp.service.DemoProviderService"/>
16 
17 </beans>

创建接口DemoProviderService.java

 1 package com.wishwzp.service;
 2 
 3 /**
 4  * 服务提供者接口
 5  * @author Administrator
 6  *
 7  */
 8 public interface DemoProviderService {
 9 
10     public String sayHello(String name);
11 }

搞个测试类ConsumerTest:

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

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

 

说明远程调用成功;

 

posted @ 2018-08-08 10:05  kika  阅读(210)  评论(0编辑  收藏  举报