-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add control api for discovery module #3742
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ title: Integration service discovery registry | |
* [**Initial service discovery**](#initial-service-discovery) | ||
* [**Configuration for Eureka**](#configuration-for-eureka) | ||
* [**Upstream setting**](#upstream-setting) | ||
* [**Embedded control api for debugging**](#embedded-control-api-for-debugging) | ||
* [**Discovery modules**](#discovery-modules) | ||
|
||
## Summary | ||
|
@@ -66,7 +67,9 @@ It is very easy for APISIX to extend the discovery client, the basic steps are a | |
|
||
2. Implement the `_M. init_worker()` function for initialization and the `_M. nodes(service_name)` function for obtaining the list of service instance nodes; | ||
|
||
3. Convert the registry data into data in APISIX; | ||
3. If you need the discovery module to export the debugging information online, implement the `_M. dump_data()` function; | ||
yongboy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
4. Convert the registry data into data in APISIX; | ||
|
||
### the example of Eureka | ||
|
||
|
@@ -92,6 +95,11 @@ Then implement the `_M.init_worker()` function for initialization and the `_M.no | |
end | ||
|
||
|
||
function _M.dump_data() | ||
... ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an example return data shoul be written here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for example ? function _M.dump_data()
return {config = your_config, services = your_services, other = ... }
end |
||
end | ||
|
||
|
||
return _M | ||
``` | ||
|
||
|
@@ -256,3 +264,30 @@ $ curl http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f | |
Suppose both A-SERVICE and B-SERVICE provide a `/test` API. The above configuration allows access to A-SERVICE's `/test` API through `/a/test` and B-SERVICE's `/test` API through `/b/test`. | ||
|
||
**Notice**:When configuring `upstream.service_name`, `upstream.nodes` will no longer take effect, but will be replaced by 'nodes' obtained from the registry. | ||
|
||
## Embedded control api for debugging | ||
|
||
Sometimes we need the discovery client to export online data snapshot in memory when running for debugging, and if you implement the `_M. dump_data()` function: | ||
|
||
```lua | ||
function _M.dump_data() | ||
return {config = local_conf.discovery.eureka, services = applications} | ||
end | ||
``` | ||
|
||
Then you can call its control api as below: | ||
|
||
```shell | ||
GET /v1/discovery/{discovery_type}/dump | ||
``` | ||
|
||
eg: | ||
|
||
```shell | ||
curl http://127.0.0.1:9090/v1/discovery/eureka/dump | ||
``` | ||
|
||
## Discovery modules | ||
|
||
- eureka | ||
- [Consul KV](discovery/consul_kv.md) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# | ||
# 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. | ||
# | ||
use t::APISIX 'no_plan'; | ||
|
||
repeat_each(1); | ||
no_long_string(); | ||
no_root_location(); | ||
no_shuffle(); | ||
log_level("info"); | ||
|
||
|
||
our $yaml_config = <<_EOC_; | ||
apisix: | ||
enable_control: true | ||
node_listen: 1984 | ||
config_center: yaml | ||
enable_admin: false | ||
|
||
discovery: | ||
eureka: | ||
host: | ||
- "http://127.0.0.1:8761" | ||
prefix: "/eureka/" | ||
fetch_interval: 10 | ||
weight: 80 | ||
timeout: | ||
connect: 1500 | ||
send: 1500 | ||
read: 1500 | ||
consul_kv: | ||
servers: | ||
- "http://127.0.0.1:8500" | ||
- "http://127.0.0.1:8600" | ||
dns: | ||
servers: | ||
- "127.0.0.1:1053" | ||
_EOC_ | ||
|
||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: test consul_kv dump_data api | ||
--- yaml_config eval: $::yaml_config | ||
--- request | ||
GET /v1/discovery/consul_kv/dump | ||
--- error_code: 200 | ||
--- response_body_unlike | ||
^{}$ | ||
yongboy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
|
||
=== TEST 2: test eureka dump_data api | ||
--- yaml_config eval: $::yaml_config | ||
--- request | ||
GET /v1/discovery/eureka/dump | ||
--- error_code: 200 | ||
--- response_body_unlike | ||
^{}$ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Y |
||
|
||
|
||
|
||
=== TEST 3: test dns api | ||
--- yaml_config eval: $::yaml_config | ||
--- request | ||
GET /v1/discovery/dns/dump | ||
--- error_code: 404 | ||
|
||
|
||
|
||
=== TEST 4: test unconfiged consul_kv dump_data api | ||
--- yaml_config | ||
apisix: | ||
enable_control: true | ||
node_listen: 1984 | ||
config_center: yaml | ||
enable_admin: false | ||
|
||
discovery: | ||
consul_kv: | ||
servers: | ||
- "http://127.0.0.1:8500" | ||
- "http://127.0.0.1:8600" | ||
#END | ||
--- request | ||
GET /v1/discovery/eureka/dump | ||
--- error_code: 404 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we can use
for key, dis_mod in pairs(discovery) do
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if use
for key, dis_mod in pairs(discovery) do
, thedis_mod
isfunction
type, not a table object.So, we can't do it.