dubbo http流量接入dubbo后端服務(wù)方式
簡介
dubbo協(xié)議是基于TCP的二進制私有協(xié)議,更適合作為后端微服務(wù)間的高效RPC通信協(xié)議,也導(dǎo)致dubbo協(xié)議對于前端流量接入不是很友好。在dubo框架中,有兩種方式可以解決這個問題:
- 多協(xié)議發(fā)布【推薦】,為dubbo協(xié)議服務(wù)暴露rest風(fēng)格的http協(xié)議訪問方式。
- 通過網(wǎng)關(guān)實現(xiàn) http->dubbo協(xié)議轉(zhuǎn)換,這種方式需要將http協(xié)議轉(zhuǎn)換為后端服務(wù)能識別的dubbo協(xié)議,要求網(wǎng)關(guān)必須支持dubbo協(xié)議。
同時發(fā)布http、dubbo協(xié)議
dubbo框架支持為同一個服務(wù)發(fā)布多個協(xié)議,并且支持客戶端通過同一個端口以不同的協(xié)議訪問服務(wù)。
發(fā)布方式一
在dubbo協(xié)議的基礎(chǔ)上增加tri協(xié)議
dubbo:
protocol:
name: dubbo
port: 20080
ext-protocol: tri
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 + '\'' +
'}';
}
}
訪問
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
發(fā)布方式二
只使用tri協(xié)議
dubbo:
protocol:
name: tri
port: 50053
訪問
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
使用REST風(fēng)格
引入javax.ws.rs-api
<dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.1.1</version> </dependency>
在使用方法上添加路徑注解
import com.alibaba.fastjson2.JSON;
import com.doudou.demo.api.DemoService;
import com.doudou.demo.po.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/demo")
@DubboService
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
/**
* curl --header "Content-Type:text/html" --data "word" http://localhost:50051/demo/hello
* curl --header "Content-Type:text/html" --data "word" http://localhost:50051/com.doudou.demo.api.DemoService/sayHello
*/
@Path("/hello")
@POST
@Override
public String sayHello(String name) {
logger.info("name:{}", name);
return "hello " + name;
}
/**
* curl -X POST --header "Content-Type:text/html" http://localhost:50051/demo/hello2/world
*/
@Path("/hello2/{name}")
@POST
@Override
public String sayHello2(@PathParam("name") String name) {
logger.info("name 2:{}", name);
return "hello 2 " + name;
}
/**
* curl -X POST --header "Content-Type:application/json" "http://localhost:50051/demo/hello3?name=doudou"
*/
@Path("/hello3")
@POST
@Override
public String sayHello3(@QueryParam("name") String name) {
logger.info("name 3:{}", name);
return "hello 3 " + name;
}
/**
* curl -X POST "http://localhost:50051/demo/hello4" --header "Content-Type: application/x-www-form-urlencoded" -d "name=admin"
* curl -X POST "http://localhost:50051/com.doudou.demo.api.DemoService/sayHello4" --header "Content-Type: application/x-www-form-urlencoded" -d "name=admin1"
*
* @FormParam 用于接收表單數(shù)據(jù)?(application/x-www-form-urlencoded 或 multipart/form-data):
*/
@Path("/hello4")
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) // 必須指定表單類型
@Override
public String sayHello4(@FormParam("name") String name) {
logger.info("name 4:{}", name);
return "hello 4 " + name;
}
/**
* curl -X POST --header "Content-Type:application/json" -H "name: world" http://localhost:50051/demo/hello5
* curl -X POST --header "Content-Type:application/json" -H "name: world" http://localhost:50051/com.doudou.demo.api.DemoService/sayHello5
*/
@Path("/hello5")
@POST
@Override
public String sayHello5(@HeaderParam("name") String name) {
logger.info("name 5:{}", name);
return "hello 5 " + name;
}
/**
* curl -X POST --header "Content-Type:application/json" -H "cookie:name=world" http://localhost:50051/demo/hello6
* curl -X POST --header "Content-Type:application/json" -H "cookie:name=world" http://localhost:50051/com.doudou.demo.api.DemoService/sayHello6
*/
@Path("/hello6")
@POST
@Override
public String sayHello6(@CookieParam("name") String name) {
logger.info("name 6:{}", name);
return "hello 6 " + name;
}
/**
* curl -X POST --header "Content-Type:application/json" --data '{"id":1,"name":"world"}' http://localhost:50051/com.doudou.demo.api.DemoService/sayHello7
* curl -X POST --header "Content-Type:application/json" --data '{user:{"id":1,"name":"world"}, user2:{"id":2,"name":"world2"}}' http://localhost:50051/com.doudou.demo.api.DemoService/sayHello7
*
* {"id":1,"name":"world"}
*/
@Path("/hello7")
@POST
@Override
public String sayHello7(User user, User user2) {
logger.info("user:{}", JSON.toJSONString(user));
logger.info("user2:{}", JSON.toJSONString(user2));
return JSON.toJSONString(user);
}
}
使用網(wǎng)關(guān)轉(zhuǎn)http協(xié)議為dubbo協(xié)議
網(wǎng)關(guān)需要實現(xiàn)的關(guān)鍵點:
協(xié)議轉(zhuǎn)換,支持http到dubbo協(xié)議的轉(zhuǎn)換,包括參數(shù)映射。自動地址發(fā)現(xiàn),支持Nacos、Zookeeper、Kubernetes等主流注冊中心,動態(tài)感知后端dubbo實例變化。結(jié)合dubbo協(xié)議的路由,如在發(fā)起 dubbo 協(xié)議調(diào)用時,支持按照特定規(guī)則地址篩選、傳遞附加參數(shù)到 dubbo 后端服務(wù)。
支持的開源網(wǎng)關(guān)
- Higress
- Apache APISIX
- Apache Shenyu
參考HTTP網(wǎng)關(guān)接入->dubbo協(xié)議
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java并發(fā)中的ABA問題學(xué)習(xí)與解決方案
這篇文章主要介紹了Java并發(fā)中的ABA問題學(xué)習(xí)與解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Java實現(xiàn)特定范圍的完數(shù)輸出算法示例
這篇文章主要介紹了Java實現(xiàn)特定范圍的完數(shù)輸出算法,簡單說明了完數(shù)的概念、計算原理并結(jié)合實例形式分析了java針對給定范圍內(nèi)的完數(shù)輸出操作實現(xiàn)技巧,需要的朋友可以參考下2017-12-12
關(guān)于Jackson的JSON工具類封裝 JsonUtils用法
這篇文章主要介紹了關(guān)于Jackson的JSON工具類封裝 JsonUtils用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
如何在Spring Boot應(yīng)用中優(yōu)雅的使用Date和LocalDateTime的教程詳解
這篇文章主要介紹了如何在Spring Boot應(yīng)用中優(yōu)雅的使用Date和LocalDateTime,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
java多線程關(guān)鍵字final和static詳解
這篇文章主要介紹了java多線程關(guān)鍵字final和static詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
Java 如何讀取Excel格式xls、xlsx數(shù)據(jù)工具類
這篇文章主要介紹了Java 如何讀取Excel格式xls、xlsx數(shù)據(jù)工具類的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

