最近一直在研究微服務相關內容,透過對比各大API閘道器,發現新起之秀 APISIX無論從開源程度上來講還是功能上,都擁有很大的優勢。
經歷了幾天折磨一樣的學習,目前在本地環境中配置成功了一套,以供自己留存吧,實在是網上的很多文章要麼太老了,要麼就是亂寫一通。
APISIX官方網址:https://apisix.apache.org/
ETCD官方網址:https://etcd.io/
1、安裝ETCD(分散式Key-Value儲存系統)
根據apisix提供的官方網檔,執行以下指令碼就可以了:
wget https://github.com/etcd-io/etcd/releases/download/v3.5.8/etcd-v3.5.8-linux-amd64.tar.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/
這裡的安裝時間因為國內的原因,可能需要執行很長時間,我們同樣也可以將檔案提前下載好,丟到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)以服務方式啟用及啟用載入自啟
systemctl start etcd systemctl enable etcd
到這裡,整個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
以上所有便是APISIX在centos 7.6的安裝過程,如果安裝中大家有什麼問題,可以一起留言討論一下