最近一直在研究微服務(wù)相關(guān)內(nèi)容,通過對比各大API網(wǎng)關(guān),發(fā)現(xiàn)新起之秀 APISIX無論從開源程度上來講還是功能上,都擁有很大的優(yōu)勢。
經(jīng)歷了幾天折磨一樣的學(xué)習(xí),目前在本地環(huán)境中配置成功了一套,以供自己留存吧,實在是網(wǎng)上的很多文章要么太老了,要么就是亂寫一通。
?
APISIX官方網(wǎng)址:https://apisix.apache.org/
ETCD官方網(wǎng)址:https://etcd.io/
?
1、安裝ETCD(分布式Key-Value存儲系統(tǒng))
根據(jù)apisix提供的官方網(wǎng)檔,執(zhí)行以下腳本就可以了:
wget http://www.ythuaji.com.cn/uploads/allimg/azq3yvdjcxk.gz tar -xvf etcd-v3.5.8-linux-amd64.tar.gz && \ cd etcd-v3.5.8-linux-amd64 && \ sudo cp -a etcd etcdctl /usr/bin/
這里的安裝時間因為國內(nèi)的原因,可能需要執(zhí)行很長時間,我們同樣也可以將文件提前下載好,丟到CentOS服務(wù)器上,再執(zhí)行解壓縮就可以了。
2、配置ETCD
不知道是我找的資料不對,還是官方精簡了一些,本打算使用 systemctl 安裝ETCD,但是各種提示報錯,經(jīng)過了大量的資料搜索和文章的研究與嘗試,這里需要以下步驟進(jìn)行安裝配置:
(1)創(chuàng)建 /etc/etcd/etcd.service 服務(wù)配置文件,并編輯內(nèi)容如下(本實例為單機模式部署):
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)以服務(wù)方式啟用及啟用加載自啟
systemctl start etcd systemctl enable etcd
到這里,整個ETCD服務(wù)就安裝完成了
3、安裝APISIX
這一步操作很簡單,直接根據(jù)官方文檔來操作就可以了,安裝說明: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)執(zhí)行APISIX安裝
# 默認(rèn)安裝 sudo yum install apisix # 指定版本安裝 sudo yum install apisix-3.3.0
安裝完APISIX我們先不要啟動,先去 /usr/local/apisix/conf/config.yaml中修改對應(yīng)的文件配置,我這例子的配置如下:
# # 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服務(wù)器安裝的位置(推薦使用內(nèi)網(wǎng),不要對外開放端口哈)
這里配置完成后,我們就可以使用systemctl啟動APISIX咯
# 使用systemctl 將APISIX安裝為服務(wù)
systemctl start apisix
# 添加服務(wù)開機自啟動
systemctl enable apsix
啟動成功后,通過訪問網(wǎng)址 http://127.0.0.1:8000 會提示404 Route Not Found的字樣,這時,我們的APISIX服務(wù)就安裝完成咯!
4、安裝APISIX-DASHBOARD(管理控制面板)
同樣的,我們根據(jù)官方給的文檔進(jìn)行安裝,文檔地址:https://apisix.apache.org/docs/dashboard/install/
因為我們使用的CENTOS直接安裝,那么我們執(zhí)行如下的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 文件中修改對應(yīng)的配置ETCD地址及管理員、用戶的賬號及密碼!
以上操作完成后,同樣的執(zhí)行以下命令,apisix-dashboard也就啟動完成,我這里默認(rèn)開的是9000端口,那么完成后通過瀏覽器訪問 http://127.0.0.1:9000 就可以使用咯
# 使用systemctl 將APISIX-DASHBOARD安裝為服務(wù) systemctl start apisix-dashboard # 添加服務(wù)開機自啟動 systemctl enable apsix-dashboard
?
?
以上所有便是APISIX在centos 7.6的安裝過程,如果安裝中大家有什么問題,可以一起留言討論一下
?