微服务如何通过springgateway调用python服务,现在通过服务名调用一直出现
{
"msg": "Failed to handle request [GET http://10.24.0.163:8081/zjl-knowledge-service/api/v1/health]: 503 SERVICE_UNAVAILABLE \"Unable to find instance for zjl-knowledge-service\"",
"code": 503,
"data": null
}
python本身有这个API可以访问到么? /api/v1/health
注册到nacos是需要对提供health接口给nacos心跳检测的,如果获取不到就会出现服务不可用的情况
你脱离Gateway,直接访问这个API地址看看返回的是什么,看看是否符合nacos的检测要求。
本地简单测了下,可以调用成功,你加个这个配置类试试
package org.springblade.gateway.config;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.URI;
import java.util.List;
/**
* Python服务动态路由配置
*
* @author BladeX
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class DirectRouteConfig {
private final NacosDiscoveryProperties nacosProperties;
private static final String SERVICE_NAME = "bladex-python-demo";
private static final String SERVICE_PATH = "/bladex-python-demo/**";
private static final String ROUTE_ID = "bladex-python-demo-direct";
@Bean
public RouteLocator pythonServiceRoute(RouteLocatorBuilder builder) {
return builder.routes()
.route(ROUTE_ID, r -> r
.path(SERVICE_PATH)
.filters(f -> f
.stripPrefix(1)
.addRequestHeader("X-Service-Type", "Python")
)
.uri(getServiceUri()))
.build();
}
/**
* 动态获取服务URI
* 从Nacos实时获取服务地址,支持服务地址变更
*/
private URI getServiceUri() {
try {
NamingService namingService = nacosProperties.namingServiceInstance();
String group = nacosProperties.getGroup() != null ?
nacosProperties.getGroup() : "DEFAULT_GROUP";
List<Instance> instances = namingService.selectInstances(SERVICE_NAME, group, true);
if (!instances.isEmpty()) {
Instance instance = instances.get(0);
String uri = String.format("http://%s:%d", instance.getIp(), instance.getPort());
log.info("Dynamic route to {} service: {}", SERVICE_NAME, uri);
return URI.create(uri);
}
} catch (Exception e) {
log.error("Failed to get service instance from Nacos, using fallback", e);
}
// 仅作为最后的备用方案
log.warn("Using fallback URI for {} service", SERVICE_NAME);
return URI.create("http://127.0.0.1:8090");
}
}
直接调用接口返回
{
"msg": "所有系统组件运行正常",
"code": 200,
"data": {
"overall_status": "healthy",
"timestamp": "2024-01-01T00:00:00Z",
"version": "0.1.0",
"environment": "production",
"checks": {
"application": {
"status": "healthy",
"app_name": "ZJL知识库系统",
"version": "0.1.0",
"uptime": "正常运行",
"config_loaded": true,
"dependencies_loaded": true
},
"mysql": {
"status": "healthy",
"connection_pool": "active",
"response_time_ms": 15,
"active_connections": 5,
"max_connections": 100
},
"milvus": {
"status": "healthy",
"connection": "active",
"response_time_ms": 25,
"collections_count": 3,
"index_status": "built"
},
"redis": {
"status": "healthy",
"connection": "active",
"memory_usage": "45MB",
"cache_hit_rate": 0.85,
"response_time_ms": 5
},
"embedding_service": {
"status": "healthy",
"provider": "azure_openai",
"model": "text-embedding-3-large",
"vector_dimension": 3072,
"test_response_time_ms": 120,
"api_quota_remaining": "充足"
},
"azure_openai": {
"status": "healthy",
"endpoint": "已连接",
"api_version": "2023-05-15",
"deployment": "text-embedding-3-large",
"quota_usage": "正常",
"response_time_ms": 180
},
"minio_storage": {
"status": "healthy",
"connection": "active",
"bucket_accessible": true,
"storage_used": "1.2GB",
"storage_available": "98.8GB",
"response_time_ms": 35
},
"local_storage": {
"status": "healthy",
"disk_usage": "65%",
"available_space": "50GB",
"temp_directory": "可写",
"log_directory": "可写"
}
},
"severity": "low",
"message": "所有系统组件运行正常",
"performance_summary": {
"total_checks": 8,
"healthy_services": 8,
"warning_services": 0,
"critical_services": 0,
"overall_response_time_ms": 85,
"system_load": "normal"
},
"recommendations": [
"✅ 系统运行良好,建议保持定期监控"
],
"support_info": {
"documentation": "https://docs.example.com/health-check",
"support_email": "support@example.com",
"emergency_contact": "紧急联系方式",
"monitoring_dashboard": "监控面板地址"
}
}
}
你用一个原生的springcloud gateway工程(不用bladex自带,新建个空白工程)去注册到nacos然后路由你的python服务,看看能不能成功,如果原版的可以,bladex不行,请把demo发我们邮箱 bladejava@qq.com ,我们安排去排查。
如果原版的也不行,那就是spring本身的配置问题,需要去找到准确的配置看看如何进行路由。
刚刚建了一个demo测试 原生的springcloud gateway工程是可以调用的 我现在将demo发送你们邮箱
原生调用
bladex网关调用
往上看,有demo配置
好的 现在可以了 但是还有一个问题 调用 http://10.24.0.163:8081/zjl-knowledge-service/api/v1/health 实际调用是 http://serverIp:serverProt/v1/health /api被重写掉了
扫一扫访问 Blade技术社区 移动端