从原理到操作,让你在 Apache APISIX 中代理 Dubbo3 服务更便捷
背景
Aliware
如何使用
Aliware
入门篇:安装使用
-
接口实现一览
public interface DemoService {/*** standard samples dubbo infterace demo* @param context pass http infos* @return Map<String, Object></> pass to response http**/Map<String, Object> apisixDubbo(Map<String, Object> httpRequestContext);}
<!-- service implementation, as same as regular local bean --><bean id="demoService" class="org.apache.dubbo.samples.provider.DemoServiceImpl"/><!-- declare the service interface to be exported --><dubbo:service interface="org.apache.dubbo.samples.apisix.DemoService" ref="demoService"/>
public class DemoServiceImpl implements DemoService {@Overridepublic Map<String, Object> apisixDubbo(Map<String, Object> httpRequestContext) {for (Map.Entry<String, Object> entry : httpRequestContext.entrySet()) {System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());}Map<String, Object> ret = new HashMap<String, Object>();ret.put("body", "dubbo success\n"); // http response bodyret.put("status", "200"); // http response statusret.put("test", "123"); // http response headerreturn ret;}}
-
操作步骤
-
启动 dubbo-samples。 -
在 config.yaml 文件中进行 dubbo-proxy 插件启用。
# Add this in config.yamlplugins:- ... # plugin you need- dubbo-proxy
curl http://127.0.0.1:9180/apisix/admin/upstreams/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{"nodes": {"127.0.0.1:20880": 1},"type": "roundrobin"}'
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '{"host": "example.org""uris": ["/demo"],"plugins": {"dubbo-proxy": {"service_name": "org.apache.dubbo.samples.apisix.DemoService","service_version": "0.0.0","method": "apisixDubbo"}},"upstream_id": 1}'
curl http://127.0.0.1:9080/demo -H "Host: example.org" -X POST --data '{"name": "hello"}'< HTTP/1.1 200 OK< Date: Sun, 26 Dec 2021 11:33:27 GMT< Content-Type: text/plain; charset=utf-8< Content-Length: 14< Connection: keep-alive< test: 123< Server: APISIX/2.11.0<dubbo success
Key = content-length, Value = 17Key = host, Value = example.orgKey = content-type, Value = application/x-www-form-urlencodedKey = body, Value = [B@70754265Key = accept, Value = */*Key = user-agent, Value = curl/7.80.0
进阶篇:复杂场景示例
-
操作步骤
public class DubboInvocationParameter {private String type;private String value;}public class DubboInvocation {private String service;private String method;private DubboInvocationParameter[] parameters;}public interface HTTP2DubboService {Map<String, Object> invoke(Map<String, Object> context) throws Exception;}@Componentpublic class HTTP2DubboServiceImpl implements HTTP2DubboService {@Autowiredprivate ApplicationContext appContext;@Overridepublic 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;}}
curl http://127.0.0.1:9080/demo -H "Host: example.org" -X POST --data '{"service": "org.apache.dubbo.samples.apisix.DemoService","method": "createUser","parameters": [{"type": "org.apache.dubbo.samples.apisix.User","value": "{'name': 'hello'}"}]}'
总结
Aliware
参考链接
Apache Dubbo
https://dubbo.apache.org/zh/
Apache APISIX
https://apisix.apache.org/
dubbo-samples
https://github.com/apache/dubbo-samples
官方文档
https://apisix.apache.org/zh/docs/apisix/plugins/dubbo-proxy/