1. grpc的负载均衡策略

文档

2. go使用grpc负载均衡

grpc-consul-resolver地址

3. 关于serverconfig

官方文档

4. go的grpc测试

  1. package main
  2. import (
  3. "OldPackageTest/grpclb_test/proto"
  4. "context"
  5. "fmt"
  6. "log"
  7. _ "github.com/mbobakov/grpc-consul-resolver" // It's important
  8. "google.golang.org/grpc"
  9. )
  10. func main() {
  11. conn, err := grpc.Dial(
  12. "consul://192.168.1.103:8500/user-srv?wait=14s&tag=srv",
  13. grpc.WithInsecure(),
  14. grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
  15. )
  16. if err != nil {
  17. log.Fatal(err)
  18. }
  19. defer conn.Close()
  20. for i := 0; i<10; i++{
  21. userSrvClient := proto.NewUserClient(conn)
  22. rsp, err := userSrvClient.GetUserList(context.Background(), &proto.PageInfo{
  23. Pn: 1,
  24. PSize: 2,
  25. })
  26. if err != nil {
  27. panic(err)
  28. }
  29. for index, data := range rsp.Data{
  30. fmt.Println(index, data)
  31. }
  32. }
  33. }