文档

DUBBO 客户端接入

更新时间:

网关提供客户端 DUBBO Demo 代码供用户参考,下载客户端 Demo后,DUBBO Demo 代码位置为:com/alipay/gateway/web/test/usercase/mainchain/dubbo/DubboClientServiceTest.java。

操作步骤

  1. 编写后端 Server。

    例如:

    @Controller
    @RequestMapping("/springmvc/test")
    public class PostMvcServerController {
    
        /**
         * 测试场景: 没有参数的测试
         *
         * @return
         */
        @RequestMapping(value = "/param/noParam", method = RequestMethod.POST)
        @ResponseBody
        public Map<String, String> hello() {
            Map<String, String> map = new HashMap<>();
            map.put("post", "hello");
            System.out.println(JSONObject.toJSONString(map));
            return map;
        }
    }
  2. 登录网关控制台。

  3. 创建系统集群,并使其指向本地后端 Server。

    重要

    如果此处系统集群的认证方式选择了 密钥,由于网关目前仅提供 JavaSDK,其他语言类型需用户自行实现。

  4. 创建分组和 API,指定前端的调用方式和后端的转发方式。

    如下图所示:

    22
    重要

    DUBBO 类型的 API 暂不支持签名认证和数据加密等需要客户端配置的需求。

  5. 编写调用方代码。

    • 编写调用代码

      package com.alipay.gateway.facade.dubbo;
      
      public interface GatewayDubbo2HttpService {
      
          Map<String, String> stringParam(String param);
      }
      
      
    • 编写 DUBBO Reference

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
             xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
      
          <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
          <dubbo:application name="consumer-of-helloworld-app"  />
           <!-- 协议必须是fastjson序列化 -->
          <dubbo:protocol name="dubbo" serialization="fastjson" />
      
          <!-- 生成远程服务代理,可以和本地bean一样使用demoService url=网关ip:20888 -->
          <dubbo:reference id="gatewayDubbo2HttpService" interface="com.alipay.gateway.facade.dubbo.GatewayDubbo2HttpService" timeout="100000" protocol="dubbo" url="网关ip:20888?serialization=fastjson" version="1.2.83" >
          </dubbo:reference>
      </beans>
      
    • DUBBO 调用

      从 API 详情获取分组 ID 并写入 x-mosng-host

      public class DubboClientServiceTest extends AbstractTestBase {
      
      
       @Test
          public void dubbo2HttpStringParam() {
              ApplicationContext context = new ClassPathXmlApplicationContext(
                      "classpath:META-INF/gateway-test-client/dubbo-consumer.xml");
              RpcContext.getContext().setAttachment("x-mosng-host", "lv51bpe4v3kiy6nn");
      
              GatewayDubbo2HttpService gatewayDubbo2HttpService = (GatewayDubbo2HttpService)context.getBean("gatewayDubbo2HttpService"); // 获取远程服务代理
      				//注意:入参需要和在网关页面上配置的类型一致,且响应参数需要和dubbo对齐否则会报错
              Map<String, String> dubbo2HttpStringParam = gatewayDubbo2HttpService.stringParam("aaa");
              System.out.println(JSON.toJSONString(dubbo2HttpStringParam)); // 显示调用结果
          }
      }
  6. 发起请求。

调用结果如下:

image

  • 本页导读 (0)
文档反馈