实测Tengine开源的Dubbo功能

327次阅读  |  发布于3年以前

背景

2019年9月,Tengine 2.3.2版本发布了dubbo_pass模块,支持HTTP协议到Dubbo协议的转换。Release页面如下:

❝https://github.com/alibaba/tengine/releases/tag/2.3.2

意义

[《Gateway技术革命 - Tengine开源Dubbo功能》] 对Tengine支持Dubbo协议的意义讲的比较清楚,总结有如下几点:

实测

Tengine环境搭建

这里我用centos的基础镜像搭了一套Tengine环境,简单说明一下步骤:

mkdir -p /home/roshi && cd /home/roshi/
wget https://github.com/alibaba/tengine/archive/2.3.2.zip
unzip 2.3.2.zip && cd tengine-2.3.2/

wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar xvf pcre-8.43.tar.gz

wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz
tar xvf openssl-1.0.2s.tar.gz

wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar xvf zlib-1.2.11.tar.gz
yum install gcc
yum install gcc-c++
yum -y install gcc automake autoconf libtool make
./configure --add-module=./modules/mod_dubbo --add-module=./modules/ngx_multi_upstream_module --add-module=./modules/mod_config --with-pcre=./pcre-8.43/ --with-openssl=./openssl-1.0.2s/ --with-zlib=./zlib-1.2.11
make && make install
/usr/local/nginx/sbin/nginx

Dubbo例子

这里要提一下之前在[《dubbo应用级服务发现初体验》] 中提到的快速搭建Dubbo调试环境的方法:

由于Tengine有限制,接口的出参和入参必须是Map<String, Object>,所以需要对例子进行修改:

public interface DemoService {

    String sayHello(String name);

    default Map<String, Object> testTengine(Map<String, Object> args) {
        return null;
    }

    default CompletableFuture<String> sayHelloAsync(String name) {
        return CompletableFuture.completedFuture(sayHello(name));
    }
}
public class DemoServiceImpl implements DemoService {
    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

    @Override
    public String sayHello(String name) {
        logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

    @Override
    public Map<String, Object> testTengine(Map<String, Object> args) {
        Map<String, Object> res = new HashMap<>();
        res.put("body", "hello tengine dubbo\n");
        res.put("status", "200");
        System.out.println("testTengine");
        return res;
    }

    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        return null;
    }

}

为了更好的测试多个provider的情况,可以用同一份代码,在不同的端口起多个服务。

修改Tengine配置

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    server {
        listen       8080;
        server_name  localhost;

        #pass the Dubbo to Dubbo Provider server listening on 127.0.0.1:20880
        location / {
            dubbo_pass_all_headers on;
            # dubbo_pass_set args $args;
            dubbo_pass_set args hello;
            dubbo_pass_set uri $uri;
            dubbo_pass_set method $request_method;

            dubbo_pass org.apache.dubbo.demo.DemoService 0.0.0 testTengine dubbo_backend;
        }
    }


    #pass the Dubbo to Dubbo Provider server listening on 127.0.0.1:20880
    upstream dubbo_backend {
        multi 1;
        server host.docker.internal:20880;
        server host.docker.internal:20881;
    }
}
/usr/local/nginx/sbin/nginx -s reload

测试

使用如下命令测试

curl -X POST http://127.0.0.1:8080/dubbo -i -d "hello=roshi"

看一下传参情况

总结

经过测试,总结以下几点:

最后

正如[《Gateway技术革命 - Tengine开源Dubbo功能》] 文中所说,Tengine只是完成了作为Dubbo Consumer的协议支持,像服务发现、自定义接口、服务分组、容错降级等其他功能暂未实现,暂时还离生产有些距离。

最后吐槽一下Tegine官网的文档


参考

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8