Code Monkey home page Code Monkey logo

lua-eureka-balancer's Introduction

lua-eureka-balancer

功能描述

用lua实现从eureka中发现服务,并自动加入nginx负载均衡。

nginx.conf

...

http {

# 配置共享缓存区
lua_shared_dict eureka_balancer 128m;

init_worker_by_lua_block {
    -- init eureka balancer
    local eureka_balancer = require "resty.xtc.eureka_balancer"
    local i_eureka_balancer = eureka_balancer:new({dict_name="eureka_balancer"})
    --配置eureka服务器地址
    i_eureka_balancer.set_eureka_service_url({"127.0.0.1:1111","127.0.0.1:1112"})
    --配置监听的服务名称
    i_eureka_balancer.watch_service({"demo-service","demo-service2"})
}

upstream up_demo {
    server 127.0.0.1:666; # Required, because empty upstream block is rejected by nginx (nginx+ can use 'zone' instead)
    balancer_by_lua_block {         
        --定义后端服务的服务名称
        local service_name = "demo-service"

        --服务发现+负载均衡
        local eureka_balancer = require "resty.xtc.eureka_balancer"
        local i_eureka_balancer = eureka_balancer:new({dict_name="eureka_balancer"}) 

        --i_eureka_balancer.ip_hash(service_name) --IP Hash负载算法
        i_eureka_balancer.round_robin(service_name) --轮询负载算法
    }
}

server {
    listen 80;
    
    location /test {
        proxy_pass  http://up_demo/;
        proxy_set_header Host $http_host";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
    }
}

}

lua-eureka-balancer's People

Contributors

lichao0 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.