Giter Club home page Giter Club logo

balancer's Introduction

balancer

Go Report Cardย GitHub top languageย GitHubย CodeFactorย codecovย  go_version

balancer is a layer 7 load balancer that supports http and https, and it is also a go library that implements load balancing algorithms.

It currently supports load balancing algorithms:

  • round-robin
  • random
  • power of 2 random choice
  • consistent hash
  • consistent hash with bounded
  • ip-hash
  • least-load

Install

First download the source code of balancer:

> git clone https://github.com/zehuamama/balancer.git

compile the source code:

> cd ./balancer

> go build

Run

Balancer needs to configure the config.yaml file, see config.yaml :

and now, you can execute balancer, the balancer will print the ascii diagram and configuration details:

> ./balancer

___ _ _  _ _   _ ___  ____ _    ____ _  _ ____ ____ ____ 
 |  | |\ |  \_/  |__] |__| |    |__| |\ | |    |___ |__/ 
 |  | | \|   |   |__] |  | |___ |  | | \| |___ |___ |  \                                        

Schema: http
Port: 8089
Health Check: true
Location:
        Route: /
        Proxy Pass: [http://192.168.1.1 http://192.168.1.2:1015 https://192.168.1.2 http://my-server.com]
        Mode: round-robin

balancer will perform health check on all proxy sites periodically. When the site is unreachable, it will be removed from the balancer automatically . However, balancer will still perform health check on unreachable sites. When the site is reachable, it will add it to the balancer automatically.

API Usage

balancer is also a go library that implements load balancing algorithms, it can be used alone as an API, you need to import it into your project first:

> go get github.com/zehuamama/balancer/balancer

Build the load balancer with balancer.Build:

hosts := []string{
	"http://192.168.11.101",
	"http://192.168.11.102",
	"http://192.168.11.103",
	"http://192.168.11.104",
}

lb, err := balancer.Build(balancer.P2CBalancer, hosts)
if err != nil {
	return err
}

and you can use balancer like this:

clientAddr := "172.160.1.5"  // request IP
	
targetHost, err := lb.Balance(clientAddr) 
if err != nil {
	log.Fatal(err)
}
	
lb.Inc(targetHost)
defer lb.Done(targetHost)

// route to target host

each load balancer implements the balancer.Balancer interface:

type Balancer interface {
	Add(string)
	Remove(string)
	Balance(string) (string, error)
	Inc(string)
	Done(string)
}

currently supports the following load balancing algorithms:

const (
	IPHashBalancer         = "ip-hash"
	ConsistentHashBalancer = "consistent-hash"
	P2CBalancer            = "p2c"
	RandomBalancer         = "random"
	R2Balancer             = "round-robin"
	LeastLoadBalancer      = "least-load"
	BoundedBalancer        = "bounded"
)

Contributing

If you are interested in contributing to balancer, please see here: CONTRIBUTING

License

balancer is licensed under the term of the BSD 2-Clause License

balancer's People

Contributors

zehuamama avatar rgbli avatar tuoeg avatar wanzste avatar hdhgpy avatar

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.