api网关(Apinto)

本文最后更新于 2024年12月12日 早上

记录自己Apinto的API网关部署与使用,目前没啥营养,照着文档部署,所以可以直接去看官方文档。

1 部署网关

1.1 使用docker-compose部署(推荐)

使用docker compose部署apinto和apinto-dashboard,前者是单纯的网关,后者是网关的配置界面。

1.1.1 安装docker环境(如果没有的话)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

如果是国内服务器,则使用阿里

1
2
3
4
# 阿里云密钥
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 阿里云Docker
sudo sh -c 'echo "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list'

根据情况还源vim /etc/docker/daemon.json

1
2
3
4
5
6
7
8
9
{
"registry-mirrors": [
"https://dockerpull.com",
"https://dockerproxy.cn",
"https://hub.uuuadc.top",
"https://docker.1panel.live",
"https://hub.rat.dev"
]
}

重启docker

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

1.1.2 开始部署

1
2
3
mkdir apinto
cd apinto
vim docker-compose.yml

docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
version: '3'

services:
mysql:
image: mysql:5.7.34
privileged: true
restart: always
container_name: apinto_mysql
hostname: apinto_mysql
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=apinto
volumes:
- ./apinto/mysql:/var/lib/mysql
networks:
- apinto

apinto-dashboard:
image: eolinker/apinto-dashboard
container_name: apinto-dashboard
privileged: true
restart: always
networks:
- apinto
ports:
- "18080:8080"
depends_on:
- mysql
- redis_cluster
environment:
- MYSQL_USER_NAME=root
- MYSQL_PWD=123456
- MYSQL_IP=apinto_mysql
- MYSQL_PORT=3306
- MYSQL_DB="apinto"
- ERROR_DIR=/apinto-dashboard/work/logs
- ERROR_FILE_NAME=error.log
- ERROR_LOG_LEVEL=info
- ERROR_EXPIRE=7d
- ERROR_PERIOD=day
- REDIS_ADDR=redis_cluster:7201,redis_cluster:7202,redis_cluster:7203,redis_cluster:7204,redis_cluster:7205,redis_cluster:7206
- REDIS_PWD=123456
volumes:
- ./apinto/apinto-dashboard:/apinto-dashboard/work

redis_cluster:
container_name: redis_cluster
image: eolinker/cluster-redis:7.2.4
hostname: redis_cluster
privileged: true
restart: always
networks:
- apinto
environment:
- REDIS_PWD=123456
- PORT=7201
- HOST=redis_cluster
volumes:
- ./apinto/redis-cluster:/usr/local/cluster_redis/data

apinto_node:
image: eolinker/apinto-gateway:latest
container_name: apinto_node
privileged: true
restart: always
networks:
- apinto
ports:
- "8099:8099"
- "9400:9400"
volumes:
- ./apinto/data-dir:/var/lib/apinto
- ./apinto/log-dir:/var/log/apinto
- ./apinto/config:/etc/apinto

networks:
apinto:
name: apinto_net
driver: bridge
ipam:
driver: default
config:
- subnet: 172.100.0.0/24

启动

1
docker-compose up -d

创建配置文件

1
2
cd apinto/config/
vim config.yml

config.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
version: 2
#certificate: # 证书存放根目录
# dir: /etc/apinto/cert
client:
#advertise_urls: # open api 服务的广播地址
#- http://127.0.0.1:9400
listen_urls: # open api 服务的监听地址
- http://0.0.0.0:9400
#certificate: # 证书配置,允许使用ip的自签证书
# - cert: server.pem
# key: server.key
gateway:
#advertise_urls: # 转发服务的广播地址
#- http://127.0.0.1:9400
listen_urls: # 转发服务的监听地址
- https://0.0.0.0:8099
- http://0.0.0.0:8099
peer: # 集群间节点通信配置信息
listen_urls: # 节点监听地址
- http://0.0.0.0:9401
#advertise_urls: # 节点通信广播地址
# - http://127.0.0.1:9400
#certificate: # 证书配置,允许使用ip的自签证书
# - cert: server.pem
# key: server.key

1.2 使用安装包仅部署apinto

下载安装包

1
2
3
wget https://github.com/eolinker/apinto/releases/download/v0.19.2/apinto_v0.19.2_linux_amd64.tar.gz
tar -zxvf apinto_v0.19.2_linux_amd64.tar.gz
cd apinto

启动

1
./install.sh install

这里会将当前文件夹的apinto.yml.tpl./config.yml.tpl复制到/etc/apinto目录下。

1.3 使用docker compose仅部署Apinto Dashboard

通过docker-compose安装

