閱讀484 返回首頁    go 阿裏雲 go 技術社區[雲棲]


vitess中rpc兼容http請求的技巧

vitess是google的一個mysql項目,用go和python實現。https://code.google.com/p/vitess/

vitess中用rpc方式來中轉mysql的請求,其中rpc的實現很有意思,兼容了http請求。

兼容http請求有明顯的好處:

1.可以用現成的監控工具來監控服務是否正常,不用另外寫插件

2.可以方便地實現查詢信息功能,不用另外再開發工具或者界麵

3.可以方便地用現成的工具測試


在vitess中很簡單地實現了這個功能。client在建立連接後,第一個包是http頭,而server端也會有一個200的回應。

詳細見代碼:

https://code.google.com/p/vitess/source/browse/py/net/gorpc.py#87

https://code.google.com/p/vitess/source/browse/go/rpcwrap/rpcwrap.go

python client:

class _GoRpcConn(object):
  def __init__(self, timeout):
    self.conn = None
    self.timeout = timeout
    self.start_time = None

  def dial(self, uri):
    parts = urlparse.urlparse(uri)
    netloc = parts.netloc.split(':')
    # NOTE(msolomon) since the deadlines are approximate in the code, set
    # timeout to oversample to minimize waiting in the extreme failure mode.
    socket_timeout = self.timeout / 10.0
    self.conn = socket.create_connection((netloc[0], int(netloc[1])),
                                         socket_timeout)
    self.conn.sendall('CONNECT %s HTTP/1.0\n\n' % parts.path)
    while True:
      data = self.conn.recv(1024)
      if not data:
        raise GoRpcError('Unexpected EOF in handshake')
      if '\n\n' in data:
        return

go server:

const (
        connected = "200 Connected to Go RPC"
)

type ClientCodecFactory func(conn io.ReadWriteCloser) rpc.ClientCodec

type BufferedConnection struct {
        *bufio.Reader
        io.WriteCloser
}

func NewBufferedConnection(conn io.ReadWriteCloser) *BufferedConnection {
        return &BufferedConnection{bufio.NewReader(conn), conn}
}

// DialHTTP connects to a go HTTP RPC server using the specified codec.
func DialHTTP(network, address, codecName string, cFactory ClientCodecFactory) (*rpc.Client, error) {
        var err error
        conn, err := net.Dial(network, address)
        if err != nil {
                return nil, err
        }
        io.WriteString(conn, "CONNECT "+GetRpcPath(codecName)+" HTTP/1.0\n\n")

        // Require successful HTTP response
        // before switching to RPC protocol.
        buffered := NewBufferedConnection(conn)
        resp, err := http.ReadResponse(buffered.Reader, &http.Request{Method: "CONNECT"})
        if err == nil && resp.Status == connected {
                return rpc.NewClientWithCodec(cFactory(buffered)), nil
        }
        if err == nil {
                err = errors.New("unexpected HTTP response: " + resp.Status)
        }
        conn.Close()
        return nil, &net.OpError{"dial-http", network + " " + address, nil, err}
}



最後更新:2017-04-02 16:47:50

  上一篇:go 判斷用戶輸入的數字是一個合法的電話號碼的正則表達式
  下一篇:go C語言圖形函數編程