* 基于zk做地址發(fā)現(xiàn)* 基于redis的get、ping、info協(xié)議構(gòu)建協(xié)議通訊* 使用json序列化協(xié)議滿足良好跨語言兼容性* 使用當(dāng)前眾多語言的redisclient即可以完成客戶端開發(fā)。
可以使用redis-cli -h -p get/ping/info 命令訪問服務(wù)或發(fā)起服務(wù)調(diào)用
Get
exp:
get {"action":"/service/bibi/go-moa","params":{"m":"setName","args":["a"]}}
action:理解為服務(wù)名稱(service-uri)
m:需要調(diào)用該服務(wù)的方法名稱(已經(jīng)做了go和java關(guān)于方法名首字母大寫兼容)
args:m方法的調(diào)用參數(shù)序列。
PING
同 Redis的PING返回PONG
INFO
同 Redis的INFO,返回MOA和網(wǎng)絡(luò)狀態(tài)(json數(shù)據(jù)moa與network節(jié)點(diǎn))。
exp:
{"moa":{"recv":0,"proc":0,"error":0},"network":{"read_count":1,"read_bytes":9,"write_count":0,"write_bytes":0,"dispatcher_go":1,"connections":1}}
安裝ZooKeeper $Zookeeper/bin/zkServer.sh start
```go get github.com/blackbeans/go-moa/corego get github.com/blackbeans/go-moa/proxy```
定義服務(wù)的接口對應(yīng)
例如接口為:
```goalng //接口 type DemoResult struct { Hosts []string `json:"hosts"` Uri string `json:"uri"` } type IGoMoaDemo interface { GetDemoName(serviceUri, proto string) (DemoResult, error) } //服務(wù)實(shí)現(xiàn) type GoMoaDemo struct { } func (self GoMoaDemo) GetDemoName(serviceUri, proto string) ( DemoResult, error) { return DemoResult{[]string{"fuck gfw"}, serviceUri}, nil } ```
約定: 為了給客戶端友好的返回錯(cuò)誤信息,go-moa的服務(wù)接口最后一個(gè)返回必須為error類型。并且為了滿足Java單一返回結(jié)果所以返回參數(shù)最多2個(gè)。
服務(wù)端啟動(dòng)啟動(dòng):
```goalng func main(){ app := core.NewApplcation("./conf/cluster_test.toml", func() []proxy.Service { return []proxy.Service{ proxy.Service{ ServiceUri: "/service/bibi/go-moa", Instance: GoMoaDemo{}, Interface: (*IGoMoaDemo)(nil)}} }) //設(shè)置啟動(dòng)項(xiàng) ch := make(chan os.Signal, 1) signal.Notify(ch, os.Kill) //kill掉的server <-ch app.DestoryApplication() }```
說明 - Service為一個(gè)服務(wù)單元,對應(yīng)了本服務(wù)對外的服務(wù)名稱、以及對應(yīng)的接口
- Applcation需要對應(yīng)的Moa的配置文件,toml類型,具體配置參見./conf/cluster_test. toml
發(fā)布服務(wù)成功可以使用客戶端進(jìn)行測試,具體 客戶端的使用請參考
env:Macbook Pro 2.2 GHz Intel Core i7redis-benchmark result : 53527.46 requests per secondgo test --bench=".*" github.com/blackbeans/go-moa/core -run=BenchmarkApplicationBenchmarkApplication-8 20000 64517 ns/op