国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

            手機站
            千鋒教育

            千鋒學習站 | 隨時隨地免費學

            千鋒教育

            掃一掃進入千鋒手機站

            領(lǐng)取全套視頻
            千鋒教育

            關(guān)注千鋒學習站小程序
            隨時隨地免費學習課程

            當前位置:首頁  >  技術(shù)干貨  > 如何使用golang實現(xiàn)自定義RPC框架

            如何使用golang實現(xiàn)自定義RPC框架

            來源:千鋒教育
            發(fā)布人:xqq
            時間: 2023-12-27 05:03:46 1703624626

            如何使用golang實現(xiàn)自定義RPC框架

            RPC (Remote Procedure Call)是一種遠程調(diào)用協(xié)議,通過網(wǎng)絡(luò)傳輸,使得程序能夠像本地調(diào)用一樣調(diào)用遠程服務(wù)。在現(xiàn)代微服務(wù)架構(gòu)中,RPC協(xié)議被廣泛使用。golang通過標準庫的net/rpc包提供了一套RPC框架,但是這個框架無法滿足一些特定的業(yè)務(wù)需求,本文就來介紹如何使用golang自己實現(xiàn)一個RPC框架。

            1. 基本概念

            在實現(xiàn)自定義RPC框架之前,需要先了解以下幾個基本概念:

            - Service:RPC調(diào)用的服務(wù),即提供RPC服務(wù)的函數(shù)集合。

            - Method:Service中的方法,即具體的RPC調(diào)用方法。

            - Codec:序列化和反序列化的方法,將調(diào)用的參數(shù)和返回值序列化成二進制數(shù)據(jù),以便通過網(wǎng)絡(luò)傳輸。

            - Transport:網(wǎng)絡(luò)傳輸協(xié)議,用于將序列化后的二進制數(shù)據(jù)通過網(wǎng)絡(luò)傳輸?shù)竭h程服務(wù)。

            2. 實現(xiàn)步驟

            接下來我們就來實現(xiàn)一個簡單的自定義RPC框架,步驟如下:

            - 定義Service和Method

            - 實現(xiàn)Codec

            - 實現(xiàn)Transport

            - 完成框架

            2.1 定義Service和Method

            我們以一個簡單的計算器服務(wù)為例,在服務(wù)端提供兩個方法Add和Multiply,客戶端可以通過RPC調(diào)用這兩個方法。

            定義服務(wù):

            `go

            // 定義CalculatorService接口

            type CalculatorService interface {

            Add(int, int) int

            Multiply(int, int) int

            }

            // 實現(xiàn)具體的CalculatorService

            type CalculatorServiceImpl struct {}

            func (c *CalculatorServiceImpl) Add(a, b int) int {

            return a + b

            }

            func (c *CalculatorServiceImpl) Multiply(a, b int) int {

            return a * b

            }

            定義Service和Method之后,接下來需要定義一個struct來存儲Service和其對應(yīng)的Method。同時,定義一個Register方法,用于注冊新的Service和Method。`gotype Server struct {    services map*service}type service struct {    typ    reflect.Type    method map*methodType}type methodType struct {    method    reflect.Method    ArgType   reflect.Type    ReplyType reflect.Type}func (s *Server) Register(receiver interface{}) error {    service := new(service)    service.typ = reflect.TypeOf(receiver).Elem()    service.method = make(map*methodType)    for i := 0; i < service.typ.NumMethod(); i++ {        method := service.typ.Method(i)        mType := method.Type        if mType.NumIn() != 3 || mType.NumOut() != 1 {            continue        }        argType := mType.In(1)        replyType := mType.In(2)        if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) {            continue        }        service.method = &methodType{            method:    method,            ArgType:   argType,            ReplyType: replyType,        }    }    s.services = service    return nil}func isExportedOrBuiltinType(t reflect.Type) bool {    pkgPath := t.PkgPath()    return pkgPath == "" || pkgPath == "builtin"}

            在Register方法中,循環(huán)遍歷service.typ中的所有方法,將滿足條件的方法添加到service.method中。最后將service添加到Server.services中。

            2.2 實現(xiàn)Codec

            Codec用于將調(diào)用的參數(shù)和返回值序列化成二進制數(shù)據(jù),以便通過網(wǎng)絡(luò)傳輸。

            在這里,我們使用golang的標準庫encoding/gob實現(xiàn)Codec。Gob是golang標準庫中的編解碼庫,支持任意類型的編解碼和傳輸,比JSON和XML更高效。在實現(xiàn)Codec之前,需要先定義一個request結(jié)構(gòu)體和response結(jié)構(gòu)體,用于存儲調(diào)用信息和返回信息。

            `go

            type request struct {

            ServiceMethod string // 形如"Service.Method"

            Seq uint64 // 請求序列號

            Args byte // 客戶端傳遞的參數(shù)

            }

            type response struct {

            Seq uint64 // 請求序列號

            ServiceMethod string // 形如"Service.Method"

            Error string // 存儲錯誤信息

            Reply byte // 存儲響應(yīng)參數(shù)

            }

            接下來實現(xiàn)Codec,具體實現(xiàn)代碼如下:`gotype Codec struct {    conn io.ReadWriteCloser    dec  *gob.Decoder    enc  *gob.Encoder    mutex sync.Mutex    ids   uint64    pending map*call}type call struct {    req  *request    resp *response    done chan *call}func (c *Codec) WriteRequest(method string, args interface{}) (uint64, error) {    c.mutex.Lock()    defer c.mutex.Unlock()    id := c.ids    c.ids++    req := &request{        ServiceMethod: method,        Seq:           id,    }    buf := bytes.NewBuffer(nil)    enc := gob.NewEncoder(buf)    if err := enc.Encode(args); err != nil {        return 0, err    }    req.Args = buf.Bytes()    call := &call{        req:  req,        resp: new(response),        done: make(chan *call),    }    c.pending = call    if err := c.enc.Encode(req); err != nil {        delete(c.pending, id)        return 0, err    }    return id, nil}func (c *Codec) ReadResponseHeader() (*rpc.Response, error) {    c.mutex.Lock()    defer c.mutex.Unlock()    var resp response    if err := c.dec.Decode(&resp); err != nil {        return nil, err    }    call := c.pending    delete(c.pending, resp.Seq)    call.resp = &resp    call.done <- call    return &rpc.Response{        ServiceMethod: resp.ServiceMethod,        Seq:           resp.Seq,        Error:         errors.New(resp.Error),    }, nil}func (c *Codec) ReadResponseBody(x interface{}) error {    c.mutex.Lock()    defer c.mutex.Unlock()    call := <-c.pending.done    if call.resp.Error != "" {        return errors.New(call.resp.Error)    }    dec := gob.NewDecoder(bytes.NewBuffer(call.resp.Reply))    return dec.Decode(x)}

            在上面的代碼中,我們使用了一個pending map來存儲請求的序列號和請求的返回值。在WriteRequest方法中,我們將請求信息編碼成二進制數(shù)據(jù),然后將請求信息和該請求的channel存儲到pending中。在ReadResponseHeader和ReadResponseBody方法中,我們根據(jù)pending中的請求序列號獲取該請求對應(yīng)的call,然后將call.resp進行解碼后返回。

            2.3 實現(xiàn)Transport

            Transport用于將序列化后的二進制數(shù)據(jù)通過網(wǎng)絡(luò)傳輸?shù)竭h程服務(wù)。

            在golang中,可以使用net包來實現(xiàn)簡單的Socket編程。在這里,我們通過net.Dial建立連接后,將Codec中序列化后的數(shù)據(jù)通過Socket發(fā)送到遠程服務(wù)端。

            `go

            type Transport struct {

            conn io.ReadWriteCloser

            }

            func (t *Transport) Dial(network, address string) error {

            conn, err := net.Dial(network, address)

            if err != nil {

            return err

            }

            t.conn = conn

            return nil

            }

            func (t *Transport) Close() error {

            return t.conn.Close()

            }

            func (t *Transport) Codec() rpc.ClientCodec {

            return &Codec{

            conn: t.conn,

            dec: gob.NewDecoder(t.conn),

            enc: gob.NewEncoder(t.conn),

            pending: make(map*call),

            }

            }

            2.4 完成框架最后,我們完成自定義RPC框架的實現(xiàn)。具體代碼如下:`gotype Server struct {    services map*service}type service struct {    typ    reflect.Type    method map*methodType}type methodType struct {    method    reflect.Method    ArgType   reflect.Type    ReplyType reflect.Type}func (s *Server) Register(receiver interface{}) error {    service := new(service)    service.typ = reflect.TypeOf(receiver).Elem()    service.method = make(map*methodType)    for i := 0; i < service.typ.NumMethod(); i++ {        method := service.typ.Method(i)        mType := method.Type        if mType.NumIn() != 3 || mType.NumOut() != 1 {            continue        }        argType := mType.In(1)        replyType := mType.In(2)        if !isExportedOrBuiltinType(argType) || !isExportedOrBuiltinType(replyType) {            continue        }        service.method = &methodType{            method:    method,            ArgType:   argType,            ReplyType: replyType,        }    }    s.services = service    return nil}func (s *Server) ServeCodec(codec rpc.ServerCodec) error {    for {        req, err := codec.ReadRequestHeader()        if err != nil {            if err != io.EOF && err != io.ErrUnexpectedEOF {                log.Println("rpc server:", err)            }            return err        }        serviceMethod := req.ServiceMethod        dot := strings.LastIndex(serviceMethod, ".")        if dot < 0 {            err := errors.New("rpc server: service/method request ill-formed: " + serviceMethod)            log.Println(err.Error())            resp := &rpc.Response{                ServiceMethod: serviceMethod,                Seq:           req.Seq,                Error:         err.Error(),            }            codec.WriteResponse(resp, nil)            continue        }        serviceName, methodName := serviceMethod, serviceMethod        service, ok := s.services        if !ok {            err := errors.New("rpc server: can't find service " + serviceName)            log.Println(err.Error())            resp := &rpc.Response{                ServiceMethod: serviceMethod,                Seq:           req.Seq,                Error:         err.Error(),            }            codec.WriteResponse(resp, nil)            continue        }        mtype, ok := service.method        if !ok {            err := errors.New("rpc server: can't find method " + methodName)            log.Println(err.Error())            resp := &rpc.Response{                ServiceMethod: serviceMethod,                Seq:           req.Seq,                Error:         err.Error(),            }            codec.WriteResponse(resp, nil)            continue        }        argv := reflect.New(mtype.ArgType)        replyv := reflect.New(mtype.ReplyType).Elem()        if err = codec.ReadRequestBody(argv.Interface()); err != nil {            log.Println("rpc server: ", err)            resp := &rpc.Response{                ServiceMethod: serviceMethod,                Seq:           req.Seq,                Error:         err.Error(),            }            codec.WriteResponse(resp, nil)            continue        }        // Call the service method.        returnValues := mtype.method.Func.Call(reflect.Value{            reflect.ValueOf(service),            reflect.ValueOf(argv.Interface()),            replyv,        })        // The return value for the method is an error.        errInter := returnValues.Interface()        if errInter != nil {            err := errInter.(error)            log.Println("rpc server: ", err)            resp := &rpc.Response{                ServiceMethod: serviceMethod,                Seq:           req.Seq,                Error:         err.Error(),            }            codec.WriteResponse(resp, nil)            continue        }        resp := &rpc.Response{            ServiceMethod: serviceMethod,            Seq:           req.Seq,            Error:         "",        }        if err = codec.WriteResponse(resp, replyv.Interface()); err != nil {            log.Println("rpc server: ", err)        }    }}func (s *Server) ServeTransport(transport *Transport) error {    codec := transport.Codec()    defer transport.Close()    return s.ServeCodec(codec)}func isExportedOrBuiltinType(t reflect.Type) bool {    pkgPath := t.PkgPath()    return pkgPath == "" || pkgPath == "builtin"}

            在上面的代碼中,我們定義了一個Server結(jié)構(gòu)體,用于注冊Service和Method,同時實現(xiàn)ServeCodec和ServeTransport方法,用于在服務(wù)端處理RPC請求。

            3. 測試

            完成自定義RPC框架的實現(xiàn)之后,我們需要對其進行測試。下面我們將分別在服務(wù)端和客戶端使用該RPC框架。

            服務(wù)端代碼:

            `go

            func main() {

            server := new(Server)

            server.services = make(map*service)

            _ = server.Register(&CalculatorServiceImpl{})

            transport := new(Transport)

            _ = transport.Dial("tcp", "localhost:8080")

            defer transport.Close()

            log.Fatal(server.ServeTransport(transport))

            }

            在服務(wù)端,我們首先通過Server.Register方法注冊了一個CalculatorServiceImpl服務(wù),然后使用Transport.Dial方法連接到特定的地址??蛻舳舜a:`gofunc main() {    transport := new(Transport)    _ = transport.Dial("tcp", "localhost:8080")    defer transport.Close()    client := rpc.NewClientWithCodec(transport.Codec())    var res int    err := client.Call("CalculatorService.Add", int{10, 20}, &res)    if err != nil {        log.Fatal(err)    }    log.Printf("Add(10, 20) = %d", res)    var mul int    err = client.Call("CalculatorService.Multiply", int{10, 20}, &mul)    if err != nil {        log.Fatal(err)    }    log.Printf("Multiply(10, 20) = %d", mul)}

            在客戶端,我們首先通過Transport.Dial方法連接到服務(wù)端,然后通過rpc.NewClientWithCodec方法創(chuàng)建一個客戶端,并使用client.Call方法調(diào)用服務(wù)端的方法。

            最后,我們啟動服務(wù)端和客戶端,可以看到客戶端成功調(diào)用了服務(wù)端提供的Add和Multiply方法。

            4. 總結(jié)

            本文介紹了如何使用golang實現(xiàn)自定義RPC框架,包括定義Service和Method,實現(xiàn)Codec和Transport,完成框架等步驟,并通過一個簡單的計算器服務(wù)對該RPC框架進行了測試。該自定義RPC框架適用于一些特定的業(yè)務(wù)需求,可以滿足不同的RPC調(diào)用場景。

            以上就是IT培訓機構(gòu)千鋒教育提供的相關(guān)內(nèi)容,如果您有web前端培訓鴻蒙開發(fā)培訓,python培訓,linux培訓,java培訓,UI設(shè)計培訓等需求,歡迎隨時聯(lián)系千鋒教育。

            tags:
            聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
            10年以上業(yè)內(nèi)強師集結(jié),手把手帶你蛻變精英
            請您保持通訊暢通,專屬學習老師24小時內(nèi)將與您1V1溝通
            免費領(lǐng)取
            今日已有369人領(lǐng)取成功
            劉同學 138****2860 剛剛成功領(lǐng)取
            王同學 131****2015 剛剛成功領(lǐng)取
            張同學 133****4652 剛剛成功領(lǐng)取
            李同學 135****8607 剛剛成功領(lǐng)取
            楊同學 132****5667 剛剛成功領(lǐng)取
            岳同學 134****6652 剛剛成功領(lǐng)取
            梁同學 157****2950 剛剛成功領(lǐng)取
            劉同學 189****1015 剛剛成功領(lǐng)取
            張同學 155****4678 剛剛成功領(lǐng)取
            鄒同學 139****2907 剛剛成功領(lǐng)取
            董同學 138****2867 剛剛成功領(lǐng)取
            周同學 136****3602 剛剛成功領(lǐng)取
            相關(guān)推薦HOT
            如何解決Goland中遇到的一些常見問題

            如何解決Goland中遇到的一些常見問題Goland是JetBrain公司開發(fā)的一款非常優(yōu)秀的Go語言開發(fā)工具,具有豐富的功能和高效的開發(fā)體驗。但是在開發(fā)過...詳情>>

            2023-12-27 06:17:40
            用Goland打造高效的Go語言開發(fā)環(huán)境

            Go語言是一種近年來越來越受歡迎的編程語言,它具有高效、簡潔、快速的特性, 適用于網(wǎng)絡(luò)應(yīng)用、分布式系統(tǒng)、云計算、容器等領(lǐng)域。而在Go語言的...詳情>>

            2023-12-27 06:10:38
            如何提高Goland的開發(fā)效率快捷鍵指南

            如何提高Goland的開發(fā)效率:快捷鍵指南Goland是一款功能強大的IDE,被廣泛應(yīng)用于Go語言開發(fā)。在開發(fā)過程中,我們經(jīng)常需要進行各種操作,例如快...詳情>>

            2023-12-27 06:05:21
            使用Goland進行微服務(wù)開發(fā)的最佳實踐

            使用Goland進行微服務(wù)開發(fā)的最佳實踐隨著微服務(wù)架構(gòu)模式的流行,微服務(wù)開發(fā)也成為了當下的熱門技術(shù)。而使用Go語言進行微服務(wù)開發(fā)也是一種極具優(yōu)...詳情>>

            2023-12-27 06:00:05
            高效Go開發(fā)使用Goland的技巧與技能

            高效Go開發(fā):使用Goland的技巧與技能Goland是一款由JetBrains公司開發(fā)的集成開發(fā)環(huán)境(IDE), 專門用于Go語言的開發(fā)。該IDE具有強大的功能和豐...詳情>>

            2023-12-27 05:53:02