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
` % parts.path)
while True:
data = self.conn.recv(1024)
if not data:
raise GoRpcError(`Unexpected EOF in handshake`)
if `
` 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 ") // 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} }
相關文章
- React技巧之發出http請求ReactHTTP
- 【HTTP】HTTP請求體中的四種格式HTTP
- 淺嘗flutter中的http請求FlutterHTTP
- http請求概述HTTP
- Jsoup http請求JSHTTP
- go http請求GoHTTP
- HTTP請求中的referrer和Referrer-PolicyHTTP
- HTTP的請求過程HTTP
- golang 的 http 請求池GolangHTTP
- Python中get、post請求詳解(HTTP請求頭、狀態碼)PythonHTTP
- 合併HTTP請求vs並行HTTP請求,到底誰更快?HTTP並行
- 合併HTTP請求 vs 並行HTTP請求,到底誰更快?HTTP並行
- JavaScript中發出HTTP請求最常用的方法JavaScriptHTTP
- http請求中get和post方法的區別HTTP
- HTTP的請求與響應HTTP
- golang常用的http請求操作GolangHTTP
- HTTP請求報文HTTP
- Cookie 與 HTTP請求CookieHTTP
- python做http請求PythonHTTP
- HTTP請求中 request payload 和 formData 區別?HTTPORM
- Flutter中http請求抓包解決方案FlutterHTTP
- HTTP請求中的Form Data與Request Payload的區別HTTPORM
- 處理 HTTP 請求的註解HTTP
- 型別安全的 Go HTTP 請求型別GoHTTP
- 聊聊 HTTP 常見的請求方式HTTP
- Http請求資料格式HTTP
- HTTP 請求與響應HTTP
- Http請求與響應HTTP
- Http請求相關(轉)HTTP
- go搞笑http請求庫GoHTTP
- HTTP網路請求原理HTTP
- go http請求流程分析GoHTTP
- HTTP請求預設值HTTP
- java傳送http請求JavaHTTP
- HTTP GET請求傳bodyHTTP
- 七種HTTP請求方法HTTP
- Http請求體詳解HTTP
- Java實現Http請求JavaHTTP
- HTTP請求格式和HTTP響應格式HTTP