閱讀851 返回首頁    go 微信


HSF 特性使用__服務開發_開發者指南_企業級分布式應用服務 EDAS-阿裏雲

本文介紹 HSF 一些特性的使用方法及注意事項。

所有特性的實例 Demo 請在這裏下載:Demo 下載

前提條件

使用 HSF 相關特性,請在 POM 文件中加入以下 edas-sdk 依賴。

  1. <dependency>
  2. <groupId>com.alibaba.edas</groupId>
  3. <artifactId>edas-sdk</artifactId>
  4. <version>1.5.1</version>
  5. </dependency>

隱式傳參(目前僅支持字符串傳輸)

隱式傳參一般用於傳遞一些簡單 KV 數據,又不想通過接口方式傳遞,類似於 Cookie。

  • 單個參數傳遞

    服務消費者:

    RpcContext.getContext().setAttachment("key", "args test");

    服務提供者:

    String keyVal=RpcContext.getContext().getAttachment("key");

  • 多個參數傳遞

    服務消費者:

    1. Map<String,String> map=new HashMap<String,String>();
    2. map.put("param1", "param1 test");
    3. map.put("param2", "param2 test");
    4. map.put("param3", "param3 test");
    5. map.put("param4", "param4 test");
    6. map.put("param5", "param5 test");
    7. RpcContext rpcContext = RpcContext.getContext();
    8. rpcContext.setAttachments(map);

    服務提供者:

    1. Map<String,String> map=rpcContext.getAttachments();
    2. Set<String> set=map.keySet();
    3. for (String key : set) {
    4. System.out.println("map value:"+map.get(key));
    5. }

異步調用

支持異步調用 callback 和 future 兩種方式。

  • callback 調用方式

    客戶端配置為 callback 方式調用時,需要配置一個實現了 HSFResponseCallback 接口的 listener ,結果返回之後, HSF 會調用 HSFResponseCallback 中的方法。

    注意:這個 HSFResponseCallback 接口的 listener 不能是內部類,否則 Pandora 的 classloader 在加載時就會報錯。

    XML 中的配置:

    1. <hsf:consumer id="demoApi" interface="com.alibaba.demo.api.DemoApi"
    2. version="1.1.2" group="test" >
    3. <hsf:asyncallMethods>
    4. <hsf:method name="ayncTest" type="callback"
    5. listener="com.alibaba.ifree.hsf.consumer.AsynABTestCallbackHandler" />
    6. </hsf:asyncallMethods>
    7. </hsf:consumer>

    其中 AsynABTestCallbackHandler 類,是實現了 HSFResponseCallback 接口。DemoApi 接口中有一個方法是 ayncTest 。

    代碼示例

    1. public void onAppResponse(Object appResponse) {
    2. //這裏獲取到異步調用後的值
    3. String msg = (String)appResponse;
    4. System.out.println("msg:"+msg);
    5. }

    注意:

    • 由於隻用方法名字來標識方法,所以並不區分重載的方法。同名的方法都會被設置為同樣的調用方式。
    • 不支持在 call 裏再發起 HSF 調用。這種做法可能導致 IO 線程掛起,無法恢複。
  • future 調用方式

    客戶端配置為 future 方式調用時,發起調用之後,通過 HSFResponseFuture 中的 public static Object getResponse(long timeout) 來獲取返回結果。

    XML 中的配置:

    1. <hsf:consumer id="demoApi" interface="com.alibaba.demo.api.DemoApi" version="1.1.2" group="test" >
    2. <hsf:asyncallMethods>
    3. <hsf:method name="ayncTest" type="future" />
    4. </hsf:asyncallMethods>
    5. </hsf:consumer>

    代碼示例如下。

    • 單個業務異步處理:

      1. //發起調用
      2. demoApi.ayncTest();
      3. // 處理業務
      4. ...
      5. //直接獲得消息(若無需獲得結果,可以不用操作該步驟)
      6. String msg=(String) HSFResponseFuture.getResponse(3000);
    • 多個業務需要並發處理:

      若是多個業務需要並發處理,可以先獲取 future,進行存儲起來,等調用完畢後在使用。

      1. //定義集合
      2. List<HSFFuture> futures = new ArrayList<HSFFuture>();
    • 方法內進行並行調用:

      1. //發起調用
      2. demoApi.ayncTest();
      3. //第一步獲取future對象
      4. HSFFuture future=HSFResponseFuture.getFuture();
      5. futures.add(future);
      6. //繼續調用其他業務(同樣采取異步調用)
      7. HSFFuture future=HSFResponseFuture.getFuture();
      8. futures.add(future);
      9. // 處理業務
      10. ...
      11. //獲得數據並做處理
      12. for (HSFFuture hsfFuture : futures) {
      13. String msg=(String) hsfFuture.getResponse(3000);
      14. //處理相應數據
      15. ...
      16. }

泛化調用

通過泛化調用可以組合接口、方法、參數進行RPC調用,無需依賴任何業務API。

操作步驟

在消費者配置中加入泛化屬性。
  1. <hsf:consumer id="demoApi" interface="com.alibaba.demo.api.DemoApi" group="unittest" generic="true"/>

說明:generic代表泛化參數,true標識支持泛化,false標識不支持,默認為false。

DemoApi接口方法:

  1. public String dealMsg(String msg);
  2. public GenericTestDO dealGenericTestDO(GenericTestDO testDO);
獲取demoApi進行強製轉換為泛化服務
  • 導入泛化服務接口

    1. import com.alibaba.dubbo.rpc.service.GenericService
  • 獲取泛化對象

    1. //若WEB項目中,可通過Spring bean進行注入後強轉,這裏是單元測試,所以采用加載配置文件方式
    2. ClassPathXmlApplicationContext consumerContext = new ClassPathXmlApplicationContext("hsf-generic-consumer-beans.xml");
    3. GenericService svc = (GenericService) consumerContext.getBean("demoApi");
泛化接口
  1. Object $invoke(String methodName, String[] parameterTypes, Object[] args) throws GenericException;

說明:

methodName:需要調用的方法名稱。

parameterTypes:需要調用方法參數的類型。

args:需要傳輸的參數值。

泛化調用(String類型參數)
  1. svc.$invoke("dealMsg", new String[] { "java.lang.String" }, new Object[] { "hello" })
泛化調用(對象參數)
  1. // 第一步構造實體對象GenericTestDO,該實體有id、name兩個屬性
  2. GenericTestDO genericTestDO = new GenericTestDO();
  3. genericTestDO.setId(1980l);
  4. genericTestDO.setName("genericTestDO-tst");
  5. // 使用 PojoUtils 生成二方包pojo的描述
  6. Object comp = PojoUtils.generalize(genericTestDO);
  7. // 服務泛化調用
  8. svc.$invoke("dealGenericTestDO",new String[] { "com.alibaba.demo.generic.domain.GenericTestDO" }, new Object[] { comp });

最後更新:2016-11-23 16:04:20

  上一篇:go 消費者訂閱服務__服務開發_開發者指南_企業級分布式應用服務 EDAS-阿裏雲
  下一篇:go HSF 單元測試__服務開發_開發者指南_企業級分布式應用服務 EDAS-阿裏雲