Code Monkey home page Code Monkey logo

grpc-lb's Introduction

This repo is no longer maintained

grpc-lb

This is a gRPC load balancing library for go.

Feature

  • supports Random, RoundRobin, LeastConnection and ConsistentHash strategies.
  • supports etcd,consul and zookeeper as a registry.

Example

package main

import (
	etcd "github.com/coreos/etcd/client"
	"github.com/liyue201/grpc-lb/balancer"
	"github.com/liyue201/grpc-lb/examples/proto"
	registry "github.com/liyue201/grpc-lb/registry/etcd"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"log"
	"time"
)

func main() {
	etcdConfg := etcd.Config{
		Endpoints: []string{"http://10.0.101.68:2379"},
	}
	registry.RegisterResolver("etcd", etcdConfg, "/backend/services", "test", "1.0")

	c, err := grpc.Dial("etcd:///", grpc.WithInsecure(), grpc.WithBalancerName(balancer.RoundRobin))
	if err != nil {
		log.Printf("grpc dial: %s", err)
		return
	}
	defer c.Close()

	client := proto.NewTestClient(c)
	for i := 0; i < 500; i++ {

		resp, err := client.Say(context.Background(), &proto.SayReq{Content: "round robin"})
		if err != nil {
			log.Println(err)
			time.Sleep(time.Second)
			continue
		}
		time.Sleep(time.Second)
		log.Printf(resp.Content)
	}
}

see more examples

Stargazers over time

Stargazers over time

grpc-lb's People

Contributors

linfangrong avatar liyue201 avatar zhangpanyi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grpc-lb's Issues

错误的ticker时间

registry/etcd3/registrar.go第95行的ticker的时间,经过NewRegistrar赋值时的/ time.Second操作后,已经变成ms了,导致下面这个ticker的间隔时间成了2ms(我不确定你是不是想要2ms这个效果,应该不需要这么快吧)。

ticker := time.NewTicker(e.ttl / 5)

建议改成这样

ticker := time.NewTicker(e.ttl * time.Second / 5)

无法编译

  • go.md
module example_go_grpc

go 1.14

require (
	github.com/Microsoft/go-winio v0.4.15 // indirect
	github.com/containerd/containerd v1.4.3 // indirect
	github.com/coreos/etcd v3.3.18+incompatible
	github.com/docker/distribution v2.7.1+incompatible // indirect
	github.com/docker/docker v20.10.0-rc1.0.20201130225557-ca520bd1b807+incompatible
	github.com/docker/go-connections v0.4.0 // indirect
	github.com/docker/go-units v0.4.0 // indirect
	github.com/golang/protobuf v1.4.2
	github.com/gorilla/mux v1.8.0 // indirect
	github.com/liyue201/grpc-lb v0.0.0-20201117062843-867c3e20933f
	github.com/moby/term v0.0.0-20201110203204-bea5bbe245bf // indirect
	github.com/morikuni/aec v1.0.0 // indirect
	github.com/opencontainers/go-digest v1.0.0 // indirect
	github.com/opencontainers/image-spec v1.0.1 // indirect
	github.com/sirupsen/logrus v1.4.2
	github.com/small-tk/pathlib v0.0.0-20190601032836-742166d9b695
	github.com/spf13/pflag v1.0.3
	github.com/spf13/viper v1.4.0
	github.com/xeipuuv/gojsonschema v1.2.0
	google.golang.org/grpc v1.31.1
	gotest.tools/v3 v3.0.3 // indirect
)
  • log
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
..\..\..\go\pkg\mod\github.com\coreos\[email protected]+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
..\..\..\go\pkg\mod\github.com\coreos\[email protected]+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
..\..\..\go\pkg\mod\github.com\coreos\[email protected]+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
..\..\..\go\pkg\mod\github.com\coreos\[email protected]+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
PS C:\Users\hsz12\WORKSPACE\service\example-go-grpc>

cannot support etcd api v3

clentv3 use lease replace ttl, keepalive instead of refresh ttl,

where put a value to a exist key,will cause "read-only range request took too long (120.66575ms) to execute" error,

by solve this question, should modify etcd3/registry.go file, use KeepAlive method replace put when key exist

baseSelector Add方法

for _, v := range b.addrs {                                                                                                                                                                                    
     if addr.Addr == v {                                                                                                                                                                                        
         return AddrExistErr                                                                                                                                                                                    
     }                                                                                                                                                                                                          
 }                                                                                                                                                                                                              
      此处判断是否已存在的时候,使用 if _, ok := b.addrMap[addr.Addr]; ok {return AddrExistErr}

还有为什么不用加锁

grpc-lb/registry/etcd3/watcher.go中发现两个bug

  1. 第46~48行在for range中对迭代变量取地址:
for _, v := range addrs {
    v := v    //添加一行fix问题
    updates = append(updates, &naming.Update{Op: naming.Add, Addr: v.Addr, Metadata: &v.Metadata}) //错误处:updates中的所有元素的Metadata都将指向最后的一个元素的Metadata
}
  1. 在69和77行之后,应该加上return语句,否则Next函数将卡在这里

least_connection_x

使用 least_connection_x 得时候 链接一直堵塞 是什么原因呢?

自定义负载均衡策略怎么实现

比如 proto 里有个account字段,我要根据服务个数取模调用对应的服务。
即: 我需要解析请求后根据请求字段确定请求转发的服务
请问这个需求怎么实现 ,在哪实现
🙆🙆🙆🙆🙆

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.