ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

API网关-apisix初始化环境

2022-01-14 17:03:23  阅读:214  来源: 互联网

标签:网关 end conf yaml API etcd local apisix


  初始化环境命令make init

  ### init: Initialize the runtime environment

  .PHONY: init

  init: default

  ./bin/apisix init

  ./bin/apisix init_etcd

  可以发现,初始化环境,实际上执行了两个lua命令init和init_etcd

  bin目录下的apisix实际上就是一个lua脚本,脚本中定义了一些cmd命令。

  #!/usr/bin/env lua

  ....

  ....

  _M[cmd_action](arg[2])apisix init

  init命令位于apisix脚本的init函数中:

  大致要做的工作,加载yaml配置文件,解析配置、通过配置文件生成nginx配置文件,检查openresty版本。

  local function init()

  -- read_yaml_conf 读取yaml配置文件

  local yaml_conf, err=read_yaml_conf()

  if not yaml_conf then

  error("failed to read local yaml config of apisix: " .. err)

  end

  -- print("etcd: ", yaml_conf.etcd)

  -- 查看openresty版本

  local or_ver=excute_cmd("openresty -V 2>&1")

  local with_module_status=true

  if or_ver and not or_ver:find("http_stub_status_module", 1, true) then

  io.stderr:write("'http_stub_status_module' module is missing in ",

  "your openresty, please check it out. Without this ",

  "module, there will be fewer monitoring indicators.

  ")

  with_module_status=false

  end

  -- Using templateder

  local sys_conf={

  lua_path=pkg_path_org,

  lua_cpath=pkg_cpath_org,

  os_name=exec("uname"),

  apisix_lua_home=apisix_home,

  with_module_status=with_module_status,

  node_ssl_listen=9443, -- default value

  error_log={level="warn"},

  }

  -- 校验配置

  if not yaml_conf.apisix then

  error("failed to read `apisix` field from yaml file")

  end

  -- 校验配置

  if not yaml_conf.nginx_config then

  error("failed to read `nginx_config` field from yaml file")

  end

  -- 解析配置

  for k,v in pairs(yaml_conf.apisix) do

  sys_conf[k]=v

  end

  for k,v in pairs(yaml_conf.nginx_config) do

  sys_conf[k]=v

  end

  if(sys_conf["enable_dev_mode"]==true) then

  sys_conf["worker_processes"]=1

  else

  sys_conf["worker_processes"]="auto"

  end

  --通过配置文件渲染生成nginx配置

  local conf_render=templatepile(ngx_tpl)

  local ngxconf=conf_render(sys_conf)

  local ok, err=write_file(apisix_home .. "/conf/nginx.conf", ngxconf)

  if not ok then

  error("failed to update nginx.conf: " .. err)

  end

  -- 检查openresty版本

  local op_ver=get_openresty_version()

  if op_ver==nil then

  io.stderr:write("can not find openresty

  ")

  return

  end

  local need_ver="1.15.8"

  if not check_or_version(op_ver, need_ver) then

  io.stderr:write("openresty version must >=", need_ver, " current ", op_ver, "

  ")

  return

  end

  endapisix init_etcd

  大致工作如下,读取yaml配置,check数据源,生成etcd配置,执行etcd接口初始化apisix数据。

  local function init_etcd(show_output)

  -- read_yaml_conf 读取yaml配置文件

  local yaml_conf, err=read_yaml_conf()

  if not yaml_conf then

  error("failed to read local yaml config of apisix: " .. err)

  end

  if not yaml_conf.apisix then

  error("failed to read `apisix` field from yaml file when init etcd")

  end

  -- check 是否用的etcd数据源

  if yaml_conf.apisix.config_center ~="etcd" then

  return true

  end

  if not yaml_conf.etcd then

  error("failed to read `etcd` field from yaml file when init etcd")

  end

  -- 生成etcd配置

  local etcd_conf=yaml_conf.etcd

  local uri=etcd_conf .. "/v2/keys" .. (etcd_conf.prefix or "")

  local timeout=etcd_conf.timeout or 3

  -- 执行相关命令,初始化数据

  for _, dir_name in ipairs({"/routes", "/upstreams", "/services",

  "/plugins", "/consumers", "/node_status",

  "/ssl", "/global_rules", "/stream_routes",

  "/proto"}) do

  local cmd="curl " .. uri .. dir_name

  .. "?prev_exist=false -X PUT -d dir=true "

  .. "--connect-timeout " .. timeout

  .. " --max-time " .. timeout * 2 .. " --retry 1 2>&1"

  local res=exec(cmd)

  if not res:find("index", 1, true)

  and not res:find("createdIndex", 1, true) then

  error(cmd .. "

  " .. res)

  end

  if show_output then

  print(cmd)

  print(res)

  end

  end

  end

标签:网关,end,conf,yaml,API,etcd,local,apisix
来源: https://www.cnblogs.com/linjingyg/p/15802616.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有