Dubbox 基本特性之泛化调用

Dubbo 是支持泛化调用的,什么是泛化调用呢,泛化调用的好处是什么呢,泛化调用说白一点就是服务消费者并没有服务的接口,学了前面几章的内容之后,你肯定会发现,在我们开发写Demo的时候,必做的一件事情,就是在服务消费者和服务提供者两端同路径下有同样的接口,只不过在服务提供者端会有该接口的具体实现,之所以在服务消费者有一个没有任何具体实现的接口,是因为在设计RPC之初,设计者的最高理念就是你去面向接口编程,你在进行远程调用的时候,并没有意识到你在进行远程调用,却也能拿到接口一样,相信你也感觉到了,服务消费者在调用服务的时候,与调用一个普通的接口是一样的。

  泛化调用就是服务消费者端因为某种原因并没有该服务接口,这个原因有很多,比如是跨语言的,一个PHP工程师想调用某个java接口,他并不能按照你约定,去写一个个的接口,Dubbo并不是跨语言的RPC框架,但并不是不能解决这个问题,这个PHP程序员搭建了一个简单的java web项目,引入了dubbo的jar包,使用dubbo的泛化调用,然后利用web返回json,这样也能完成跨语言的调用。泛化调用的好处之一就是这个了

  好了,简而言之,泛化调用,最最直接的表现就是服务消费者不需要有任何接口的实现,就能完成服务的调用。

 

3.5.1 泛化调用入门

 

老规矩,我们现在服务提供者端定义一个简单的接口,(与往常不一样的地方就是不需要把该接口复制到服务提供者的同路径下了,因为服务消费者不需要使用该接口进行调用),好了,定义一个简单的接口EasyCommonService.java

[java]   view plain  copy
  1. package org.bazinga.service;  
  2.   
  3. public interface EasyCommonService {  
  4.       
  5.     public String helloService(String name);  
  6.   
  7. }  

该接口的具体实现EasyCommonServiceImpl.java

[java]   view plain  copy
  1. package org.bazinga.service.impl;  
  2.   
  3. import org.bazinga.service.EasyCommonService;  
  4.   
  5. public class EasyCommonServiceImpl implements EasyCommonService {  
  6.   
  7.     public String helloService(String name) {  
  8.         System.out.println("name is "+ name);  
  9.         return "hello " + name;  
  10.     }  
  11.   
  12. }  

服务提供者端的Spring的配置文件spring-dubbo-provider-generic.xml

[html]   view plain  copy
  1. <?xml version="1.1" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.        http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.        http://code.alibabatech.com/schema/dubbo    
  7.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.          
  9.     <dubbo:application owner="lyncc" name="bazinga-app" />  
  10.     <!--zookeeper注册中心 -->  
  11.     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>   
  12.       
  13.     <dubbo:protocol name ="dubbo" port="20880" />  
  14.       
  15.     <bean id="easyCommonService" class="org.bazinga.service.impl.EasyCommonServiceImpl" />  
  16.     <dubbo:service  interface="org.bazinga.service.EasyCommonService" ref="easyCommonService" />  
  17.       
  18. </beans>  

服务提供者端再写一个启动类DubboxProviderGenericService.java:

[html]   view plain  copy
  1. package org.bazinga.service.test;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. public class DubboxProviderGenericService {  
  6.       
  7.     public static void main(String[] args) throws InterruptedException {  
  8.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  9.                 "spring-dubbo-provider-generic.xml");  
  10.         context.start();  
  11.         Thread.sleep(2000000l);  
  12.     }  
  13.   
  14. }  

好了,到此为止,泛化调用服务提供者的配置文件就全部结束了,你可以看到从代码和spring的配置文件都没有任何特殊处理,与一般的服务提供者并没有任何的不同,泛化调用,从这四个字看也知道是调用的方式不一样而已,我们再看看服务消费者端的代码编写

  因为服务消费者没有了接口,我们直接编写消费者端spring的配置文件spring-dubbo-consumer-generic.xml

