| # Licensed to the Apache Software Foundation (ASF) under one |
| # or more contributor license agreements. See the NOTICE file |
| # distributed with this work for additional information |
| # regarding copyright ownership. The ASF licenses this file |
| # to you under the Apache License, Version 2.0 (the |
| # "License"); you may not use this file except in compliance |
| # with the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, |
| # software distributed under the License is distributed on an |
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| # KIND, either express or implied. See the License for the |
| # specific language governing permissions and limitations |
| # under the License. |
| |
| lua_package_path "/usr/local/openresty/lualib/?.lua;;"; |
| lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; |
| |
| lua_shared_dict upstream_list 10m; |
| |
| # Initialize for the first time |
| init_by_lua_block { |
| local eureka = require "eureka"; |
| eureka.init({ |
| upstream_list = ngx.shared.upstream_list, |
| base_url = "http://192.168.9.252:8761", |
| path = "/eureka/apps/demo", |
| |
| }); |
| } |
| |
| # Periodic pull configuration |
| init_worker_by_lua_block { |
| local eureka = require "eureka"; |
| local handle = nil; |
| |
| handle = function () |
| # Only one worker executes the control at a time |
| eureka.get_server_list(); |
| ngx.timer.at(5, handle); |
| end |
| ngx.timer.at(5, handle); |
| } |
| |
| upstream api_server { |
| server 0.0.0.1 down; #Placeholder server |
| |
| balancer_by_lua_block { |
| local balancer = require "ngx.balancer"; |
| local eureka = require "eureka"; |
| local tmp_upstreams = eureka.get_upstreams(); |
| local ip_port = tmp_upstreams[math.random(1, table.getn(tmp_upstreams))]; |
| balancer.set_current_peer(ip_port.ip, ip_port.port); |
| } |
| } |
| |
| server { |
| listen 12000; |
| server_name localhost; |
| charset utf-8; |
| location / { |
| proxy_pass http://api_server; |
| } |
| } |