最近一直在研究微服务相关内容,通过对比各大API网关,发现新起之秀 APISIX无论从开源程度上来讲还是功能上,都拥有很大的优势。
经历了几天折磨一样的学习,目前在本地环境中配置成功了一套,以供自己留存吧,实在是网上的很多文章要么太老了,要么就是乱写一通。
APISIX官方网址:https://apisix.apache.org/
ETCD官方网址:https://etcd.io/
1、安装ETCD(分布式Key-Value存储系统)
根据apisix提供的官方网档,执行以下脚本就可以了:
1 2 3 4 |
|
这里的安装时间因为国内的原因,可能需要执行很长时间,我们同样也可以将文件提前下载好,丢到CentOS服务器上,再执行解压缩就可以了。
2、配置ETCD
不知道是我找的资料不对,还是官方精简了一些,本打算使用 systemctl 安装ETCD,但是各种提示报错,经过了大量的资料搜索和文章的研究与尝试,这里需要以下步骤进行安装配置:
(1)创建 /etc/etcd/etcd.conf 服务配置文件,并编辑内容如下(本实例为单机模式部署):
ETCD_NAME=etcd ETCD_DATA_DIR=/etc/etcd/data ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 ETCD_ADVERTISE_CLIENT_URLS=http://0.0.0.0:2379 ETCD_INITIAL_ADVERTISE_PEER_URLS=http://0.0.0.0:2380 ETCD_INITIAL_CLUSTER_STATE=new ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
编写完成后,保存即可。
(2)编写systemctl服务文件(/usr/lib/systemd/system/etcd.service
[Unit] Description=etcd key-value store Documentation=https://github.com/etcd-io/etcd After=network.target [Service] EnvironmentFile=/etc/etcd/etcd.conf ExecStart=/usr/bin/etcd Restart=always [Install] WantedBy=multi-user.target
(3)以服务方式启用及启用加载自启
1 2 |
|
到这里,整个ETCD服务就安装完成了
3、安装APISIX
这一步操作很简单,直接根据官方文档来操作就可以了,安装说明:https://apisix.apache.org/docs/apisix/installation-guide/
(1)安装OpenRestry并且安装APISIX包
sudo yum install -y https://repos.apiseven.com/packages/centos/apache-apisix-repo-1.0-1.noarch.rpm
(2)添加APISIX YUM包源
sudo yum-config-manager --add-repo https://repos.apiseven.com/packages/centos/apache-apisix.repo
(3)执行APISIX安装
# 默认安装 sudo yum install apisix # 指定版本安装 sudo yum install apisix-3.3.0
安装完APISIX我们先不要启动,先去 /usr/local/apisix/conf/config.yaml中修改对应的文件配置,我这例子的配置如下:
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # If you want to set the specified configuration value, you can set the new # in this file. For example if you want to specify the etcd address: # # deployment: # role: traditional # role_traditional: # config_provider: etcd # etcd: # host: # - http://127.0.0.1:2379 # # To configure via environment variables, you can use `${{VAR}}` syntax. For instance: # # deployment: # role: traditional # role_traditional: # config_provider: etcd # etcd: # host: # - http://${{ETCD_HOST}}:2379 # # And then run `export ETCD_HOST=$your_host` before `make init`. # # If the configured environment variable can't be found, an error will be thrown. # # Also, If you want to use default value when the environment variable not set, # Use `${{VAR:=default_value}}` instead. For instance: # # deployment: # role: traditional # role_traditional: # config_provider: etcd # etcd: # host: # - http://${{ETCD_HOST:=localhost}}:2379 # # This will find environment variable `ETCD_HOST` first, and if it's not exist it will use `localhost` as default value. # apisix: node_listen: 8000 deployment: role: traditional role_traditional: config_provider: etcd etcd: host: - http://127.0.0.1:2379 admin: admin_key: - name: admin key: edd1c9f034335f136f87ad84b625c8f1 # using fixed API token has security risk, please update it when you deploy to production environment role: admin
这里要注意的是,apisix: node_listen 是不存在的,你要自己添加上并指定一下需要绑定的端口,并且在etcd 下的host 指定你ETCD服务器安装的位置(推荐使用内网,不要对外开放端口哈)
这里配置完成后,我们就可以使用systemctl启动APISIX咯
# 使用systemctl 将APISIX安装为服务 systemctl start apisix # 添加服务开机自启动 systemctl enable apsix
启动成功后,通过访问网址 http://127.0.0.1:8000 会提示404 Route Not Found的字样,这时,我们的APISIX服务就安装完成咯!
4、安装APISIX-DASHBOARD(管理控制面板)
同样的,我们根据官方给的文档进行安装,文档地址:https://apisix.apache.org/docs/dashboard/install/
因为我们使用的CENTOS直接安装,那么我们执行如下的BASH脚本即可:
sudo yum install -y https://github.com/apache/apisix-dashboard/releases/download/v3.0.1/apisix-dashboard-3.0.1-0.el7.x86_64.rpm
安装需要一定的时间,安装完成后记得去 /usr/local/apisix/dashboard/conf/conf.yaml 文件中修改对应的配置ETCD地址及管理员、用户的账号及密码!
以上操作完成后,同样的执行以下命令,apisix-dashboard也就启动完成,我这里默认开的是9000端口,那么完成后通过浏览器访问 http://127.0.0.1:9000 就可以使用咯
# 使用systemctl 将APISIX-DASHBOARD安装为服务 systemctl start apisix-dashboard # 添加服务开机自启动 systemctl enable apsix-dashboard
欢迎大家加入编程交流群
Qt编程交流群 57634222
C++编程交流群 910854771
编程交流群 241531722