[html]   view plain  copy
  1. <?xml version="1.1" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.        http://www.springframework.org/schema/beans/spring-beans.xsd    
  6.        http://code.alibabatech.com/schema/dubbo    
  7.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
  8.          
  9.     <dubbo:application owner="lyncc" name="bazinga-consumer" />  
  10.     <!--zookeeper注册中心 -->  
  11.     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>   
  12.       
  13.     <dubbo:reference id="easyCommonService" interface="org.bazinga.service.EasyCommonService" generic="true"/>   
  14.       
  15. </beans>  

可以看到上图的配置文件中有两个地方需要注意一下,第一个是interface,其实该接口在消费者端并不存在,这是与往常写的不一样的地方,第二个地方需要注意的地方就是generic=”true”这样的标签,表示该接口支持泛型调用。

好了,我们写一个服务测试类,看看如何进行泛型调用DubboConsumerGenericService.java

[java]   view plain  copy
  1. package org.bazinga.service.test;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4.   
  5. import com.alibaba.dubbo.rpc.service.GenericService;  
  6.   
  7. public class DubboConsumerGenericService {  
  8.       
  9.       
  10.     public static void main(String[] args) {  
  11.           
  12.           
  13.         /Spring泛化调用/  
  14.       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
  15.               "spring-dubbo-consumer-generic.xml");  
  16.       context.start();  
  17.         
  18.       GenericService easyCommonService = (GenericService) context.getBean("easyCommonService");  
  19.       Object result = easyCommonService.$invoke("helloService"new String[] { "java.lang.String" }, new Object[] { "hello" });  
  20.       System.out.println(result);  
  21.         
  22.           
  23.     }  
  24.   
  25. }  

看到Main函数中,从spring的上下文中读到”easyCommonService”之后却把它强转为GenericService的类,然后调用GenericService的$invoke的方法,该方法有三个参数,第一个参数是你调用远程接口的具体方法名,第二个参数是helloService这个方法的入参的类型,最后一个参数是值。

 

我们测试一下,启动DubboxProviderGenericService的main函数,然后启动服务消费者的测试类DubboConsumerGenericService.java,你会发现控制台打印:


可以看到能够调通,没有问题。

 

 

我们刚才说,一个PHP程序员想要搭建一个简单的web项目,可是你却叫他依赖于spring的配置文件,对他难度是不小的,dubbo也帮你想到了,泛型调用,服务消费端可以不依赖spring的配置文件,我们重新写下测试类DubboConsumerGenericService:

[java]   view plain  copy
  1. package org.bazinga.service.test;  
  2.   
  3. import com.alibaba.dubbo.config.ApplicationConfig;  
  4. import com.alibaba.dubbo.config.ReferenceConfig;  
  5. import com.alibaba.dubbo.config.RegistryConfig;  
  6. import com.alibaba.dubbo.config.utils.ReferenceConfigCache;  
  7. import com.alibaba.dubbo.rpc.service.GenericService;  
  8.   
  9. public class DubboConsumerGenericService {  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         // 普通编码配置方式  
  14.         ApplicationConfig application = new ApplicationConfig();  
  15.         application.setName("bazinga-consumer");  
  16.   
  17.         // 连接注册中心配置  
  18.         RegistryConfig registry = new RegistryConfig();  
  19.         registry.setAddress("zookeeper://127.0.0.1:2181");  
  20.   
  21.         ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();  
  22.         reference.setApplication(application);  
  23.         reference.setRegistry(registry);  
  24.         reference.setInterface("org.bazinga.service.EasyCommonService");  
  25.         reference.setGeneric(true); // 声明为泛化接口  
  26.   
  27.         ReferenceConfigCache cache = ReferenceConfigCache.getCache();  
  28.         GenericService genericService = cache.get(reference);  
  29.   
  30.         // 基本类型以及Date,List,Map等不需要转换,直接调用  
  31.         Object result = genericService.$invoke("helloService"new String[] { "java.lang.String" },  
  32.                 new Object[] { "world" });  
  33.         System.out.println(result);  
  34.   
  35.     }  
  36.   
  37. }  

重新启动一下该类。控制台正常打印:



也能正常进行调用。

 

 

3.5.2 泛化调用小结

  泛化调用可以方便用户对dubbo服务消费者端的扩展,可以方便,丰富了服务消费者的调用方式,甚至可以做变相的Rest调用,这些都是可以的,不过,它的缺点也是很明显的,参数传递复杂,不方便使用。但是这种方式是不能缺失的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值