APISIX dubbo-proxy 實戰

APISIX

APISIX 聲稱支持 Dubbo Proxy

實踐

主要是 APISIX 官方網站上的這篇博客寫的問題很大。

博客上寫的 HTTP2DubboService 實現類

@Component
public class HTTP2DubboServiceImpl implements HTTP2DubboService {

    @Autowired
    private ApplicationContext appContext;

    @Override
    public Map<String, Object> invoke(Map<String, Object> context) throws Exception {
        DubboInvocation invocation = JSONObject.parseObject((byte[]) context.get("body"), DubboInvocation.class);
        Object[] args = new Object[invocation.getParameters().size()];
        for (int i = 0; i < args.length; i++) {
            DubboInvocationParameter parameter = invocation.getParameters().get(i);
            args[i] = JSONObject.parseObject(parameter.getValue(), Class.forName(parameter.getType()));
        }

        Object svc = appContext.getBean(Class.forName(invocation.getService()));
        Object result = svc.getClass().getMethod(invocation.getMethod()).invoke(args);
        Map<String, Object> httpResponse = new HashMap<>();
        httpResponse.put("status", 200);
        httpResponse.put("body", JSONObject.toJSONString(result));
        return httpResponse;
    }

}

槽點1

首先名字就很讓人誤解:HTTP2Dubbo,其實這個根本不是HTTP協議。這個是 Tengine 提供的 mod_dubbo 實現的功能,openresty/tengine 接收的是 HTTP數據報文,但是對於應用程序提供者,仍然不是HTTP協議。
本質上,要讓 APISIX 的 Dubbo Proxy 能調用成功,還是有很多前提條件的。

槽點2

官方給的 Demo 沒問題,但是沒有強調這點:入參和出參只支持 Map<String, Object> ,否則500 Bad Gateway。

槽點3

上述程序,作用是將統一的參數轉換成反射調用,調用實際方法,但是這能跑的通?

  • 如果參數類型爲 com.slankka.service.dubbo.ISampleService.getData(java.lang.Long, java.lang.String)。
    這樣會導致 JSONObject.parseObject("string value", String.class), 這樣無論是 fastjson,fastjson2還是 jackson 都是會失敗的。

  • 反射是這樣用的嗎?

  svc.getClass().getMethod(invocation.getMethod()).invoke(args);

實際上應該是這樣用的

  Class<?>[] = ...
  parameterTypes[i] = Class.forName(parameter.getType())

  svc.getClass().getMethod(invocation.getMethod(), parameterTypes).invoke(svc, args);

槽點4

apisix的 nginx 返回值,Content-Type 是 text/plain; 這樣豈不是要了 OpenFeign 的命!Spring Cloud OpenFeign,RestTemplate, Spring HttpMessageConverter 全部反對!

實際情況可能因apisix自己編譯 mod_dubbo 的實現有關

HTTP/1.1 200 OK
Date: Sun, 24 Dec 2023 12:16:29 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 178
Connection: keep-alive
Server: APISIX/3.2.1

參考文檔

Tengine: Dubbo Example
APISIX dubbo-proxy
實測Tengine開源的Dubbo功能
讓你在 Apache APISIX 中代理 Dubbo 服務更便捷
Tengine Header Response Content-Type
github.com/api7/mod_dubbo
github.com/api7/apisix-build-tools

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章