Skip to content

Commit 5ac6264

Browse files
authored
fix: handle consul nil port cases by defaulting to port 80 (#12304)
1 parent fb3f3b2 commit 5ac6264

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

apisix/discovery/consul/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ function _M.connect(premature, consul_server, retry_delay)
516516
end
517517

518518
local svc_address, svc_port = node.Service.Address, node.Service.Port
519+
-- Handle nil or 0 port case - default to 80 for HTTP services
520+
if not svc_port or svc_port == 0 then
521+
svc_port = 80
522+
end
519523
-- if nodes is nil, new nodes table and set to up_services
520524
if not nodes then
521525
nodes = core.table.new(1, 0)

docs/en/latest/discovery/consul.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ curl -X PUT 'http://127.0.0.1:8500/v1/agent/service/register' \
155155
In some cases, same service name might exist in different consul servers.
156156
To avoid confusion, use the full consul key url path as service name in practice.
157157

158+
### Port Handling
159+
160+
When APISIX retrieves service information from Consul, it handles port values as follows:
161+
162+
- If the service registration includes a valid port number, that port will be used.
163+
- If the port is `nil` (not specified) or `0`, APISIX will default to port `80` for HTTP services.
164+
158165
### Upstream setting
159166

160167
#### L7

t/discovery/consul2.t

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ location /consul3 {
140140
"PUT /consul1/deregister/service_b2",
141141
"PUT /consul1/deregister/service_a3",
142142
"PUT /consul1/deregister/service_a4",
143+
"PUT /consul1/deregister/service_no_port",
143144
"PUT /consul2/deregister/service_a1",
144145
"PUT /consul2/deregister/service_a2",
145146
"PUT /consul3/deregister/service_a1",
@@ -148,13 +149,14 @@ location /consul3 {
148149
"PUT /consul1/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
149150
"PUT /consul1/register\n" . "{\"ID\":\"service_a3\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"localhost\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
150151
"PUT /consul1/register\n" . "{\"ID\":\"service_a4\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"localhost\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
152+
"PUT /consul1/register\n" . "{\"ID\":\"service_no_port\",\"Name\":\"service_no_port\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Meta\":{\"service_version\":\"1.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
151153
"PUT /consul2/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
152154
"PUT /consul2/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
153155
"PUT /consul3/register\n" . "{\"ID\":\"service_a1\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30511,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
154156
"PUT /consul3/register\n" . "{\"ID\":\"service_a2\",\"Name\":\"service_a\",\"Tags\":[\"primary\",\"v1\"],\"Address\":\"127.0.0.1\",\"Port\":30512,\"Meta\":{\"service_a_version\":\"4.0\"},\"EnableTagOverride\":false,\"Weights\":{\"Passing\":10,\"Warning\":1}}",
155157
]
156158
--- error_code eval
157-
[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
159+
[200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200]
158160
159161
160162
@@ -187,7 +189,7 @@ discovery:
187189
--- request
188190
GET /t
189191
--- response_body
190-
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}]}
192+
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
191193
192194
193195
@@ -221,7 +223,7 @@ discovery:
221223
--- request
222224
GET /t
223225
--- response_body
224-
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}]}
226+
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
225227
226228
227229
@@ -255,7 +257,7 @@ discovery:
255257
--- request
256258
GET /t
257259
--- response_body
258-
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30512,"weight":1}]}
260+
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
259261
260262
261263
@@ -289,4 +291,44 @@ discovery:
289291
--- request
290292
GET /t
291293
--- response_body
292-
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}]}
294+
{"service_a":[{"host":"127.0.0.1","port":30511,"weight":1},{"host":"127.0.0.1","port":30512,"weight":1},{"host":"localhost","port":30511,"weight":1},{"host":"localhost","port":30512,"weight":1}],"service_no_port":[{"host":"127.0.0.1","port":80,"weight":1}]}
295+
296+
297+
298+
=== TEST 6: verify service without port defaults to port 80
299+
--- yaml_config
300+
apisix:
301+
node_listen: 1984
302+
enable_control: true
303+
discovery:
304+
consul:
305+
servers:
306+
- "http://127.0.0.1:9500"
307+
dump:
308+
path: "consul.dump"
309+
load_on_init: false
310+
--- config
311+
location /t {
312+
content_by_lua_block {
313+
local json = require("toolkit.json")
314+
local t = require("lib.test_admin")
315+
ngx.sleep(2)
316+
317+
local code, body, res = t.test('/v1/discovery/consul/show_dump_file',
318+
ngx.HTTP_GET)
319+
local entity = json.decode(res)
320+
321+
-- Check that service_no_port exists and has default port 80
322+
local service_no_port = entity.services.service_no_port
323+
if service_no_port and #service_no_port > 0 then
324+
ngx.say("service_no_port found with port: ", service_no_port[1].port)
325+
else
326+
ngx.say("service_no_port not found")
327+
end
328+
}
329+
}
330+
--- timeout: 3
331+
--- request
332+
GET /t
333+
--- response_body
334+
service_no_port found with port: 80

0 commit comments

Comments
 (0)