文件驱动的独立模式
在 文件驱动的独立模式 中,APISIX 从 apisix.yaml 或 apisix.json 获取配置,而不是使用 etcd 作为配置中心。这些配置在启动时加载到内存中,并定期监控更改,无需手动重新加载 APISIX。
important
如果你使用的是 apisix.yaml,请注意,如果文件末尾没有 #END,APISIX 将不会加载配置。
本文档提供了在文件驱动的独立模式下部署的 APISIX 的一些配置示例。要了解有关可用配置选项的更多信息,请参阅 Admin API 参考(但不要使用这些端点)。这些配置选项可以转换为 YAML 或 JSON,以便在文件驱动的独立模式下使用。
配置路由
此示例创建了两个路由,将对 /hello 和 /hello2 的请求转发到不同的上游:
- YAML
- JSON
apisix.yaml
routes:
-
uri: /hello
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
-
uri: /hello2
upstream:
nodes:
"127.0.0.1:1981": 1
type: roundrobin
#END
apisix.json
{
"routes": [
{
"uri": "/hello",
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
},
{
"uri": "/hello2",
"upstream": {
"nodes": {
"127.0.0.1:1981": 1
},
"type": "roundrobin"
}
}
]
}
配置路由和服务
- YAML
- JSON
apisix.yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
apisix.json
{
"routes": [
{
"uri": "/hello",
"service_id": 1
}
],
"services": [
{
"id": 1,
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}
]
}
配置路由和上游
- YAML
- JSON
apisix.yaml
routes:
-
uri: /hello
upstream_id: 1
upstreams:
-
id: 1
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
apisix.json
{
"routes": [
{
"uri": "/hello",
"upstream_id": 1
}
],
"upstreams": [
{
"id": 1,
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
]
}
配置路由、服务和上游
- YAML
- JSON
apisix.yaml
routes:
-
uri: /hello
service_id: 1
services:
-
id: 1
upstream_id: 2
upstreams:
-
id: 2
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
apisix.json
{
"routes": [
{
"uri": "/hello",
"service_id": 1
}
],
"services": [
{
"id": 1,
"upstream_id": 2
}
],
"upstreams": [
{
"id": 2,
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
]
}
配置插件
此示例启用了三个 插件:
- YAML
- JSON
apisix.yaml
plugins:
- name: ip-restriction
- name: jwt-auth
- name: mqtt-proxy
stream: true
#END
apisix.json
{
"plugins": [
{
"name": "ip-restriction"
},
{
"name": "jwt-auth"
},
{
"name": "mqtt-proxy",
"stream": true
}
]
}
对于 L4 插件,将 stream 设置为 true。
请注意,此配置将覆盖默认启用或在 配置文件 中启用的插件列表。
配置插件配置
- YAML
- JSON
apisix.yaml
plugin_configs:
-
id: 1
plugins:
response-rewrite:
body: "hello\n"
routes:
- id: 1
uri: /hello
plugin_config_id: 1
upstream:
nodes:
"127.0.0.1:1980": 1
type: roundrobin
#END
apisix.json
{
"plugin_configs": [
{
"id": 1,
"plugins": {
"response-rewrite": {
"body": "hello\n"
}
}
}
],
"routes": [
{
"id": 1,
"uri": "/hello",
"plugin_config_id": 1,
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}
]
}
配置全局规则
此示例创建了一个 全局规则:
- YAML
- JSON
apisix.yaml
global_rules:
-
id: 1
plugins:
response-rewrite:
body: "hello\n"
#END
apisix.json
{
"global_rules": [
{
"id": 1,
"plugins": {
"response-rewrite": {
"body": "hello\n"
}
}
}
]
}
配置插件元数据
此示例创建 插件元数据:
- YAML
- JSON
apisix.yaml
upstreams:
- id: 1
nodes:
"127.0.0.1:1980": 1
type: roundrobin
routes:
-
uri: /hello
upstream_id: 1
plugins:
http-logger:
batch_max_size: 1
uri: http://127.0.0.1:1980/log
plugin_metadata:
- id: http-logger
log_format:
host: "$host",
remote_addr: "$remote_addr"
#END
apisix.json
{
"upstreams": [
{
"id": 1,
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
],
"routes": [
{
"uri": "/hello",
"upstream_id": 1,
"plugins": {
"http-logger": {
"batch_max_size": 1,
"uri": "http://127.0.0.1:1980/log"
}
}
}
],
"plugin_metadata": [
{
"id": "http-logger",
"log_format": {
"host": "$host",
"remote_addr": "$remote_addr"
}
}
]
}
配置 SSL
此示例配置 SSL:
- YAML
- JSON
apisix.yaml
ssls:
-
cert: ${{SERVER_CERT}}
key: ${{SERVER_KEY}}
snis:
- "yourdomain.com"
#END
apisix.json
{
"ssls": [
{
"cert": "${{SERVER_CERT}}",
"key": "${{SERVER_KEY}}",
"snis": [
"yourdomain.com"
]
}
]
}
要了解有关设置环境变量的更多信息,请参阅 在配置文件中使用环境变量。
配置消费者
此示例创建了一个 消费者:
- YAML
- JSON
apisix.yaml
consumers:
- username: jwt
plugins:
jwt-auth:
key: user-key
secret: my-secret-key
#END
apisix.json
{
"consumers": [
{
"username": "jwt",
"plugins": {
"jwt-auth": {
"key": "user-key",
"secret": "my-secret-key"
}
}
}
]
}
配置四层路由
此示例创建了一个 四层路由:
- YAML
- JSON
apisix.yaml
stream_routes:
- server_addr: 127.0.0.1
server_port: 1985
id: 1
upstream:
nodes:
"127.0.0.1:1981": 1
type: roundrobin
plugins:
mqtt-proxy:
protocol_name: "MQTT"
protocol_level: 4
#END
apisix.json
{
"stream_routes": [
{
"server_addr": "127.0.0.1",
"server_port": 1985,
"id": 1,
"upstream": {
"nodes": {
"127.0.0.1:1981": 1
},
"type": "roundrobin"
},
"plugins": {
"mqtt-proxy": {
"protocol_name": "MQTT",
"protocol_level": 4
}
}
}
]
}
配置 Protos
此示例创建了一个 proto 对象:
- YAML
- JSON
apisix.yaml
protos:
- id: helloworld
desc: hello world
content: >
syntax = "proto3";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
#END
apisix.json
{
"protos": [
{
"id": "helloworld",
"desc": "hello world",
"content": "syntax = \"proto3\";\npackage helloworld;\n\nservice Greeter {\n rpc SayHello (HelloRequest) returns (HelloReply) {}\n}\nmessage HelloRequest {\n string name = 1;\n}\nmessage HelloReply {\n string message = 1;\n}"
}
]
}