|
1 | | -# apisix-seed |
2 | | -Do service discovery on the CP side |
| 1 | +# APISIX-Seed for Apache APISIX |
| 2 | +[](https://goreportcard.com/report/github.com/api7/apisix-seed) |
| 3 | +[](https://github.com/api7/apisix-seed/actions) |
| 4 | +[](https://codecov.io/gh/api7/apisix-seed) |
| 5 | + |
| 6 | +Do service discovery for Apache APISIX on the Control Plane. |
| 7 | + |
| 8 | +# What's APISIX-Seed |
| 9 | +[Apache APISIX](https://github.com/apache/apisix) is a dynamic, real-time, high-performance API gateway. |
| 10 | + |
| 11 | +In terms of architecture design, Apache APISIX is divided into two parts: data plane and control plane. The data plane is Apache APISIX itself, which is the component of the traffic proxy and offers many full-featured plugins covering areas such as authentication, security, traffic control, serverless, analytics & monitoring, transformation and logging. |
| 12 | +The control plane is mainly used to manage routing, and implement the configuration center through etcd. |
| 13 | + |
| 14 | +For cloud-native gateways, it is necessary to dynamically obtain the latest service instance information (service discovery) through the service registry. Currently, Apache APISIX already supports [service discovery](https://github.com/apache/apisix/blob/master/docs/en/latest/discovery.md) in the data plane. |
| 15 | + |
| 16 | +This project is a component of Apache APISIX to implement service discovery in the control plane. It supports cluster deployment. At present, we have supported zookeeper and nacos. We will also support more service registries. |
| 17 | + |
| 18 | +The following figure is the topology diagram of APISIX-Seed deployment. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +# Why APISIX-Seed |
| 23 | +- Network topology becomes simpler |
| 24 | + |
| 25 | +> APISIX does not need to maintain a network connection with each registry, and only needs to pay attention to the configuration information in etcd. This will greatly simplify the network topology. |
| 26 | +
|
| 27 | +- Total data volume about upstream service becomes smaller |
| 28 | +> Due to the characteristics of the registry, APISIX may store the full amount of registry service data in the worker, such as consul_kv. By introducing APISIX-Seed, each process of APISIX will not need to additionally cache upstream service-related information. |
| 29 | +
|
| 30 | +- Easier to manage |
| 31 | +> Service discovery configuration needs to be configured once per APISIX instance. By introducing APISIX-Seed, Apache APISIX will be indifferent to the configuration changes of the service registry. |
| 32 | +
|
| 33 | +# How it works |
| 34 | +We use the go language to implement APISIX-Seed. The flow diagram: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +APISIX-Seed completes data exchange by watching the changes of etcd and service registry at the same time. |
| 39 | + |
| 40 | +The process is as follows: |
| 41 | + |
| 42 | +1、APISIX registers an upstream and specifies the service discovery type to etcd. |
| 43 | + |
| 44 | +2、APISIX-Seed watches the resource changes of APISIX in etcd and filters the discovery type and obtains the service name. |
| 45 | + |
| 46 | +3、APISIX-Seed binds the service to the etcd resource and starts watching the service in the service registry. |
| 47 | + |
| 48 | +4、The client registers the service in the service registry. |
| 49 | + |
| 50 | +5、APISIX-Seed gets the service changes in the service registry. |
| 51 | + |
| 52 | +6 、APISIX-Seed queries the bound etcd resource information through the service name, and writes the updated service node to etcd. |
| 53 | + |
| 54 | +7、The APISIX worker watches etcd changes and refreshes the service node information to the memory. |
| 55 | + |
| 56 | +# Development |
| 57 | + |
| 58 | +The following will take the nacos service registry as an example to show how to deploy and use APISIX-Seed to complete service discovery. Before starting, please make sure that you have installed Apache APISIX correctly. And make sure it can work properly. |
| 59 | + |
| 60 | +## Step 1: Deploy Nacos |
| 61 | + |
| 62 | +Quickly deploy Nacos using the Nacos Docker image: |
| 63 | +```bash |
| 64 | +docker run --name nacos-quick -e MODE=standalone -p 8848:8848 -d nacos/nacos-server:2.0.2 |
| 65 | +``` |
| 66 | + |
| 67 | +## Step 2: Install APISIX-Seed |
| 68 | + |
| 69 | +Download and build APISIX-Seed: |
| 70 | +```bash |
| 71 | +git clone https://github.com/api7/apisix-seed.git |
| 72 | +cd apisix-seed |
| 73 | +make build && make install |
| 74 | +``` |
| 75 | + |
| 76 | +The default configuration file is in `/usr/local/apisix-seed/conf/conf.yaml` with the following contents: |
| 77 | +```yaml |
| 78 | +etcd: # APISIX etcd Configure |
| 79 | + host: |
| 80 | + - "http://127.0.0.1:2379" |
| 81 | + prefix: /apisix |
| 82 | + timeout: 30 |
| 83 | + |
| 84 | +discovery: # service discovery center |
| 85 | + nacos: |
| 86 | + host: # it's possible to define multiple nacos hosts addresses of the same nacos cluster. |
| 87 | + - "http://127.0.0.1:8848" |
| 88 | + prefix: /nacos |
| 89 | + weight: 100 # default weight for node |
| 90 | + timeout: |
| 91 | + connect: 2000 # default 2000ms |
| 92 | + send: 2000 # default 2000ms |
| 93 | + read: 5000 # default 5000ms |
| 94 | +``` |
| 95 | +You can easily understand each configuration item, we will not explain it additionally. |
| 96 | +
|
| 97 | +Start APISIX-Seed: |
| 98 | +```bash |
| 99 | +APISIX_SEED_WORKDIR=/usr/local/apisix-seed /usr/local/apisix-seed/apisix-seed |
| 100 | +``` |
| 101 | + |
| 102 | +## Step 3: Register the upstream service |
| 103 | + |
| 104 | +Start the httpbin service via Docker: |
| 105 | +```bash |
| 106 | +docker run -d -p 8080:80 --rm kennethreitz/httpbin |
| 107 | +``` |
| 108 | + |
| 109 | +Register the service to Nacos: |
| 110 | +```bash |
| 111 | +curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=httpbin&ip=127.0.0.1&port=8080' |
| 112 | +``` |
| 113 | + |
| 114 | +## Step 4: Verify in Apache APISIX |
| 115 | + |
| 116 | +Start Apache APISIX with default configuration: |
| 117 | +```bash |
| 118 | +apisix start |
| 119 | +``` |
| 120 | + |
| 121 | +Create a Route through the Admin API interface of Apache APISIX: |
| 122 | +```bash |
| 123 | +curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d ' |
| 124 | +{ |
| 125 | + "uris": "/*", |
| 126 | + "hosts": [ |
| 127 | + "httpbin" |
| 128 | + ], |
| 129 | + "upstream": { |
| 130 | + "discovery_type": "nacos", |
| 131 | + "service_name": "httpbin", |
| 132 | + "type": "roundrobin" |
| 133 | + } |
| 134 | +}' |
| 135 | +``` |
| 136 | + |
| 137 | +Send a request to confirm whether service discovery is in effect: |
| 138 | +```bash |
| 139 | +curl http://127.0.0.1:9080/get -H 'Host: httpbin' |
| 140 | +``` |
0 commit comments