Giter Site home page Giter Site logo

lua-resty-dyups-etcd's Introduction

lua-resty-dyups-etcd

!!!This module is under heavy development, do not use in  production environment.!!!

A lua module for OpenResty, can dynamically update the upstreams from etcd.

DEPENDENCE

  • openresty-1.9.7.3 + (balancer_by_lua*)
  • lua-resty-http
  • cjson

USAGE

Prepare data in etcd:

etcdctl set /v1/testing/services/my_test_service/10.1.1.1:8080 1
etcdctl set /v1/testing/services/my_test_service/10.1.1.2:8080 1
etcdctl set /v1/testing/services/my_test_service/10.1.1.3:8080 1

Value is ignored, we only need the key: IP:PORT, and IP cannot be dns name.

Init the module:

lua_shared_dict dyups 10k; # for global lock and version

init_worker_by_lua_block {
    local u = require "dyups"
    u.init({
        etcd_host = "127.0.0.1",
        etcd_port = 2379,
        etcd_path = "/v1/testing/services/",
        dump_file = "/tmp/nginx-upstreams",
        dict = ngx.shared.dyups
    })
}

Get a server in upstream:

upstream test {
    server 127.0.0.1:2222; # fake server

    balancer_by_lua_block {
        local balancer = require "ngx.balancer"
        local u = require "dyups"
        local s, err = u.round_robin_server("my_test_service")
        if not s then
            ngx.log(ngx.ERR, err)
            return ngx.exit(500)
        end
        local ok, err = balancer.set_current_peer(s.host, s.port)
        if not ok then
            ngx.log(ngx.ERR, "failed to set current peer: " .. err)
            return ngx.exit(500)
        end
    }
}

Functions

dyups.round_robin_server(service_name)

Get a backend server from the server list in a upstream, and using round-robin algorithm.
return a table: 
{
  host = "127.0.0.1",
   port = 1234
}

dyups.all_servers(service_name)

Get all backend servers in a upstream.
return a table:
{
  {host= "127.0.0.1", port = 1234},
  {host= "127.0.0.2", port = 1234},
  {host= "127.0.0.3", port = 1234}
}

So you can realize your own balance algorithms.

License

I have not think about it yet.

lua-resty-dyups-etcd's People

Watchers

James Cloos avatar  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.