Integrating apollo configuration center on Lua resty apollo openresty

lua-resty-apollo

Github link

Content

The configuration center realizes the centralized management and persistence of configuration. Through the configuration center, it is convenient to manage project configuration. For the background service, the configuration center is to realize gray publishing, configure hot update and optimize the code structure. Solve the shortcomings of traditional project code in the form of code or file in the project. In the configuration center, you can add different accounts and configure different permissions, which can facilitate the modification of project configuration and better management of operations and products.

The general idea of the configuration center is to create a config object, which represents all the configurations of a nameSpace. The config object attribute contains a hashMap. The configuration is obtained through the intuitive way of key value, and the hot update is through polling apollo Service, update hashMap. however Openresty It is a multi process web service. The variables of each worker process do not affect each other, so it is impossible to realize hot update in the traditional way.

This design, using Openresty The shared memory implements a local configuration warehouse. The ConfigService in each worker process checks for updates by polling, while the local configuration warehouse is the polling service of Openresty privilege process to realize updates.

introduce

The data warehouse service of the configuration center adopts apollo. To this end, first introduce the relevant concepts of apollo:

namenote
app_idProject ID
nameSpaceNamespace, which represents a configuration file and configuration collection

You can find out Apollo API development document

Installation

1 take libs Copy directory to Openresty Under the project level directory, use it according to the following instructions.

use

    1. Modify nginx conf:

    http module added:

   lua_shared_dict config_shared 30m; # Shared memory to be configured in the configuration center
   init_worker_by_lua_file 'conf/libs/config/config_init_worker.lua'; # Start related scheduled services
    1. Modify init_by_lua.lua:

    (instantiating ConfigService and Config needs to be completed in the init_by_lua stage. If the apollo service is abnormal, it will automatically announce restart failure to ensure that the service is not affected by apollo)
    (start the Openresty privilege process)

   local process = require "ngx.process"
   local ok, err = process.enable_privileged_agent()
   -- Check whether the startup is successful
   if not ok then
      ngx.log(ngx.ERR, "create privileged process failed")
      error("create privileged process failed")
   end
   -- obtain ConfigService object,Represents one app_id Corresponding configuration, zdao_midservice by app_id,Is a global variable
   MidConfigServive = require "libs.agollo.configService".new("zdao_midservice")
  
   -- adopt ConfigService obtain config object
   LBSConfig = MidConfigServive:GetConfig("zdao_backend.lbs")

    1. apollo related configurations:
      By configuration file:
      /usr/local/openresty/nginx/conf/APOLLO_META_ADDR.txt apollo's metaserver address, for example: 127.0.0.1:8080
    1. use:
   local lbs_config = LBSConfig:GetValue("gaude_config"):Json() -- Representation acquisition nameSpace by zdao_backend.lbs lower gaude_config Value of,And convert to json object

Introduction to relevant methods

    -- obtain ConfigService Object, passed as app_id
    MidConfigServive = require "libs.agollo.configService".new("zdao_midservice")

    -- adopt ConfigService obtain config object,Chuan Shenwei nameSpace
    LBSConfig = MidConfigServive:GetConfig("zdao_backend.lbs")

    -- adopt Config Object acquisition value object,Chuan Shenwei key Value of
    local value = LBSConfig:GetValue("gaude_config")

    -- value correlation method 
    local lbs_config = LBSConfig:GetValue("gaude_config"):Json() -- Translate into table
    local lbs_config = LBSConfig:GetValue("str"):String() -- Translate into string
    local lbs_config = LBSConfig:GetValue("num"):Int() -- Translate into int
    local lbs_config = LBSConfig:GetValue("float"):Float() -- 
    local lbs_config = LBSConfig:GetValue("bool"):Boolean() -- 

Tags: Back-end Nginx lua

Posted by pmaonline on Thu, 14 Apr 2022 21:24:21 +0930