大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 Dubbo泛化調(diào)用示例

Dubbo泛化調(diào)用示例

更新時(shí)間:2022-03-18 12:04:45 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1806次

簡單的介紹

通用接口調(diào)用方式主要用于沒有API接口和模型類元素的客戶端,所有...中的參數(shù)和返回值POJO都使用Map Express。使用泛化調(diào)用時(shí),服務(wù)提供者應(yīng)用沒有特殊操作,服務(wù)消費(fèi)者應(yīng)用不再需要引入服務(wù)提供者 SDK 兩方包。

適用于 API Gateway 服務(wù)、框架集成等場景,提供一個(gè) Dubbo 統(tǒng)一的服務(wù)管理平臺(tái),讓所有消費(fèi)者調(diào)用統(tǒng)一管理平臺(tái)中注冊的服務(wù),無需引入 SDK 雙方庫。

使用示例

public interface GreetingService {
   String sayHello(String name);
}
/**
* adopt API The method uses generalization to call
*/
@Test
public void test() {
   // Application configuration
   ApplicationConfig applicationConfig = new ApplicationConfig();
   applicationConfig.setName("dubbo-consumer");?
   // Registry configuration
   RegistryConfig registryConfig = new RegistryConfig();
   registryConfig.setAddress("zookeeper://127.0.0.1:2181");?
   // Reference remote service
   ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
   reference.setApplication(applicationConfig);
   reference.setRegistry(registryConfig);
   reference.setInterface("com.xxx.GreetingService");
   reference.setGeneric(true);?
   // obtain GenericService, Instead of
   ReferenceConfigCache cache = ReferenceConfigCache.getCache();
   GenericService genericService = cache.get(reference);?
   // Call the service
   String[] parameterTypes = new String[] { "java.lang.String" };
   Object[] args = Stream.of("xiaoming").toArray();
   Object result = genericService.$invoke("sayHello", parameterTypes, args);
   System.out.println("result = " + result);
}

源碼分析

服務(wù)消費(fèi)者

在 dubbo 的責(zé)任鏈中,GenericImplFilter 會(huì)攔截泛化調(diào)用,檢查參數(shù),并發(fā)起 RPC 調(diào)用。

org.apache.dubbo.rpc.filter.GenericImplFilter#invoke
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
   String generic = invoker.getUrl().getParameter(Constants.GENERIC_KEY);   
// Determine whether it is a generalization call
   if (invocation.getMethodName().equals(Constants.$INVOKE)
       && invocation.getArguments() != null
       && invocation.getArguments().length == 3
       && ProtocolUtils.isGeneric(generic)) {
       // Call parameters
       Object[] args = (Object[]) invocation.getArguments()[2];       
       if (ProtocolUtils.isJavaGenericSerialization(generic)) {
           for (Object arg : args) {
               if (!(byte[].class == arg.getClass())) {
                   error(generic, byte[].class.getName(), arg.getClass().getName());
              }
          }
      } else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
           for (Object arg : args) {
               if (!(arg instanceof JavaBeanDescriptor)) {
                   error(generic, JavaBeanDescriptor.class.getName(), arg.getClass().getName());
              }
          }
      }
       // Pass generic call mode parameters , So that the service provider can analyze the request parameters and other data
      ((RpcInvocation) invocation).setAttachment(
           Constants.GENERIC_KEY, invoker.getUrl().getParameter(Constants.GENERIC_KEY));
  }   
   // launch RPC call
   return invoker.invoke(invocation);
}

服務(wù)提供者

服務(wù)提供者使用GenericFilter攔截請求,反序列化并解析泛化參數(shù),將請求轉(zhuǎn)發(fā)給具體的服務(wù)提供者實(shí)現(xiàn)類對象進(jìn)行業(yè)務(wù)執(zhí)行。

