• 首页
  • 邻居
  • 关于
  • 归档
  • 搜索
  • 夜间模式
    ©2020-2026  我的学习笔记 Theme by OneBlog

    我的学习笔记博客

    搜索
    标签
    # 随笔 # Java # 教程 # openwrt # Mysql # SQL # 爬虫 # post # Js调优 # MAVEN
  • 首页>
  • 随笔>
  • 正文
  • DUBBO泛化调用原理与设计思想

    2023年03月29日 1.4 k 阅读 0 评论 2821 字

    1 泛化调用实例
    对于JAVA服务端开发者而言在使用Dubbo时并不经常使用泛化调用,通常方法是在生产者发布服务之后,消费者可以通过引入生产者提供的client进行调用。那么泛化调用使用场景是什么呢?

    第一种场景是消费者不希望引入生产者提供的client依赖,只希望关注调用哪个方法,需要传什么参数即可。第二种场景是消费者不是使用Java语言,而是使用例如Python语言,那么如何调用使用Java语言生产者提供的服务呢?这时我们可以选择泛化调用。

    泛化调用使用方法并不复杂,下面我们编写一个泛化调用实例。首先生产者发布服务,这与普通服务发布没有任何区别。

    package com.java.front.dubbo.demo.provider;
    <!--more-->
    public interface HelloService {
        public String sayHelloGeneric(Person person, String message);
    }
    
    public class HelloServiceImpl implements HelloService {
        @Override
        public String sayHelloGeneric(Person person, String message) throws Exception {
            String result = "hello[" + person + "],message=" + message;
            return result;
        }
    }

    Person类声明:

    <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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans        
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
     <!-- 提供方应用信息,用于计算依赖关系 -->
     <dubbo:application name="java-front-provider" />
    
     <!-- 连接注册中心 -->
     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
     <!-- 生产者9999在端口暴露服务 -->
     <dubbo:protocol name="dubbo" port="9999" />
     
     <!-- Bean -->
     <bean id="helloService" class="com.java.front.dubbo.demo.provider.HelloServiceImpl" />
     
     <!-- 暴露服务 -->
     <dubbo:service interface="com.java.front.dubbo.demo.provider.HelloService" ref="helloService" />
    </beans>

    消费者代码有所不同:

    import org.apache.dubbo.config.ApplicationConfig;
    import org.apache.dubbo.config.ReferenceConfig;
    import org.apache.dubbo.config.RegistryConfig;
    import org.apache.dubbo.rpc.RpcContext;
    import org.apache.dubbo.rpc.service.GenericService;
    
    public class Consumer {
        public static void testGeneric() {
            ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
            reference.setApplication(new ApplicationConfig("java-front-consumer"));
            reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
            reference.setInterface("com.java.front.dubbo.demo.provider.HelloService");
            reference.setGeneric(true);
            GenericService genericService = reference.get();
            Map<String, Object> person = new HashMap<String, Object>();
            person.put("name", "微信公众号「JAVA前线」");
            String message = "你好";
            Object result = genericService.$invoke("sayHelloGeneric", new String[] { "com.java.front.dubbo.demo.provider.model.Person", "java.lang.String" }, new Object[] { person, message });
            System.out.println(result);
        }
    }
    本文著作权归作者 [ admin ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。
    取消回复

    发表留言
    回复

    首页邻居关于归档
    Copyright©2020-2026  All Rights Reserved.  Load:0.017 s
    京ICP备18019712号
    Theme by OneBlog V3.6.5
    夜间模式

    开源不易,请尊重作者版权,保留基本的版权信息。