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}
}
分享到:
相关推荐
vitess golang实现的库 code.google.com上没有提供pkg下载。
开源项目-youtube-vitess.zip,Vitess is a storage platform for scaling MySQL. Brought to you by YouTube/Google.
2015 Oracle 技术嘉年华(OTN)分会场4董泽润 - Vitess Kingshard中间件的启示
vitess提供服务器和工具便于扩展MySQL数据库实现大规模web服务
Youtube出品的开源分布式MySQL工具集Vitess,自动分片存储MySQL数据表,将单个SQL查询改写为分布式发送到多个MySQL Server上,支持行缓存(比MySQL本身缓存效率高),支持复制容错,已用于Youtube生产环境,文档有待...
MySQL开源中间件Vitess,用于数据库分片,Youtube用于生产环境并开源,使用go语言开发
维特斯 Vitess是一个数据库集群系统,用于通过广义分片对MySQL进行水平扩展。 通过封装分片路由逻辑,Vitess允许应用程序代码和...除非另有说明,否则Vitess源文件将根据LICENSE文件中的Apache Version 2.0许可证
另外,某些库直接从vitess sql解析器中使用。 但是vitess的sql解析器仅部分支持DDL解析。 我们希望完美地支持SQL和DDL的解析。 因此,我们直接使用vitess sql解析器,也将TiDB解析器用于DDL解析。 比较Go中SQL解析...
tidb-vs-vitess
Vitess分布式数据库中间件