org.apache.dubbo.rpc.filter.GenericFilter#invoke
@Override
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {   
   // Generalization call
   if (inv.getMethodName().equals(Constants.$INVOKE)
       && inv.getArguments() != null
       && inv.getArguments().length == 3
       && !GenericService.class.isAssignableFrom(invoker.getInterface())) {      
       // parameter information
       String name = ((String) inv.getArguments()[0]).trim();
       String[] types = (String[]) inv.getArguments()[1];
       Object[] args = (Object[]) inv.getArguments()[2];
       try {
           // Get call method
           Method method = ReflectUtils.findMethodByMethodSignature(invoker.getInterface(), name, types);
           Class<?>[] params = method.getParameterTypes();
           if (args == null) {
               args = new Object[params.length];
          }
           String generic = inv.getAttachment(Constants.GENERIC_KEY);
?
           if (StringUtils.isBlank(generic)) {
               generic = RpcContext.getContext().getAttachment(Constants.GENERIC_KEY);
          }?
           // The generalized type is null , Use the default deserialization method
           if (StringUtils.isEmpty(generic)
               || ProtocolUtils.isDefaultGenericSerialization(generic)) {
               args = PojoUtils.realize(args, params, method.getGenericParameterTypes());
           // nativejava Deserialization method
          } else if (ProtocolUtils.isJavaGenericSerialization(generic)) {
               for (int i = 0; i < args.length; i++) {
                   if (byte[].class == args[i].getClass()) {
                       try(UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream((byte[]) args[i])) {
                           args[i] = ExtensionLoader.getExtensionLoader(Serialization.class)
                              .getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA)
                              .deserialize(null, is).readObject();
                      } catch (Exception e) {
                           throw new RpcException("Deserialize argument [" + (i + 1) + "] failed.", e);
                      }
                  } else {
                       throw new RpcException(...);
                  }
              }
           // bean Deserialization method
          } else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
               for (int i = 0; i < args.length; i++) {
                   if (args[i] instanceof JavaBeanDescriptor) {
                       args[i] = JavaBeanSerializeUtil.deserialize((JavaBeanDescriptor) args[i]);
                  } else {
                       throw new RpcException(...));
                  }
              }
          }          
           // Perform specific services
           Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments()));
           if (result.hasException()
               && !(result.getException() instanceof GenericException)) {
               return new RpcResult(new GenericException(result.getException()));
          }           
           // Serialize the result and return
           if (ProtocolUtils.isJavaGenericSerialization(generic)) {
               try {
                   UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
                   ExtensionLoader.getExtensionLoader(Serialization.class)
                      .getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA)
                      .serialize(null, os).writeObject(result.getValue());
                   return new RpcResult(os.toByteArray());
              } catch (IOException e) {
                   throw new RpcException("Serialize result failed.", e);
              }
          } else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
               return new RpcResult(JavaBeanSerializeUtil.serialize(result.getValue(), JavaBeanAccessor.METHOD));
          } else {
               return new RpcResult(PojoUtils.generalize(result.getValue()));
          }
      } catch (NoSuchMethodException e) {
           throw new RpcException(e.getMessage(), e);
      } catch (ClassNotFoundException e) {
           throw new RpcException(e.getMessage(), e);
      }
  }   
   // Non generalized call , Pass the request to the next filter
   return invoker.invoke(inv);
}

 

提交申請后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 中文字幕在线一区二区在线 | 国内精品久久久久影院网站 | 手机看片国产永久1204 | 中国女人精69xxxxxx视频 | 亚洲精品区在线播放一区二区 | 免费两性的视频网站 | 欧美精品在线一区 | 四虎视频在线精品免费观看 | 亚洲精品性夜夜夜 | 欧美视频 亚洲视频 | 亚洲在线视频 | 99热久久这里只有精品在 | 国产欧美精品一区aⅴ影院 国产欧美精品一区二区 | 精品美女在线观看 | 真人女人一级毛片免费视频观看 | 精品国产一区二区三区不卡在线 | 99久久精品费精品国产一区二区 | 日本高清视频www夜色资源 | 手机看片福利盒子久久 | 噜噜嘿在线视频免费观看 | 国产一级特黄全黄毛片 | 特黄特级a级黄毛片免费观看多人 | 在线看片亚洲 | 四虎精品在线观看 | 亚洲欧美日韩国产精品 | 国产玖玖在线观看 | 久久99精品久久久久久牛牛影视 | 九九九国产视频 | 亚欧有色亚欧乱色视频 | 国产高清在线精品一区二区 | 日韩女人毛片在线播放 | 伊人久热这里只有精品视频99 | 国产精品一级毛片不收费 | 日韩精品一区二区三区中文在线 | 玖玖草在线观看 | 成人精品mv视频在线观看 | 国产欧美一区二区成人影院 | 国产福利视频深夜福利 | 免费高清一级欧美片在线观看 | 亚洲欧美日韩久久精品第一区 | 91久久老司机福利精品网 |