最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

dubbo http流量接入dubbo后端服務(wù)方式

 更新時(shí)間:2025年10月21日 08:36:51   作者:hello_zzw  
Dubbo協(xié)議基于TCP,適用于后端高效RPC通信,但對(duì)前端不友好,可通過多協(xié)議發(fā)布或網(wǎng)關(guān)協(xié)議轉(zhuǎn)換支持HTTP接入,主流網(wǎng)關(guān)如Higress、APISIX可實(shí)現(xiàn)協(xié)議轉(zhuǎn)換和自動(dòng)服務(wù)發(fā)現(xiàn)

簡(jiǎn)介

dubbo協(xié)議是基于TCP的二進(jìn)制私有協(xié)議,更適合作為后端微服務(wù)間的高效RPC通信協(xié)議,也導(dǎo)致dubbo協(xié)議對(duì)于前端流量接入不是很友好。在dubo框架中,有兩種方式可以解決這個(gè)問題:

  • 多協(xié)議發(fā)布【推薦】,為dubbo協(xié)議服務(wù)暴露rest風(fēng)格的http協(xié)議訪問方式。
  • 通過網(wǎng)關(guān)實(shí)現(xiàn) http->dubbo協(xié)議轉(zhuǎn)換,這種方式需要將http協(xié)議轉(zhuǎn)換為后端服務(wù)能識(shí)別的dubbo協(xié)議,要求網(wǎng)關(guān)必須支持dubbo協(xié)議。

同時(shí)發(fā)布http、dubbo協(xié)議

dubbo框架支持為同一個(gè)服務(wù)發(fā)布多個(gè)協(xié)議,并且支持客戶端通過同一個(gè)端口以不同的協(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)需要實(shí)現(xiàn)的關(guān)鍵點(diǎn):

  • 協(xié)議轉(zhuǎn)換,支持http到dubbo協(xié)議的轉(zhuǎn)換,包括參數(shù)映射。
  • 自動(dòng)地址發(fā)現(xiàn),支持Nacos、Zookeeper、Kubernetes等主流注冊(cè)中心,動(dòng)態(tài)感知后端dubbo實(shí)例變化。
  • 結(jié)合dubbo協(xié)議的路由,如在發(fā)起 dubbo 協(xié)議調(diào)用時(shí),支持按照特定規(guī)則地址篩選、傳遞附加參數(shù)到 dubbo 后端服務(wù)。

支持的開源網(wǎng)關(guān)

  • Higress
  • Apache APISIX
  • Apache Shenyu

參考HTTP網(wǎng)關(guān)接入->dubbo協(xié)議

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

岫岩| 通渭县| 绥德县| 灵石县| 辽宁省| 江城| 梅州市| 额敏县| 凤凰县| 耿马| 漳浦县| 石门县| 奉节县| 浪卡子县| 望谟县| 宽城| 湖州市| 宜都市| 阳新县| 忻城县| 蓬安县| 镇远县| 绵阳市| 岑溪市| 嘉禾县| 合肥市| 保德县| 霍林郭勒市| 车致| 永昌县| 台南县| 太原市| 额济纳旗| 怀远县| 娱乐| 洛南县| 雷山县| 瑞昌市| 呼图壁县| 子洲县| 萍乡市|