Kratos 傳輸協(xié)議-概覽

2022-04-25 10:06 更新

概覽

kratos 框架對(duì)傳輸層進(jìn)行了抽象,用戶可以通過實(shí)現(xiàn)接口來接入實(shí)現(xiàn),框架默認(rèn)實(shí)現(xiàn)了gRPC和HTTP兩種通信協(xié)議傳輸層。用戶在實(shí)現(xiàn)通訊協(xié)議傳輸層時(shí)可以參考一下官方實(shí)現(xiàn)的代碼。

接口抽象

  • ?server ?

// 服務(wù)的啟動(dòng)和停止,用于管理服務(wù)生命周期。
type Server interface {
    Start(context.Context) error
    Stop(context.Context) error
}

  • ?Transporter ?

type Transporter interface {
    // 代表實(shí)現(xiàn)的通訊協(xié)議的種類,如內(nèi)置的 http grpc,也可以實(shí)現(xiàn)其他的類型如 mqtt,websocket
    Kind() Kind
    // 提供的服務(wù)終端地址
    Endpoint() string
    // 用于標(biāo)識(shí)服務(wù)的完整方法路徑
    // 示例: /helloworld.Greeter/SayHello
    Operation() string
    // http 的請(qǐng)求頭或者 grpc 的元數(shù)據(jù)
    Header() Header
}

  • ?Endpointer ?

type Endpointer interface {
    // 用于實(shí)現(xiàn)注冊(cè)到注冊(cè)中心的終端地址,如果不實(shí)現(xiàn)這個(gè)方法則不會(huì)注冊(cè)到注冊(cè)中心
    Endpoint() (*url.URL, error)
}

使用方式

使用方式將 http 或 grpc 注冊(cè)到 server 中

app := kratos.New(
    kratos.Name(Name),
    kratos.Server(
        httpSrv,
    ),
)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)