使用docker-compose进行安装:vim docker-compose.yml,自行修改数据库密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
version: '3'
services:
mysql:
image: mysql:5.7.34
privileged: true
restart: always
container_name: apinto_mysql
hostname: apinto_mysql
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
ports:
- "33306:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=apinto
volumes:
- /var/lib/apinto/mysql:/var/lib/mysql
networks:
- apinto
apinto-dashboard:
image: eolinker/apinto-dashboard
container_name: apinto-dashboard
privileged: true
restart: always
networks:
- apinto
ports:
- "18080:8080"
depends_on:
- mysql
- redis_cluster
environment:
- MYSQL_USER_NAME=root
- MYSQL_PWD=123456
- MYSQL_IP=apinto_mysql
- MYSQL_PORT=3306 #mysql端口
- MYSQL_DB="apinto"
- ERROR_DIR=/apinto-dashboard/work/logs # 日志放置目录
- ERROR_FILE_NAME=error.log # 错误日志文件名
- ERROR_LOG_LEVEL=info # 错误日志等级,可选:panic,fatal,error,warning,info,debug,trace 不填或者非法则为info
- ERROR_EXPIRE=7d # 错误日志过期时间,默认单位为天,d|天,h|小时, 不合法配置默认为7d
- ERROR_PERIOD=day # 错误日志切割周期,仅支持day、hour
- REDIS_ADDR=redis_cluster:7201,redis_cluster:7202,redis_cluster:7203,redis_cluster:7204,redis_cluster:7205,redis_cluster:7206 #Redis集群地址 多个用,隔开
- REDIS_PWD=123456 # Redis密码
volumes:
- /var/log/apinto/apinto-dashboard/work:/apinto-dashboard/work #挂载log到主机目录
redis_cluster:
container_name: redis_cluster
image: eolinker/cluster-redis:7.2.4
hostname: redis_cluster
privileged: true
restart: always
ports:
- 7201-7206:7201-7206
- 17201-17206:17201-17206
networks:
- apinto
environment:
- REDIS_PWD=123456
- PORT=7201
- HOST=redis_cluster
volumes:
- /var/lib/apinto/redis-cluster/data:/usr/local/cluster_redis/data
networks:
apinto:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.100.0.0/24

如果其中数据库只本地访问,则可以删掉其中的端口,示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
version: '3'
services:
mysql:
image: mysql:5.7.34
privileged: true
restart: always
container_name: apinto_mysql
hostname: apinto_mysql
command:
- "--character-set-server=utf8mb4"
- "--collation-server=utf8mb4_unicode_ci"
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=apinto
volumes:
- /var/lib/apinto/mysql:/var/lib/mysql
networks:
- apinto

apinto-dashboard:
image: eolinker/apinto-dashboard
container_name: apinto-dashboard
privileged: true
restart: always
networks:
- apinto
ports:
- "18080:8080"
depends_on:
- mysql
- redis_cluster
environment:
- MYSQL_USER_NAME=root
- MYSQL_PWD=123456
- MYSQL_IP=apinto_mysql
- MYSQL_PORT=3306
- MYSQL_DB="apinto"
- ERROR_DIR=/apinto-dashboard/work/logs
- ERROR_FILE_NAME=error.log
- ERROR_LOG_LEVEL=info
- ERROR_EXPIRE=7d
- ERROR_PERIOD=day
- REDIS_ADDR=redis_cluster:7201,redis_cluster:7202,redis_cluster:7203,redis_cluster:7204,redis_cluster:7205,redis_cluster:7206
- REDIS_PWD=123456
volumes:
- /var/log/apinto/apinto-dashboard/work:/apinto-dashboard/work

redis_cluster:
container_name: redis_cluster
image: eolinker/cluster-redis:7.2.4
hostname: redis_cluster
privileged: true
restart: always
networks:
- apinto
environment:
- REDIS_PWD=123456
- PORT=7201
- HOST=redis_cluster
volumes:
- /var/lib/apinto/redis-cluster/data:/usr/local/cluster_redis/data

networks:
apinto:

driver: bridge
ipam:
driver: default
config:
- subnet: 172.100.0.0/24

2 测试路由

这里以docker compose部署为例,使用的是docker内部网络

绑定apinto

浏览器访问http://ip:18080,默认用户名admin,密码12345678

登录后,点击左侧导航栏的基础设施->集群。然后点击新建集群

在部署服务器上查看网关的ip地址:

1
2
docker network ls
docker network inspect <网络名>

找到"Name": "apinto_node",中的IPv4Address,端口为9400,如下图填入

使用Apinto Dashboard创建路由

使用一言的接口来测试

  • v1.hitokoto.cn

上游服务

点击左侧上游,新建上游服务

在操作处进行发布

创建路由

点击左侧API,新建API->HTTP

在操作处进行发布

测试

浏览器访问http://ip:8099/test,返回结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"id": 6681,
"uuid": "02843879-ab0c-45a4-9a46-8549a5f84a08",
"hitokoto": "总是以为我们会长久,可能因为我们会长久,然后这样我们就长久。",
"type": "i",
"from": "三行情书",
"from_who": "北川理惠",
"creator": "草生出来了",
"creator_uid": 7674,
"reviewer": 1044,
"commit_from": "web",
"created_at": "1604797126",
"length": 30
}

链接


api网关(Apinto)
https://blog.kala.love/posts/aa7cd6b/
作者
久远·卡拉
发布于
2024年12月10日
许可协议