金慶的專欄
C++博客
::
首頁
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
423 隨筆 :: 0 文章 :: 454 評論 :: 0 Trackbacks
公告
我的隨筆
我的評論
我參與的隨筆
留言簿
(12)
給我留言
查看公開留言
查看私人留言
隨筆分類
(502)
1. C/C++(166)
(rss)
2. 網(wǎng)游開發(fā)(108)
(rss)
3. Golang(20)
(rss)
4. Linux/Unix(30)
(rss)
5. 軟工與管理(44)
(rss)
6. Python(23)
(rss)
7. Erlang(18)
(rss)
8. Rust(16)
(rss)
9. 其它(77)
(rss)
隨筆檔案
(423)
2023年1月 (1)
2022年11月 (1)
2022年10月 (2)
2022年9月 (1)
2022年4月 (6)
2022年1月 (2)
2021年12月 (4)
2021年11月 (6)
2021年10月 (2)
2021年9月 (2)
2021年8月 (7)
2021年7月 (2)
2021年5月 (2)
2021年3月 (1)
2021年2月 (2)
2021年1月 (1)
2020年12月 (1)
2020年10月 (1)
2020年9月 (5)
2020年8月 (1)
2020年7月 (1)
2020年6月 (1)
2020年4月 (2)
2020年3月 (3)
2020年2月 (3)
2020年1月 (1)
2019年12月 (1)
2019年9月 (2)
2019年4月 (2)
2019年1月 (1)
2018年12月 (1)
2018年11月 (3)
2018年10月 (1)
2018年9月 (3)
2018年8月 (3)
2018年7月 (2)
2018年6月 (4)
2018年5月 (4)
2018年4月 (4)
2018年3月 (1)
2018年1月 (2)
2017年12月 (2)
2017年11月 (3)
2017年10月 (3)
2017年8月 (7)
2017年7月 (1)
2017年6月 (1)
2017年5月 (3)
2017年4月 (3)
2017年3月 (3)
2017年2月 (2)
2017年1月 (2)
2016年12月 (5)
2016年11月 (2)
2016年10月 (2)
2016年9月 (1)
2016年8月 (6)
2016年7月 (3)
2016年6月 (2)
2016年5月 (4)
2016年4月 (2)
2016年3月 (2)
2016年1月 (3)
2015年12月 (2)
2015年11月 (2)
2015年10月 (1)
2015年8月 (2)
2015年7月 (1)
2015年6月 (1)
2015年5月 (4)
2015年4月 (3)
2015年3月 (4)
2015年2月 (5)
2015年1月 (4)
2014年12月 (3)
2014年11月 (3)
2014年10月 (2)
2014年9月 (3)
2014年8月 (1)
2014年4月 (4)
2014年3月 (1)
2014年2月 (4)
2014年1月 (5)
2013年12月 (5)
2013年11月 (5)
2013年9月 (2)
2013年8月 (2)
2013年7月 (2)
2013年6月 (2)
2013年5月 (1)
2013年1月 (2)
2012年12月 (1)
2012年11月 (1)
2012年9月 (1)
2012年8月 (3)
2012年7月 (2)
2012年6月 (1)
2012年4月 (3)
2012年3月 (2)
2012年2月 (3)
2012年1月 (2)
2011年11月 (2)
2011年10月 (3)
2011年9月 (2)
2011年8月 (2)
2011年7月 (3)
2011年6月 (2)
2011年5月 (3)
2011年1月 (2)
2010年12月 (1)
2010年11月 (2)
2010年10月 (2)
2010年9月 (3)
2010年8月 (2)
2010年7月 (3)
2010年6月 (1)
2010年5月 (3)
2010年4月 (3)
2010年3月 (5)
2010年2月 (4)
2010年1月 (4)
2009年12月 (2)
2009年11月 (3)
2009年10月 (4)
2009年9月 (3)
2009年8月 (2)
2009年7月 (4)
2009年6月 (1)
2009年5月 (3)
2009年4月 (4)
2009年3月 (2)
2009年2月 (5)
2009年1月 (1)
2008年12月 (7)
2008年11月 (4)
2008年10月 (1)
2008年9月 (3)
2008年8月 (4)
2008年7月 (3)
2008年6月 (4)
2008年5月 (6)
2008年4月 (7)
2008年3月 (6)
2008年1月 (5)
2007年12月 (7)
2007年11月 (4)
2007年10月 (5)
2007年9月 (6)
2007年8月 (8)
2007年7月 (5)
相冊
公告照片
搜索
積分與排名
積分 - 659030
排名 - 25
最新評論
1.?re: boost::asio::spawn 將一統(tǒng)C++網(wǎng)絡(luò)庫
asio 成為C++首選網(wǎng)絡(luò)庫
--linda
2.?re: log4cxx中文輸出錯誤補丁
評論內(nèi)容較長,點擊標(biāo)題查看
--金慶
3.?re: mingw編譯OrzNet
能發(fā)送一個mingw編譯好的OrzNet庫給我嗎? liuweiqcxy@163.com
謝謝!
--劉威
4.?re: log4cxx中文輸出錯誤補丁
評論內(nèi)容較長,點擊標(biāo)題查看
--bigbad
5.?re: log4cxx中文輸出錯誤補丁
評論內(nèi)容較長,點擊標(biāo)題查看
--bigbad
閱讀排行榜
1.?"multiple definition of" 錯誤(11054)
2.?SVN中邪惡的replace(10972)
3.?VS2005編譯libevent(10429)
4.?混音算法的學(xué)習(xí)與研究(10230)
5.?C調(diào)用lua腳本的效率測試(9019)
評論排行榜
1.?VC6正在被拋棄(35)
2.?VS2005編譯libevent(21)
3.?"multiple definition of" 錯誤(18)
4.?C++引用優(yōu)于指針(17)
5.?ACE與ASIO之間關(guān)于Socket編程的比較(16)
grpc外部負(fù)載均衡器測試
# grpc外部負(fù)載均衡器測試
(金慶的專欄 2020.4)
grpc 對每個請求進(jìn)行負(fù)載均衡。負(fù)載均衡的方式有:
* 代理模式
* 客戶端實現(xiàn)
* 外部負(fù)載均衡
參考:gRPC LB https://blog.csdn.net/xiaojia1100/article/details/78842295
gRPC 中負(fù)載均衡的主要機制是外部負(fù)載均衡。
gRPC 定義了外部負(fù)載均衡服務(wù)的接口:https://github.com/grpc/grpc/tree/master/src/proto/grpc/lb/v1
* load_balancer.proto 客戶端向 lb 服查詢后端列表
* load_reporter.proto lb 服向后端服查詢負(fù)載
https://github.com/bsm/grpclb 實現(xiàn)了一個 grpc 的外部負(fù)載均衡服。
因為其實現(xiàn)早于負(fù)載均衡服的接口規(guī)范,所以接口定義與 grpc 規(guī)范不同。
見 issue#26: https://github.com/bsm/grpclb/issues/26#issuecomment-613873655
grpclb 目前僅支持 consul 服務(wù)發(fā)現(xiàn)。
標(biāo)準(zhǔn)的 grpclb 實現(xiàn)目前好像只有 https://github.com/joa/jawlb。
jawlb 通過 Kubernetes API 來發(fā)現(xiàn)服務(wù)。
以下測試 grpc 客戶端從 jawlb 服查詢服務(wù)器列表,然后請求服務(wù)。
首先在本機開了多個 greeter 服實例,端口不同。
然后更改 greeter 客戶端,不要直接連 greeter 服地址,而是配一個 jawlb 服地址。
同時更改 jawlb, 刪除服務(wù)發(fā)現(xiàn),改為固定輸出本機服務(wù)列表,定時切換。
greeter 是指 grpc-go 中的例子:grpc-go\examples\helloworld\greeter
## greeter 服更改
添加參數(shù)指定服務(wù)端口。
```
package main
import (
"fmt"
"log"
"net"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
)
// GreeterServer is used to implement helloworld.GreeterServer.
type GreeterServer struct {
}
// SayHello implements helloworld.GreeterServer
func (s *GreeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
msg := fmt.Sprintf("Hello %s from server-%d", in.Name, viper.GetInt("port"))
return &pb.HelloReply{Message: msg}, nil
}
func main() {
pflag.Int("port", 8000, "server bind port")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
port := viper.GetInt("port")
addr := fmt.Sprintf(":%d", port)
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &GreeterServer{})
s.Serve(lis)
}
```
## greeter 客戶端更改
```
package main
import (
"context"
"log"
"os"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
_ "google.golang.org/grpc/balancer/grpclb"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
)
const (
defaultName = "world"
)
func init() {
grpclog.SetLogger(logrus.New())
}
func main() {
rb := manual.NewBuilderWithScheme("whatever")
rb.InitialState(resolver.State{Addresses: []resolver.Address{
{Addr: "127.0.0.1:8888", Type: resolver.GRPCLB},
}})
conn, err := grpc.Dial("whatever:///this-gets-overwritten", grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithResolvers(rb))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
for {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
cancel()
if err != nil {
log.Fatalf("could not greet: %v", err)
time.Sleep(time.Second)
continue
}
log.Printf("Greeting: %s", r.GetMessage())
time.Sleep(time.Second)
}
}
```
有以下更改:
* import _ "google.golang.org/grpc/balancer/grpclb"
* grpc.Dial("whatever:///this-gets-overwritten", grpc.WithResolvers(rb))
+ 采用一個自定義解析器,用來獲取 jawlb 地址
+ Scheme("whatever") 可以任意,用作解析器名字
+ 目標(biāo) this-gets-overwritten 可以任意,因為 jawlb 忽略了該名字
+ 127.0.0.1:8888 是 jawlb 地址
* 改為每秒請求一次
正常的 grpclb 是在 DNS 中設(shè)置 SRV 記錄,
此處測試避免設(shè)置 DNS, 采用了一個自定義解析器,
代碼上多了幾行。
用 DNS 設(shè)置的好處是, 可以直接解析為后端 IP, 也可以添加 grpclb, 代碼上如同直接連接后端:
```
conn, err := grpc.Dial("dns:///myservice.domain.com", grpc.WithInsecure())
```
## jawlb 更改
### main.go
刪除所有配置,改為固定本機 8888 端口監(jiān)聽。
* 刪除 `envconfig.MustProcess("JAWLB", &cfg)`
* listen() 改為
```
func listen() (conn net.Listener, err error) {
conn, err = net.Listen("tcp", ":8888")
return
}
```
### watch.go
```
package main
import (
"context"
"fmt"
"net"
"time"
)
func watchService(ctx context.Context) (_ <-chan ServerList, err error) {
ch := make(chan ServerList)
go func() {
ticker := time.NewTicker(10 * time.Second)
i := 0
for {
select {
case <-ctx.Done():
ticker.Stop()
close(ch)
return
case <-ticker.C:
i += 1
fmt.Printf("i = %d\n", i)
ports := []int32{8010, 8020}
var servers []Server
for _, port := range ports {
servers = append(servers, Server{IP: net.ParseIP("127.0.0.1"), Port: port + int32(i%2)})
}
ch <- servers
} // select
} // for
}()
return ch, nil
}
```
刪除所有服務(wù)發(fā)現(xiàn)代碼,改為每10秒切換端口:8010,8020 <-> 8011,8021
## 運行
### jawlb
```
λ jawlb.exe
2020/04/16 15:35:17 waiting for TERM
i = 1
2020/04/16 15:35:27 endpoints:
2020/04/16 15:35:27 127.0.0.1:8011
2020/04/16 15:35:27 127.0.0.1:8021
i = 2
2020/04/16 15:35:37 endpoints:
2020/04/16 15:35:37 127.0.0.1:8010
2020/04/16 15:35:37 127.0.0.1:8020
```
### server
運行 4 個實例:
```
server --port 8010
server --port 8020
server --port 8011
server --port 8021
```
### client
```
λ client
INFO[0002] lbBalancer: handle SubConn state change: 0xc00008a590, CONNECTING
INFO[0002] Channel Connectivity change to CONNECTING
INFO[0002] lbBalancer: handle SubConn state change: 0xc00008a5f0, CONNECTING
INFO[0002] Subchannel picks a new address "127.0.0.1:8021" to connect
INFO[0002] Subchannel Connectivity change to READY
INFO[0002] lbBalancer: handle SubConn state change: 0xc00008a590, READY
INFO[0002] Channel Connectivity change to READY
INFO[0002] Subchannel Connectivity change to READY
INFO[0002] lbBalancer: handle SubConn state change: 0xc00008a5f0, READY
2020/04/16 15:37:47 Greeting: Hello world from server-8021
2020/04/16 15:37:48 Greeting: Hello world from server-8011
2020/04/16 15:37:49 Greeting: Hello world from server-8021
2020/04/16 15:37:50 Greeting: Hello world from server-8011
2020/04/16 15:37:51 Greeting: Hello world from server-8021
2020/04/16 15:37:52 Greeting: Hello world from server-8011
2020/04/16 15:37:53 Greeting: Hello world from server-8021
2020/04/16 15:37:54 Greeting: Hello world from server-8011
2020/04/16 15:37:55 Greeting: Hello world from server-8021
2020/04/16 15:37:56 Greeting: Hello world from server-8011
INFO[0012] lbBalancer: processing server list: servers:<ip_address:"\000\000\000\000\000\000\000\000\000\000\377\377\177\000\000\001" port:8020 > servers:<ip_address:"\000\000\000\000\000\000\000\000\000\000\377\377\177\000\000\001" port:8010 >
INFO[0012] lbBalancer: server list entry[0]: ipStr:|127.0.0.1|, port:|8020|, load balancer token:||
INFO[0012] lbBalancer: server list entry[1]: ipStr:|127.0.0.1|, port:|8010|, load balancer token:||
2020/04/16 15:37:57 Greeting: Hello world from server-8020
2020/04/16 15:37:58 Greeting: Hello world from server-8010
2020/04/16 15:37:59 Greeting: Hello world from server-8020
2020/04/16 15:38:00 Greeting: Hello world from server-8010
2020/04/16 15:38:01 Greeting: Hello world from server-8020
2020/04/16 15:38:02 Greeting: Hello world from server-8010
2020/04/16 15:38:03 Greeting: Hello world from server-8020
2020/04/16 15:38:04 Greeting: Hello world from server-8010
2020/04/16 15:38:05 Greeting: Hello world from server-8020
2020/04/16 15:38:06 Greeting: Hello world from server-8010
INFO[0022] lbBalancer: processing server list: servers:<ip_address:"\000\000\000\000\000\000\000\000\000\000\377\377\177\000\000\001" port:8021 > servers:<ip_address:"\000\000\000\000\000\000\000\000\000\000\377\377\177\000\000\001" port:8011 >
INFO[0022] lbBalancer: server list entry[0]: ipStr:|127.0.0.1|, port:|8021|, load balancer token:||
INFO[0022] lbBalancer: server list entry[1]: ipStr:|127.0.0.1|, port:|8011|, load balancer token:||
2020/04/16 15:38:07 Greeting: Hello world from server-8011
2020/04/16 15:38:08 Greeting: Hello world from server-8021
2020/04/16 15:38:09 Greeting: Hello world from server-8011
```
## 結(jié)論
客戶端應(yīng)用一個自定義 resolver 解析 "whatever:///this-gets-overwritten",
獲取到 `{Addr: "127.0.0.1:8888", Type: resolver.GRPCLB}`,
知道這是一個 grpclb,于是按 load_balancer.proto 的定義查詢 jawlb 來獲取后端地址列表。
jawlb 每 10s 更新一次服務(wù)器列表,每次輸出多個地址??蛻舳嗽诙鄠€地址間輪換請求。
## 其他測試
* 不開 jawlb,客戶端將無法成功請求,直到 jawlb 開啟才成功
* 中途關(guān)閉 jawlb, 請求仍會成功,但是保持為最后的服務(wù)器列表
+ 同時會不斷嘗試重連 jawlb, 但是重連成功后沒有切換服務(wù),應(yīng)該是個錯誤
* Dial() 不加 grpc.WithBlock() 參數(shù), 報錯:all SubConns are in TransientFailure
```
λ client
INFO[0000] parsed scheme: "whatever"
INFO[0000] ccResolverWrapper: sending update to cc: {[{127.0.0.1:8888 <nil> 1 <nil>}] <nil> <nil>}
INFO[0000] ClientConn switching balancer to "grpclb"
INFO[0000] Channel switches to new LB policy "grpclb"
INFO[0000] lbBalancer: UpdateClientConnState: {ResolverState:{Addresses:[{Addr:127.0.0.1:8888 ServerName: Attributes:<nil> Type:1 Metadata:<nil>}] ServiceConfig:<nil> Attributes:<nil>} BalancerConfig:<nil>}
INFO[0000] parsed scheme: "grpclb-internal"
INFO[0000] ccResolverWrapper: sending update to cc: {[{127.0.0.1:8888 <nil> 0 <nil>}] <nil> <nil>}
INFO[0000] ClientConn switching balancer to "pick_first"
INFO[0000] Channel switches to new LB policy "pick_first"
INFO[0000] Subchannel Connectivity change to CONNECTING
INFO[0000] blockingPicker: the picked transport is not ready, loop back to repick
INFO[0000] pickfirstBalancer: HandleSubConnStateChange: 0xc00003fb10, {CONNECTING <nil>}
INFO[0000] Channel Connectivity change to CONNECTING
INFO[0000] Subchannel picks a new address "127.0.0.1:8888" to connect
INFO[0000] CPU time info is unavailable on non-linux or appengine environment.
INFO[0000] Subchannel Connectivity change to READY
INFO[0000] pickfirstBalancer: HandleSubConnStateChange: 0xc00003fb10, {READY <nil>}
INFO[0000] Channel Connectivity change to READY
INFO[0000] lbBalancer: processing server list: servers:<ip_address:"\000\000\000\000\000\000\000\000\000\000\377\377\177\000\000\001" port:8010 > servers:<ip_address:"\000\000\000\000\000\000\000\000\000\000\377\377\177\000\000\001" port:8020 >
INFO[0000] lbBalancer: server list entry[0]: ipStr:|127.0.0.1|, port:|8010|, load balancer token:||
INFO[0000] lbBalancer: server list entry[1]: ipStr:|127.0.0.1|, port:|8020|, load balancer token:||
INFO[0000] Subchannel Connectivity change to CONNECTING
INFO[0000] Subchannel Connectivity change to CONNECTING
INFO[0000] Channel Connectivity change to TRANSIENT_FAILURE
INFO[0000] lbBalancer: handle SubConn state change: 0xc00008a220, CONNECTING
INFO[0000] Channel Connectivity change to CONNECTING
INFO[0000] lbBalancer: handle SubConn state change: 0xc00008a280, CONNECTING
2020/04/16 16:40:06 could not greet: rpc error: code = Unavailable desc = all SubConns are in TransientFailure
```
posted on 2020-04-17 10:08
金慶
閱讀(1161)
評論(0)
編輯
收藏
引用
所屬分類:
3. Golang
只有注冊用戶
登錄
后才能發(fā)表評論。
【推薦】100%開源!大型工業(yè)跨平臺軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
net.LookupSRV()查詢k8s無頭服務(wù)
rpc應(yīng)答太快造成請求超時
golang各數(shù)值類型的最大最小值
參數(shù)太靈活容易出錯
grpc外部負(fù)載均衡器測試
數(shù)組make參數(shù)錯誤
go不要導(dǎo)出channel
go代碼覆蓋測試
考察go一致性hash庫
將go函數(shù)指針轉(zhuǎn)為接口
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Powered by:
C++博客
Copyright © 金慶
欧美粉嫩小泬久久久久久久
|
伊人丁香狠狠色综合久久
|
久久亚洲精品无码AV红樱桃
|
粉嫩小泬无遮挡久久久久久
|
久久99中文字幕久久
|
久久久久久免费视频
|
久久久久久久亚洲Av无码
|
久久精品成人影院
|
2020最新久久久视精品爱
|
一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区
|
99久久精品国产麻豆
|
国内高清久久久久久
|
久久精品一区二区影院
|
久久亚洲欧美日本精品
|
青青热久久综合网伊人
|
久久ww精品w免费人成
|
avtt天堂网久久精品
|
久久精品国产99国产电影网
|
亚洲人成伊人成综合网久久久
|
色天使久久综合网天天
|
日韩va亚洲va欧美va久久
|
久久久久久国产精品无码下载
|
91久久香蕉国产熟女线看
|
国产99久久精品一区二区
|
精品久久久久国产免费
|
久久黄视频
|
亚洲精品乱码久久久久久蜜桃不卡
|
久久亚洲精品成人无码网站
|
久久久久se色偷偷亚洲精品av
|
日韩av无码久久精品免费
|
国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲
|
午夜精品久久久久9999高清
|
亚洲欧美成人综合久久久
|
91精品国产综合久久香蕉
|
亚洲精品乱码久久久久久自慰
|
国产精品热久久无码av
|
性欧美大战久久久久久久久
|
性做久久久久久久久浪潮
|
美女久久久久久
|
亚洲天堂久久久
|
久久九九兔免费精品6
|