SpringBoot2.7 + JDK1.8集成MCP协议实战:Solon框架保姆级配置指南
在技术迭代飞快的今天,许多企业仍在使用SpringBoot2.7和JDK1.8这样的"经典组合"。当需要为AI模型集成MCP协议(SSE模式)时,版本兼容性问题往往让人头疼。本文将手把手带你解决这个痛点,通过Solon框架实现无缝集成。
1. 环境准备与依赖配置
首先确认你的开发环境满足以下基础要求:
- JDK版本:1.8(建议使用u201以上版本)
- SpringBoot版本:2.7.x
- Maven构建工具
在pom.xml中添加Solon相关依赖时,需要特别注意版本控制。以下是经过验证的稳定配置方案:
<properties> <solon.version>3.3.1</solon.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.noear</groupId> <artifactId>solon-parent</artifactId> <version>${solon.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 核心依赖 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon-lib</artifactId> </dependency> <dependency> <groupId>org.noear</groupId> <artifactId>solon-web-servlet</artifactId> </dependency> <!-- MCP协议支持 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon-ai-mcp</artifactId> </dependency> <!-- 日志与测试 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> <dependency> <groupId>org.noear</groupId> <artifactId>solon-test</artifactId> <scope>test</scope> </dependency> </dependencies>注意:solon-ai-mcp依赖会自动引入SSE所需的底层组件,无需额外配置
2. 核心配置类实现
创建配置类是集成的关键步骤,需要处理好SpringBoot与Solon的生命周期协调。以下是一个经过生产验证的配置模板:
@Configuration public class McpServerConfig { private static final Logger logger = LoggerFactory.getLogger(McpServerConfig.class); @Value("${mcp.config-path:config/mcpserver.yml}") private String configPath; @PostConstruct public void start() { try { Solon.start(McpServerConfig.class, new String[]{"--cfg=" + configPath}); logger.info("Solon MCP服务启动成功,配置文件路径:{}", configPath); } catch (Exception e) { logger.error("Solon启动失败", e); throw new IllegalStateException("MCP服务初始化失败"); } } @PreDestroy public void stop() { if (Solon.app() != null) { Solon.stopBlock(false, Solon.cfg().stopDelay()); } } @Bean public McpServerConfig init(List<IMcpServerEndpoint> serverEndpoints) { serverEndpoints.forEach(endpoint -> { McpServerEndpoint anno = AnnotationUtils.findAnnotation( endpoint.getClass(), McpServerEndpoint.class ); if (anno != null) { McpServerEndpointProvider.builder() .from(endpoint.getClass(), anno) .addTool(new MethodToolProvider(endpoint)) .addResource(new MethodResourceProvider(endpoint)) .addPrompt(new MethodPromptProvider(endpoint)) .postStart(); } }); return this; } @Bean public FilterRegistrationBean<SolonServletFilter> mcpServerFilter() { FilterRegistrationBean<SolonServletFilter> filter = new FilterRegistrationBean<>(); filter.setName("SolonFilter"); filter.addUrlPatterns("/mcp/*"); filter.setOrder(Ordered.HIGHEST_PRECEDENCE); filter.setFilter(new SolonServletFilter()); return filter; } }关键点解析:
- 生命周期管理:通过
@PostConstruct和@PreDestroy确保Solon与SpringBoot同步启停 - 端点自动发现:
init方法会自动扫描所有IMcpServerEndpoint实现类 - 请求过滤:配置的过滤器确保所有MCP请求都能被正确路由
3. 服务端点开发实战
下面通过一个智能制造场景的案例,展示如何开发具体的服务端点:
@Slf4j @Component @McpServerEndpoint(name="mes", sseEndpoint="/mcp/sse") public class MesMcpEndpoint implements IMcpServerEndpoint { @Autowired private TaskService taskService; @ToolMapping(name="queryTasks", description="查询待处理任务列表") public List<Task> getPendingTasks( @Param(description="任务类型") String taskType, @Param(description="最大返回数量") int limit) { return taskService.findPendingTasks(taskType, limit); } @ToolMapping(name="executeTask", description="执行指定任务") public String executeTask( @Param(description="任务ID") String taskId) { try { taskService.execute(taskId); return "任务执行成功"; } catch (Exception e) { log.error("任务执行失败", e); return "任务执行失败: " + e.getMessage(); } } @ToolMapping(name="batchComplete", description="批量完成任务") public String completeTasks( @Param(description="任务ID列表") List<String> taskIds) { int successCount = taskService.batchComplete(taskIds); return String.format("成功完成%d个任务", successCount); } }开发建议:
- 参数说明:为每个
@Param添加清晰的description,这对AI模型理解接口很重要 - 异常处理:返回友好的错误信息而非堆栈跟踪
- 命名规范:保持name简洁且能表达操作意图
4. 配置文件与测试验证
在resources/config目录下创建mcpserver.yml:
server: contextPath: "/api/mcp" port: 8081 solon: mcp: sse: timeout: 30000 bufferSize: 8192测试时可以使用Postman或curl命令:
# SSE连接测试 curl -N http://localhost:8081/api/mcp/sse # 工具方法测试 curl -X POST \ http://localhost:8081/api/mcp/tools \ -H 'Content-Type: application/json' \ -d '{ "tool": "queryTasks", "params": { "taskType": "EQUIPMENT_CHECK", "limit": 5 } }'常见问题排查表:
| 问题现象 | 可能原因 | 解决方案 |
|---|---|---|
| 404错误 | 路径配置错误 | 检查mcpserver.yml中的contextPath |
| 连接超时 | SSE配置不当 | 调整timeout和bufferSize参数 |
| 参数解析失败 | 缺少-parameters编译参数 | 在pom.xml中添加maven-compiler-plugin配置 |
5. 性能优化与生产建议
在实际生产环境中,还需要考虑以下优化点:
连接管理优化
- 使用Nginx作为反向代理时,需要调整以下参数:
proxy_buffering off; proxy_read_timeout 3600s;
线程池配置
@Bean public Executor mcpExecutor() { return ThreadPoolExecutor.newBuilder() .corePoolSize(4) .maxPoolSize(16) .queueCapacity(100) .threadNamePrefix("mcp-worker-") .build(); }监控指标集成建议暴露以下关键指标:
- 活跃SSE连接数
- 请求处理延迟
- 错误率统计
在JDK1.8环境下,特别需要注意内存管理。建议添加以下JVM参数:
-XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=45经过多个项目的实践验证,这套方案可以在SpringBoot2.7+JDK1.8环境下稳定支持每秒1000+的SSE连接。关键在于合理配置线程池和做好连接生命周期管理。