apisix 开发公共对外接口

apisix 开发公共对外接口

1 背景

公司网关改造,使用 Apisix 替换原有的 Springcloud Gateway,原来网关上自带了一个接口

逻辑比较简单,配置文件中有一个开关:

  • 值为 true,则返回
{
    "status": 200,
    "message": "分享登录开关打开",
    "data": true,
    "rel": true
}
  • 值为 false,则返回
{
    "status": 200,
    "message": "分享登录开关关闭",
    "data": false,
    "rel": true
}

希望保留原有逻辑,将该接口编写在 Apisix 中(当然,可以随便找个服务,补充这个接口,然后路由到上面,但是这里不想破坏原有逻辑)

2 资料分析

查看官网资料可以看到,Apisix 可以通过 public-api 插件对外暴露接口

但是开启插件必须要与授权插件一起使用,orz___

image-20230917172126607

只能通过自己的方式绕过它了

3 插件开发

这里开发一个 名为 api-test 的插件,用于对外公开一个接口

local ngx = ngx
local core = require("apisix.core")
local consumer_mod = require("apisix.consumer")


local schema = {
  type = "object",
  properties = {
  },
}

local consumer_schema = {
  type = "object",
  properties = {
    key = { type = "string" },
    switch = {
      type = "boolean",
      default = false
    },
  },
  required = {"key"},
}

local plugin_name = "api-test"

local _M = {
  version = 0.1,
  priority = 35,
  type = 'auth',
  name = plugin_name,
  schema = schema,
  consumer_schema = consumer_schema
}


function _M.check_schema(conf)
  if schema_type == core.schema.TYPE_CONSUMER then
    return core.schema.check(consumer_schema, conf)
  else
    return core.schema.check(schema, conf)
  end
end

local function getSwitch()
  local consumer_conf = consumer_mod.plugin(plugin_name)
  if not consumer_conf then
      return core.response.exit(404)
  end
  local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key")
  local consumer = consumers[plugin_name]
  if not consumer then
    return core.response.exit(401, "Consumer Key Should Be Equal To Plugin Name")
  end
  local switch_flag = consumer.auth_conf.switch
  core.log.warn("consumer_conf: ", switch_flag)
  local tbl = { status = 200 , rel = true }
  if switch_flag then
    tbl["message"] = "分享登录开关打开"
    tbl["data"] = true
  else
    tbl["message"] = "分享登录开关关闭"
    tbl["data"] = false
  end
  return core.response.exit(200, core.json.encode(tbl))
end

function _M.api()
  return {
      {
        methods = {"GET"},
        uri = "/switch/getSwitch",
        handler = getSwitch,
      }
  }
end

return _M

核心代码,这里需要保证 consumer 的名称与插件名称相同即可通过校验

local consumer = consumers[plugin_name]

4 部署与测试

添加自定义插件可以参考:apisix 开发自定义插件

  • 添加 消费者
curl http://127.0.0.1:9180/apisix/admin/consumers \
-H 'X-API-KEY: <your-key>' -X PUT -d '
{
    "username": "api-test",
    "plugins": {
        "api-test": {
            "key": "api-test",
            "switch": true
        }
    }
}'
  • 添加路由
curl -X PUT 'http://127.0.0.1:9180/apisix/admin/routes/r1' \
    -H 'X-API-KEY: <your-key>' \
    -H 'Content-Type: application/json' \
    -d '{
    "uri": "/switch/getSwitch",
    "plugins": {
        "public-api": {}
    }
}'
  • 插件重载

如果需要改动 lua 脚本,可以通过以下指令 热加载

curl http://127.0.0.1:9180/apisix/admin/plugins/reload -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT

然后就可以测试接口了

curl 'http://127.0.0.1:9080/switch/getSwitch'

返回结果

{
	"status": 200,
	"message": "分享登录开关打开",
	"rel": true,
	"data": true
}

5 参考资料

注册公共接口

public-api插件

<think>我们正在讨论Apache APISIX作为API网关的开发和应用。根据用户当前的需求,用户想要查找基于APISIX开发API网关的相关资料,特别是方法和教程。 根据提供的引用,我们可以总结出APISIX的核心特性:动态、实时、高性能,支持丰富的流量管理功能(如负载均衡、动态上游、灰度发布、服务熔断、身份认证、可观测性等),并且支持多种服务发现组件(如Nacos, Eureka, Consul)以及可以与Dapr集成。 由于用户需要的是开发方法和教程,我们应当从以下几个方面进行回答: 1. 官方文档:最权威的参考资料,包括安装、配置、插件开发等。 2. 核心概念:路由、服务、上游、插件等。 3. 开发自定义插件:APISIX支持插件扩展,用户可以开发自己的插件。 4. 集成服务发现:如引用[3]中提到的Nacos作为服务发现组件。 5. 与微服务架构集成:如引用[4]中提到的Dapr集成。 因此,回答将围绕这些方面展开,提供指导性的步骤和相关资源的链接(如果有的话,但根据要求不能提供外部链接,所以重点描述方法)。 注意:由于用户要求生成相关问题,我们将在最后提出几个相关问题。 此外,回答的结构要清晰,使用中文,并正确使用数学公式格式(虽然这个主题可能较少涉及数学公式,但如果有公式出现,按规则处理)。 </think>### Apache APISIX开发API网关的方法与教程 根据提供的参考资料,Apache APISIX作为高性能API网关的开发方法主要包括以下核心步骤和技术要点: #### 一、核心开发流程 1. **环境部署** 使用官方Helm Chart在Kubernetes部署APISIX网关及Ingress Controller[^4]: ```bash helm repo add apisix https://charts.apisix.apache.org helm install apisix apisix/apisix ``` 2. **路由配置** 通过Admin API定义路由规则(示例配置): ```json { "uri": "/user/*", "upstream": { "type": "roundrobin", "nodes": { "user-service:8080": 1 } }, "plugins": { "jwt-auth": {} } } ``` 3. **服务发现集成** 支持Nacos、Consul等服务发现组件(引用[3]): ```yaml discovery: nacos: host: "nacos-server" port: 8848 ``` 4. **插件开发** 使用Lua编写自定义插件(目录结构): ``` apisix/plugins/ └── custom-plugin.lua ``` #### 二、关键开发教程 1. **流量管理功能实现** - **灰度发布**:通过`traffic-split`插件分流流量 - **服务熔断**:配置`proxy-rewrite`和`fault-injection`插件 - **认证授权**:集成Keycloak/OAuth2插件 2. **Dapr集成开发**(引用[4]) 在Kubernetes Pod添加注解实现服务网格集成: ```yaml annotations: dapr.io/enabled: "true" dapr.io/app-id: "apisix-proxy" ``` 3. **可观测性配置** 通过Prometheus插件暴露指标: ```bash curl http://127.0.0.1:9091/apisix/admin/plugin_configs/1 -X PUT -d ' { "plugins": { "prometheus": {} } }' ``` #### 三、开发资源推荐 1. **官方文档**:https://apisix.apache.org/zh/docs/apisix/getting-started 2. **GitHub示例**:Apache APISIX示例仓库(含插件开发案例) 3. **开发者社区**:Apache邮件列表 + GitHub Discussions > 开发要点提示:APISIX的核心优势在于其动态加载能力,所有配置变更均无需重启服务[^1][^2],建议优先掌握ETCD配置中心和Admin API的使用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值