Skip to content

Commit 8d7f65d

Browse files
authored
fix: Update portal interface adaptation; add retry logic; removed domain name usage; add gateway ip. (#138)
1 parent bb87bfb commit 8d7f65d

9 files changed

Lines changed: 415 additions & 229 deletions

File tree

deploy/docker/scripts/data/higress-mcp.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"description": "出行小助手",
55
"type": "OPEN_API",
66
"higress": {
7-
"domains": ["travel.assistant.io"],
8-
"enableHttps": false,
97
"serviceSources": [
108
{
119
"type": "dns",
@@ -45,8 +43,6 @@
4543
"description": "基金诊断助手",
4644
"type": "DIRECT_ROUTE",
4745
"higress": {
48-
"domains": ["fund.assistant.io"],
49-
"enableHttps": false,
5046
"serviceSources": [
5147
{
5248
"type": "dns",

deploy/docker/scripts/hooks/post_ready.d/30-init-higress-mcp.sh

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,6 @@ EOF
189189
call_higress_api "POST" "/v1/service-sources" "$data" "[${mcp_name}] 创建服务源 (${name})"
190190
}
191191

192-
########################################
193-
# 创建域名
194-
########################################
195-
create_domain() {
196-
local domain="$1"
197-
local enable_https="$2"
198-
local mcp_name="$3"
199-
200-
local https_value="off"
201-
if [[ "$enable_https" == "true" ]]; then
202-
https_value="on"
203-
fi
204-
205-
local data=$(cat <<EOF
206-
{
207-
"name": "${domain}",
208-
"enableHttps": "${https_value}"
209-
}
210-
EOF
211-
)
212-
213-
call_higress_api "POST" "/v1/domains" "$data" "[${mcp_name}] 创建域名 (${domain})"
214-
}
215-
216192
########################################
217193
# 处理 OpenAPI 类型的 MCP
218194
########################################
@@ -247,7 +223,6 @@ process_openapi_mcp() {
247223
fi
248224

249225
# 构建请求数据
250-
local domains=$(echo "$mcp_config" | jq -c '.higress.domains')
251226
local services=$(echo "$mcp_config" | jq -c '.higress.services')
252227
local consumer_auth=$(echo "$mcp_config" | jq -c '.higress.consumerAuth')
253228

@@ -256,7 +231,7 @@ process_openapi_mcp() {
256231
"id": null,
257232
"name": "${mcp_name}",
258233
"description": "${description}",
259-
"domains": ${domains},
234+
"domains": [],
260235
"services": ${services},
261236
"type": "OPEN_API",
262237
"consumerAuthInfo": ${consumer_auth},
@@ -285,7 +260,6 @@ process_direct_route_mcp() {
285260
log "处理 DIRECT_ROUTE MCP: ${mcp_name}"
286261

287262
# 构建请求数据
288-
local domains=$(echo "$mcp_config" | jq -c '.higress.domains')
289263
local services=$(echo "$mcp_config" | jq -c '.higress.services')
290264
local consumer_auth=$(echo "$mcp_config" | jq -c '.higress.consumerAuth')
291265

@@ -294,7 +268,7 @@ process_direct_route_mcp() {
294268
"id": null,
295269
"name": "${mcp_name}",
296270
"description": "${description}",
297-
"domains": ${domains},
271+
"domains": [],
298272
"services": ${services},
299273
"type": "DIRECT_ROUTE",
300274
"consumerAuthInfo": ${consumer_auth},
@@ -334,18 +308,7 @@ process_single_mcp() {
334308
return 1
335309
fi
336310
done <<< "$sources"
337-
338-
# 2. 创建所有域名
339-
local enable_https=$(echo "$mcp_config" | jq -r '.higress.enableHttps')
340-
local domains=$(echo "$mcp_config" | jq -r '.higress.domains[]')
341-
while IFS= read -r domain; do
342-
if ! create_domain "$domain" "$enable_https" "$mcp_name"; then
343-
err "[${mcp_name}] 创建域名失败"
344-
return 1
345-
fi
346-
done <<< "$domains"
347-
348-
# 3. 根据类型创建 MCP 服务器配置
311+
# 2. 根据类型创建 MCP 服务器配置
349312
if [[ "$mcp_type" == "OPEN_API" ]]; then
350313
if ! process_openapi_mcp "$mcp_config"; then
351314
err "[${mcp_name}] 创建 OpenAPI MCP 失败"

deploy/docker/scripts/hooks/post_ready.d/40-init-himarket-mcp.sh

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,35 @@ login_admin() {
175175
return 1
176176
}
177177

178+
########################################
179+
# 获取 Higress Gateway 公网 IP 地址
180+
########################################
181+
get_higress_gateway_address() {
182+
log "获取 Higress Gateway 公网 IP 地址..." >&2
183+
184+
local gateway_ip=""
185+
186+
# 尝试多个公网 IP 检测服务
187+
for service in "ifconfig.me" "icanhazip.com" "ipecho.net/plain" "api.ipify.org"; do
188+
gateway_ip=$(curl -s --connect-timeout 3 --max-time 5 "http://${service}" 2>/dev/null | tr -d '[:space:]')
189+
190+
# 验证是否为有效的 IPv4 地址(排除内网地址)
191+
if [[ "$gateway_ip" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
192+
if [[ ! "$gateway_ip" =~ ^10\. ]] && \
193+
[[ ! "$gateway_ip" =~ ^172\.(1[6-9]|2[0-9]|3[0-1])\. ]] && \
194+
[[ ! "$gateway_ip" =~ ^192\.168\. ]] && \
195+
[[ ! "$gateway_ip" =~ ^127\. ]]; then
196+
log "检测到公网 IP: ${gateway_ip}" >&2
197+
echo "${gateway_ip}"
198+
return 0
199+
fi
200+
fi
201+
done
202+
203+
err "无法获取公网 IP 地址"
204+
return 1
205+
}
206+
178207
########################################
179208
# 获取或创建 Gateway ID
180209
########################################
@@ -189,8 +218,32 @@ get_or_create_gateway() {
189218
return 0
190219
fi
191220

192-
# 尝试创建(Docker 环境使用 higress:8001)
193-
local body="{\"gatewayName\":\"${gateway_name}\",\"gatewayType\":\"HIGRESS\",\"higressConfig\":{\"address\":\"http://higress:8001\",\"username\":\"admin\",\"password\":\"${HIGRESS_PASSWORD}\"}}"
221+
# 获取 Higress Gateway 公网 IP
222+
local gateway_ip
223+
gateway_ip=$(get_higress_gateway_address)
224+
if [[ -z "$gateway_ip" ]]; then
225+
err "无法获取 Higress Gateway 公网 IP,无法创建网关"
226+
return 1
227+
fi
228+
229+
# 使用 jq 构建 JSON 请求体
230+
local body=$(jq -n \
231+
--arg gatewayName "$gateway_name" \
232+
--arg gatewayType "HIGRESS" \
233+
--arg address "http://higress:8001" \
234+
--arg username "admin" \
235+
--arg password "$HIGRESS_PASSWORD" \
236+
--arg gatewayAddress "http://$gateway_ip:8082" \
237+
'{
238+
gatewayName: $gatewayName,
239+
gatewayType: $gatewayType,
240+
higressConfig: {
241+
address: $address,
242+
username: $username,
243+
password: $password,
244+
gatewayAddress: $gatewayAddress
245+
}
246+
}')
194247

195248
call_api "插入网关" "POST" "/api/v1/gateways" "$body" >/dev/null 2>&1 || true
196249

@@ -385,9 +438,20 @@ publish_to_portal() {
385438
local portal_id="$2"
386439
local mcp_name="$3"
387440

388-
if call_api "发布到门户" "POST" "/api/v1/products/${product_id}/publications/${portal_id}" ""; then
389-
log "[${mcp_name}] 发布到门户成功"
390-
return 0
441+
# 构建请求体
442+
local body="{\"portalId\":\"${portal_id}\"}"
443+
444+
if call_api "发布到门户" "POST" "/api/v1/products/${product_id}/publications" "$body"; then
445+
if [[ "$API_HTTP_CODE" =~ ^2[0-9]{2}$ ]]; then
446+
log "[${mcp_name}] 发布到门户成功"
447+
return 0
448+
elif [[ "$API_HTTP_CODE" == "409" ]]; then
449+
log "[${mcp_name}] 产品已发布到门户(跳过)"
450+
return 0
451+
else
452+
log "[${mcp_name}] 发布到门户失败(HTTP ${API_HTTP_CODE}"
453+
return 0 # 允许失败,继续执行
454+
fi
391455
else
392456
log "[${mcp_name}] 发布到门户失败(可能已发布)"
393457
return 0 # 允许失败,继续执行

deploy/docker/scripts/hooks/post_ready.d/60-init-portal-developer.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,15 @@ step_7_subscribe_products() {
524524
fi
525525
else
526526
# 检查是否是不支持订阅的产品(HTTP 400 且包含特定错误消息)
527-
if [[ "$API_HTTP_CODE" == "400" ]] && echo "$API_RESPONSE" | grep -q "不支持订阅"; then
527+
if [[ "$API_HTTP_CODE" == "400" ]] && (echo "$API_RESPONSE" | grep -qi "does not support subscription\|不支持订阅"); then
528528
log "产品 ${product_id} 不支持订阅,跳过"
529529
skipped_count=$((skipped_count + 1))
530530
skipped_products+=("$product_id")
531531
break
532532
fi
533533

534534
# 检查是否是重复订阅(HTTP 400 且包含"重复订阅"),视为成功(幂等性)
535-
if [[ "$API_HTTP_CODE" == "400" ]] && echo "$API_RESPONSE" | grep -q "重复订阅"; then
535+
if [[ "$API_HTTP_CODE" == "400" ]] && (echo "$API_RESPONSE" | grep -qi "duplicate subscription\|重复订阅"); then
536536
log "产品 ${product_id} 已订阅(重复订阅,幂等)"
537537
subscribed=true
538538
break

deploy/helm/scripts/data/higress-mcp.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
"description": "出行小助手",
55
"type": "OPEN_API",
66
"higress": {
7-
"domains": ["travel.assistant.io"],
8-
"enableHttps": false,
97
"serviceSources": [
108
{
119
"type": "dns",
@@ -45,8 +43,6 @@
4543
"description": "基金诊断助手",
4644
"type": "DIRECT_ROUTE",
4745
"higress": {
48-
"domains": ["fund.assistant.io"],
49-
"enableHttps": false,
5046
"serviceSources": [
5147
{
5248
"type": "dns",

deploy/helm/scripts/hooks/post_ready.d/30-init-higress-mcp.sh

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -211,30 +211,6 @@ EOF
211211
call_higress_api "POST" "/v1/service-sources" "$data" "[${mcp_name}] 创建服务源 (${name})"
212212
}
213213

214-
########################################
215-
# 创建域名
216-
########################################
217-
create_domain() {
218-
local domain="$1"
219-
local enable_https="$2"
220-
local mcp_name="$3"
221-
222-
local https_value="off"
223-
if [[ "$enable_https" == "true" ]]; then
224-
https_value="on"
225-
fi
226-
227-
local data=$(cat <<EOF
228-
{
229-
"name": "${domain}",
230-
"enableHttps": "${https_value}"
231-
}
232-
EOF
233-
)
234-
235-
call_higress_api "POST" "/v1/domains" "$data" "[${mcp_name}] 创建域名 (${domain})"
236-
}
237-
238214
########################################
239215
# 处理 OpenAPI 类型的 MCP
240216
########################################
@@ -269,7 +245,6 @@ process_openapi_mcp() {
269245
fi
270246

271247
# 构建请求数据
272-
local domains=$(echo "$mcp_config" | jq -c '.higress.domains')
273248
local services=$(echo "$mcp_config" | jq -c '.higress.services')
274249
local consumer_auth=$(echo "$mcp_config" | jq -c '.higress.consumerAuth')
275250

@@ -278,7 +253,7 @@ process_openapi_mcp() {
278253
"id": null,
279254
"name": "${mcp_name}",
280255
"description": "${description}",
281-
"domains": ${domains},
256+
"domains": [],
282257
"services": ${services},
283258
"type": "OPEN_API",
284259
"consumerAuthInfo": ${consumer_auth},
@@ -307,7 +282,6 @@ process_direct_route_mcp() {
307282
log "处理 DIRECT_ROUTE MCP: ${mcp_name}"
308283

309284
# 构建请求数据
310-
local domains=$(echo "$mcp_config" | jq -c '.higress.domains')
311285
local services=$(echo "$mcp_config" | jq -c '.higress.services')
312286
local consumer_auth=$(echo "$mcp_config" | jq -c '.higress.consumerAuth')
313287

@@ -316,7 +290,7 @@ process_direct_route_mcp() {
316290
"id": null,
317291
"name": "${mcp_name}",
318292
"description": "${description}",
319-
"domains": ${domains},
293+
"domains": [],
320294
"services": ${services},
321295
"type": "DIRECT_ROUTE",
322296
"consumerAuthInfo": ${consumer_auth},
@@ -357,17 +331,7 @@ process_single_mcp() {
357331
fi
358332
done <<< "$sources"
359333

360-
# 2. 创建所有域名
361-
local enable_https=$(echo "$mcp_config" | jq -r '.higress.enableHttps')
362-
local domains=$(echo "$mcp_config" | jq -r '.higress.domains[]')
363-
while IFS= read -r domain; do
364-
if ! create_domain "$domain" "$enable_https" "$mcp_name"; then
365-
err "[${mcp_name}] 创建域名失败"
366-
return 1
367-
fi
368-
done <<< "$domains"
369-
370-
# 3. 根据类型创建 MCP 服务器配置
334+
# 2. 根据类型创建 MCP 服务器配置
371335
if [[ "$mcp_type" == "OPEN_API" ]]; then
372336
if ! process_openapi_mcp "$mcp_config"; then
373337
err "[${mcp_name}] 创建 OpenAPI MCP 失败"

0 commit comments

Comments
 (0)