简介
dubbo协议是基于TCP的二进制私有协议,更适合作为后端微服务间的高效RPC通信协议,也导致dubbo协议对于前端流量接入不是很友好。在dubo框架中,有两种方式可以解决这个问题:
多协议发布【推荐】
,为dubbo协议服务暴露rest风格的http协议访问方式。通过网关实现 http->dubbo协议转换
,这种方式需要将http协议转换为后端服务能识别的dubbo协议,要求网关必须支持dubbo协议。
同时发布http、dubbo协议
dubbo框架支持为同一个服务发布多个协议,并且支持客户端通过同一个端口以不同的协议访问服务。
发布方式一
在dubbo
协议的基础上增加tri
协议
yml
dubbo:
protocol:
name: dubbo
port: 20080
ext-protocol: tri
java
public interface DemoService01 {
String buyApple(Apple apple);
String sayHello(String name);
}
public class Apple implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public Apple(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Apple{" +
"name='" + name + '\'' +
'}';
}
}
访问
shell
curl --header "Content-Type:application/json" --data '{"name":"apple"}' http://localhost:20880/com.doudou.rpc.api.DemoService01/buyApple
curl --header "Content-Type:text/html" --data "name" http://localhost:20880/com.doudou.rpc.api.DemoService01/sayHello
发布方式二
只使用tri
协议
yaml
dubbo:
protocol:
name: tri
port: 50053
访问
shell
curl --header "Content-Type:application/json" --data '{"name":"apple"}' http://localhost:50053/com.doudou.rpc.api.DemoService01/buyApple
curl --header "Content-Type:text/html" --data "name" http://localhost:50053/com.doudou.rpc.api.DemoService01/sayHello
使用网关转http协议为dubbo协议
网关需要实现的关键点:
协议转换
,支持http到dubbo协议的转换,包括参数映射。自动地址发现
,支持Nacos、Zookeeper、Kubernetes等主流注册中心,动态感知后端dubbo实例变化。结合dubbo协议的路由
,如在发起 dubbo 协议调用时,支持按照特定规则地址筛选、传递附加参数到 dubbo 后端服务。
支持的开源网关
- Higress
- Apache APISIX
- Apache Shenyu