Skip to content

Commit fab68d7

Browse files
authored
feat: stream subsystem support consul_kv service discovery (#8633)
Fixes #7779
1 parent db2077a commit fab68d7

File tree

2 files changed

+290
-0
lines changed

2 files changed

+290
-0
lines changed

docs/en/latest/discovery/consul_kv.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ To avoid confusion, use the full consul key url path as service name in practice
131131

132132
### Upstream setting
133133

134+
#### L7
135+
134136
Here is an example of routing a request with a URL of "/*" to a service which named "http://127.0.0.1:8500/v1/kv/upstreams/webpages/" and use consul_kv discovery client in the registry :
135137

136138
```shell
@@ -172,6 +174,25 @@ The format response as below:
172174

173175
You could find more usage in the `apisix/t/discovery/consul_kv.t` file.
174176

177+
#### L4
178+
179+
Consul_kv service discovery also supports use in L4, the configuration method is similar to L7.
180+
181+
```shell
182+
$ curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
183+
{
184+
"remote_addr": "127.0.0.1",
185+
"upstream": {
186+
"scheme": "tcp",
187+
"service_name": "http://127.0.0.1:8500/v1/kv/upstreams/webpages/",
188+
"type": "roundrobin",
189+
"discovery_type": "consul_kv"
190+
}
191+
}'
192+
```
193+
194+
You could find more usage in the `apisix/t/discovery/stream/consul_kv.t` file.
195+
175196
## Debugging API
176197

177198
It also offers control api for debugging.

t/discovery/stream/consul_kv.t

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
use t::APISIX 'no_plan';
18+
19+
repeat_each(1);
20+
log_level('info');
21+
no_root_location();
22+
no_shuffle();
23+
24+
25+
add_block_preprocessor(sub {
26+
my ($block) = @_;
27+
28+
my $http_config = $block->http_config // <<_EOC_;
29+
30+
server {
31+
listen 20999;
32+
33+
location / {
34+
content_by_lua_block {
35+
ngx.say("missing consul_kv services")
36+
}
37+
}
38+
}
39+
40+
server {
41+
listen 30511;
42+
43+
location /hello {
44+
content_by_lua_block {
45+
ngx.say("server 1")
46+
}
47+
}
48+
}
49+
server {
50+
listen 30512;
51+
52+
location /hello {
53+
content_by_lua_block {
54+
ngx.say("server 2")
55+
}
56+
}
57+
}
58+
server {
59+
listen 30513;
60+
61+
location /hello {
62+
content_by_lua_block {
63+
ngx.say("server 3")
64+
}
65+
}
66+
}
67+
server {
68+
listen 30514;
69+
70+
location /hello {
71+
content_by_lua_block {
72+
ngx.say("server 4")
73+
}
74+
}
75+
}
76+
_EOC_
77+
78+
$block->set_value("http_config", $http_config);
79+
80+
if (!$block->stream_request) {
81+
$block->set_value("stream_request", "GET /hello HTTP/1.1\r\nHost: 127.0.0.1:1985\r\nConnection: close\r\n\r\n");
82+
}
83+
});
84+
85+
our $yaml_config = <<_EOC_;
86+
apisix:
87+
node_listen: 1984
88+
deployment:
89+
role: data_plane
90+
role_data_plane:
91+
config_provider: yaml
92+
discovery:
93+
consul_kv:
94+
servers:
95+
- "http://127.0.0.1:8500"
96+
- "http://127.0.0.1:8600"
97+
prefix: "upstreams"
98+
skip_keys:
99+
- "upstreams/unused_api/"
100+
timeout:
101+
connect: 1000
102+
read: 1000
103+
wait: 60
104+
weight: 1
105+
fetch_interval: 1
106+
keepalive: true
107+
default_service:
108+
host: "127.0.0.1"
109+
port: 20999
110+
metadata:
111+
fail_timeout: 1
112+
weight: 1
113+
max_fails: 1
114+
_EOC_
115+
116+
117+
run_tests();
118+
119+
__DATA__
120+
121+
=== TEST 1: prepare consul kv register nodes
122+
--- config
123+
location /consul1 {
124+
rewrite ^/consul1/(.*) /v1/kv/$1 break;
125+
proxy_pass http://127.0.0.1:8500;
126+
}
127+
128+
location /consul2 {
129+
rewrite ^/consul2/(.*) /v1/kv/$1 break;
130+
proxy_pass http://127.0.0.1:8600;
131+
}
132+
--- pipelined_requests eval
133+
[
134+
"DELETE /consul1/upstreams/webpages/?recurse=true",
135+
"DELETE /consul2/upstreams/webpages/?recurse=true",
136+
"PUT /consul1/upstreams/webpages/127.0.0.1:30511\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
137+
"PUT /consul1/upstreams/webpages/127.0.0.1:30512\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
138+
"PUT /consul2/upstreams/webpages/127.0.0.1:30513\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
139+
"PUT /consul2/upstreams/webpages/127.0.0.1:30514\n" . "{\"weight\": 1, \"max_fails\": 2, \"fail_timeout\": 1}",
140+
]
141+
--- response_body eval
142+
["true", "true", "true", "true", "true", "true"]
143+
144+
145+
146+
=== TEST 2: test consul server 1
147+
--- yaml_config eval: $::yaml_config
148+
--- apisix_yaml
149+
stream_routes:
150+
- server_addr: 127.0.0.1
151+
server_port: 1985
152+
id: 1
153+
upstream:
154+
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
155+
discovery_type: consul_kv
156+
type: roundrobin
157+
#END
158+
--- stream_response eval
159+
qr/server [1-2]/
160+
--- no_error_log
161+
[error]
162+
163+
164+
165+
=== TEST 3: test consul server 2
166+
--- yaml_config eval: $::yaml_config
167+
--- apisix_yaml
168+
stream_routes:
169+
- server_addr: 127.0.0.1
170+
server_port: 1985
171+
id: 1
172+
upstream:
173+
service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/
174+
discovery_type: consul_kv
175+
type: roundrobin
176+
#END
177+
--- stream_response eval
178+
qr/server [3-4]/
179+
--- no_error_log
180+
[error]
181+
182+
183+
184+
=== TEST 4: test mini consul_kv config
185+
--- yaml_config
186+
apisix:
187+
node_listen: 1984
188+
deployment:
189+
role: data_plane
190+
role_data_plane:
191+
config_provider: yaml
192+
discovery:
193+
consul_kv:
194+
servers:
195+
- "http://127.0.0.1:8500"
196+
- "http://127.0.0.1:6500"
197+
#END
198+
--- apisix_yaml
199+
stream_routes:
200+
- server_addr: 127.0.0.1
201+
server_port: 1985
202+
id: 1
203+
upstream:
204+
service_name: http://127.0.0.1:8500/v1/kv/upstreams/webpages/
205+
discovery_type: consul_kv
206+
type: roundrobin
207+
#END
208+
--- stream_response eval
209+
qr/server [1-2]/
210+
--- ignore_error_log
211+
212+
213+
214+
=== TEST 5: test invalid service name
215+
sometimes the consul key maybe deleted by mistake
216+
--- yaml_config eval: $::yaml_config
217+
--- apisix_yaml
218+
stream_routes:
219+
- server_addr: 127.0.0.1
220+
server_port: 1985
221+
id: 1
222+
upstream:
223+
service_name: http://127.0.0.1:8600/v1/kv/upstreams/deleted_keys/
224+
discovery_type: consul_kv
225+
type: roundrobin
226+
#END
227+
--- stream_response_like
228+
missing consul_kv services
229+
--- ignore_error_log
230+
231+
232+
233+
=== TEST 6: test skip keys
234+
skip some keys, return default nodes, get response: missing consul_kv services
235+
--- yaml_config
236+
apisix:
237+
node_listen: 1984
238+
deployment:
239+
role: data_plane
240+
role_data_plane:
241+
config_provider: yaml
242+
discovery:
243+
consul_kv:
244+
servers:
245+
- "http://127.0.0.1:8600"
246+
prefix: "upstreams"
247+
skip_keys:
248+
- "upstreams/webpages/"
249+
default_service:
250+
host: "127.0.0.1"
251+
port: 20999
252+
metadata:
253+
fail_timeout: 1
254+
weight: 1
255+
max_fails: 1
256+
#END
257+
--- apisix_yaml
258+
stream_routes:
259+
- server_addr: 127.0.0.1
260+
server_port: 1985
261+
id: 1
262+
upstream:
263+
service_name: http://127.0.0.1:8600/v1/kv/upstreams/webpages/
264+
discovery_type: consul_kv
265+
type: roundrobin
266+
#END
267+
--- stream_response_like
268+
missing consul_kv services
269+
--- ignore_error_log

0 commit comments

Comments
 (0)