编程知识 cdmana.com

Based on http2, the 9-member group moved an RPC brick, Shuimu Shuang. It's gone and never comes again

 

Manual implementation RPC

Code link : HalokidRPC

Form of agreement

type Halokid struct {
    Header          string      
    Service         string      
    Method          string      
    Payload         []byte      
}

communication mode

HTTP2, About http2 Please help yourself research

Server side

Routinely HTTP2 need Use TLS, Need to generate Certificate , however go One doesn't need to TLS You can use HTTP2 The implementation of the , It is H2C, It is based on  TCP Built on top of HTTP2, Have http2 All the characteristics of , But the data is not encrypted , Lack of security , Our server uses H2C To achieve

PS

https, be based on TLS Built on top of HTTP2

h2c, Directly in TCP Built on top of HTTP/2, The agreement also works http

func main() {
  s := server.NewServer()
  s.RegisterSvc("Echo", new(service.Echo))
  log.Printf("s -------------- %+v", s)
  s.Run(":9527")
}

client

The default is browser (postman) And so on , Currently only supported TLS Above HTTP2( namely https), So this RPC If the server uses the browser to request , Will retire to HTTP1. Directly in code client To request , The agreement is HTTP2.

use client Code to request , The returned data is as follows :

&{Status:200 OK StatusCode:200 Proto:HTTP/2.0 ProtoMajor:2 ProtoMinor:0 Header:map[Content-Length:[19] Content-Type:[text/plain; charset=utf-8] Date:[Sat, 30 Jan 2021 12:30:43 GMT]] Body:{cs:0xc0000ecdc0} ContentLength:19 TransferEncoding:[] Close:false Uncompressed:false Trailer:map[] Request:0xc000104000 TLS:}

func main() {
  client := http.Client{
    Transport:     &http2.Transport{
      AllowHTTP:  true,
      DialTLS: func(network, addr string, cfg *tls.Config) (conn net.Conn, err error) {
        return net.Dial(network, addr)
      },
    },
  }

  url := "http://127.0.0.1:9527"
  payload, err := json.Marshal(map[string]string {
    "name": "halokid",
  })
  ColorfulRabbit.CheckError(err, " Client serialization payload Request error ")

  req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload))
  req.Header.Add("svc", "Echo")
  req.Header.Add("method", "Say")
  rsp, _ := client.Do(req)
  log.Printf("RPC Request returns  --------- %+v", rsp)
  defer rsp.Body.Close()
  reply, _ := ioutil.ReadAll(rsp.Body)
  log.Printf("RPC Request returns reply --------- %+v", string(reply))
}

Service invocation process

 

 

Link to this article :https://www.pangulab.com/post/kb-yo8n7r.html,  Participate in comments »

--EOF--

Published in  2021-01-30 13:06:00 , And was added 「 rpc 」 label .

This site uses 「 A signature 4.0 The international 」 Creative sharing agreement , Please indicate the author and the original website for reprint . More about »

Architects 39 Group Edition -- Scalability design »

版权声明
本文为[other]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/02/20210201005155638j.html

Scroll to Top