Skip to content

Instantly share code, notes, and snippets.

@hzbd
Forked from moonming/Apache_APISIX_Consul
Created October 29, 2020 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hzbd/e36245256dfaa96da96f3ae1d83ef790 to your computer and use it in GitHub Desktop.
Save hzbd/e36245256dfaa96da96f3ae1d83ef790 to your computer and use it in GitHub Desktop.
How to use Consul as Registration Center in Apache APISIX?
## Install Consul
1. download consul
```
wget https://releases.hashicorp.com/consul/1.7.3/consul_1.7.3_linux_amd64.zip
```
2. unzip to `/usr/bin`
```
sudo unzip consul_1.7.3_linux_amd64.zip -d /usr/bin
```
3. create consul service file
```
sudo vim /lib/systemd/system/consul.service
[Unit]
Description=consul
[Service]
ExecStart=/usr/bin/consul agent -config-dir /etc/consul
KillSignal=SIGINT
```
4. create server json file
```
sudo mkdir /etc/consul/
sudo vim /etc/consul/server.json
{
"data_dir": "/var/consul",
"log_level": "INFO",
"node_name": "test",
"server": true,
"ui": true,
"bootstrap_expect": 1,
"client_addr": "0.0.0.0",
"advertise_addr": "127.0.0.1",
"ports": {
"dns": 53
},
"advertise_addr_wan": "127.0.0.1"
}
```
5. start consul
```
sudo systemctl start consul
```
## start service - golang version
```
git clone https://github.com/api7/consul-test-golang.git
cd consul-test-golang
nohup go run main.go &
```
## install etcd -- need by Apache APISIX
```
sudo yum install etcd
nohup /usr/bin/etcd --enable-v2=true &
```
## install openresty -- need by Apache APISIX
```
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
sudo yum install openresty -y
```
## install Apache APISIX
1. install from RPM, and you can get the latest version from https://github.com/apache/incubator-apisix/releases
```
wget https://github.com/apache/incubator-apisix/releases/download/1.3/apisix-1.3-0.el7.noarch.rpm
sudo yum install apisix-1.3-0.el7.noarch.rpm -y
```
2. change config.yaml
vi /usr/local/apisix/conf/config.yaml
add consul address to `dns_resolver`:
```
dns_resolver:
- 127.0.0.1
```
3. start Apache APISIX:
```
sudo apisix start
```
## Test
1. add route in Apache APISIX:
```
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
{
"uri": "/healthz",
"upstream": {
"type": "roundrobin",
"nodes": {
"go-consul-test.service.consul:8080": 1
}
}
}'
```
**go-consul-test.service.consul is registered DNS SRV by consul-test-golang service**
2. test:
```
curl http://127.0.0.1:9080/healthz
```
will return:
```
{"message":"consul test healthz"}
```
Cool, it works!
If you have any questions, please goto https://github.com/apache/incubator-apisix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment