当前位置: 首页 > news >正文

基于nacos2.5.1的MCP服务端微服务项目开发环境配置简介

基于nacos2.5.1的MCP服务端微服务项目开发环境配置简介

实现MCP的服务
@Tool 是 Spring AI MCP 框架中用于快速暴露业务能力为 AI 工具的核心注解,
该注解实现 Java 方法与 MCP 协议工具的自动映射,
并且可以通过注解的属性 description,
帮助人工智能模型根据用户输入的信息决定是否调用这些工具,
并返回相应的结果。

引用组件包版本关系
JDK 17
spring-boot: 3.3.3
spring-cloud: 2023.0.3
spring-cloud-alibaba:2023.0.3.2
spring-ai.version:1.0.0-M7
spring-ai-alibaba:1.0.0-M7.1
dashscope-sdk-java:2.20.1

工程启动运行结果图:
在这里插入图片描述

一、创建java工程
maven/quickstart工程模板
org.apache.maven.archetypes:maven-archetype-quickstart

Project SDK ms-17 version 17.0.14

pom.xml引用组件配置情况:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.3.3</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.cwgis</groupId><artifactId>galaxy-mcp</artifactId><version>1.0</version><name>galaxy-mcp</name><description>galaxy-mcp</description><url/><properties><java.version>17</java.version><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><spring-boot.version>3.3.3</spring-boot.version><!-- Spring AI --><spring-ai.version>1.0.0-M7</spring-ai.version><spring-ai-alibaba.version>1.0.0-M7.1</spring-ai-alibaba.version><dashscope-sdk-java.version>2.20.1</dashscope-sdk-java.version><spring-cloud.version>2023.0.3</spring-cloud.version><spring-cloud-alibaba.version>2023.0.3.2</spring-cloud-alibaba.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- Nacos Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- add spring ai mcp jar --><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server</artifactId><version>${spring-ai.version}</version></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-mcp-server-webmvc</artifactId><version>${spring-ai.version}</version></dependency></dependencies><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>sonatype-nexus-staging</id><name>Nexus Release Repository</name><url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url></repository><repository><id>aliyun-central</id><name>aliyun-central</name><url>https://maven.aliyun.com/repository/central</url></repository><repository><id>aliyun-public</id><name>aliyun-public</name><url>https://maven.aliyun.com/repository/public</url></repository><repository><id>aliyun-snapshots</id><name>aliyun-snapshots</name><url>https://maven.aliyun.com/repository/apache-snapshots</url></repository><repository><id>aliyun-plugin</id><name>aliyun-plugin</name><url>https://maven.aliyun.com/repository/gradle-plugin</url></repository><repository><id>osgeo</id><name>OSGeo Release Repository</name><url>https://repo.osgeo.org/repository/release/</url></repository></repositories><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><dependencyManagement><dependencies><!--add alibaba java lib --><dependency><groupId>com.alibaba</groupId><artifactId>dashscope-sdk-java</artifactId><version>${dashscope-sdk-java.version}</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></exclusion><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>${spring-ai.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai</artifactId><version>${spring-ai.version}</version></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-mcp-server-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-mcp-server-webmvc-spring-boot-starter</artifactId></dependency><!-- SpringCloud 微服务 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringCloud Alibaba 微服务 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba.version}</version><type>pom</type><scope>import</scope></dependency><!-- SpringBoot 依赖配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>

src/main/resources/application.yml配置情况:

#
server.port: 9001
server.tomcat.max-http-form-post-size: 5120MB
#server.ssl.key-store-type: PKCS12
#server.ssl.key-store: classpath:keystore.p12
#server.ssl.key-store-password: your_password
#
spring:application:name: galaxy-mcpconfig:import: nacos:192.168.30.33:8848#profiles:#  active:devai:chat:client:enabled: true#------------------------------mcp:server:name: galaxy-mcp-serversse-message-endpoint: /mcp/galaxy#------------------------------dashscope:api-key: sk-1chat:options:model: deepseek-r1huggingface:chat:api-key: sk-1openai:api-key:base-url: http://192.168.3.88:8000/chat:options:model: qwq32b-q8ollama:base-url: http://localhost:11434/chat:model: llama3options:temperature: 0.7#------------------------------localai:endpoint: 192.168.3.88:11434    #ollama model=deepseek  192.168.1.82:11434cloud:nacos:config:server-addr: 192.168.30.33:8848#namespace: 'cwgis'group: 'mcp'username: 'nacos'password: 'nacos'file-extension: yamlenable: truerefresh-enabled: truediscovery:# Nacosserver-addr: 192.168.30.33:8848#namespace: 'cwgis'group: 'mcp'management:endpoint:health:show-details: alwaysendpoints:web:exposure:include: '*'logging:level:com.alibaba.cloud.nacos.configdata: debug

定义MCP接口类

package com.cwgis.mcp.service;public interface GzhService {String recommendGZhInfo();String bestContext();
}

定义MCP接口实现类

package com.cwgis.mcp.service;import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;//这里定义了两个MCP方法,一个推荐技术类公众号,一个是选择公众号中最好的文章。
@Service("gzhService")
public class GzhServiceImpl implements GzhService {@Tool(description = "推荐技术类公众号")@Overridepublic String recommendGZhInfo() {return "推荐【编程朝花夕拾】公众号,该公众号精选编程干货,回顾技术经典,分享实战经验、可以助你温故知新、在代码世界不断精进";}@Tool(description = "公众号中最好的文章")@Overridepublic String bestContext() {return "技术类中【编程朝花夕拾】公众号的文章,都偏向技术干货,对于技术宅,都是好内容!";}
}

注册MCP方法

package com.cwgis.mcp.config;import com.cwgis.mcp.service.GzhService;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;//启动之后,日志上显示注册了2个工具。说明注册成功了。
//注册MCP方法
@Configuration
public class ToolCallbackProviderConfig {@Beanpublic ToolCallbackProvider gzhRecommendTools(GzhService gzhService) {return MethodToolCallbackProvider.builder().toolObjects(gzhService).build();}
}

nacos配置类 src/main/java/com.cwgis.mcp/config/NacosConfigManager.java

package com.cwgis.mcp.config;import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;import java.util.Properties;
import java.util.concurrent.Executor;public class NacosConfigManager {private static final String SERVER_ADDR = "192.168.30.33:8848"; // Nacos服务器地址private static final String DATA_ID = "cwgis"; // 配置ID   your-data-idprivate static final String GROUP = "mcp"; // 配置分组private static final Integer CONNECTION_POOL_SIZE=50;private ConfigService configService;public NacosConfigManager() {try {Properties properties = new Properties();properties.put("serverAddr", SERVER_ADDR);//properties.put("namespace", DATA_ID);//properties.put("group", GROUP);//添加附加配置参数//properties.put("encode", "UTF-8");//properties.put("DASHSCOPE_CONNECTION_POOL_SIZE", CONNECTION_POOL_SIZE);//properties.put("DASHSCOPE_CONNECTION_TIMEOUT",5000);//properties.put("DASHSCOPE_MAXIMUM_ASYNC_REQUESTS",100);//properties.put("DASHSCOPE_MAXIMUM_ASYNC_REQUESTS_PER_HOST",100);//properties.put("DASHSCOPE_READ_TIMEOUT",30000);//properties.put("DASHSCOPE_WRITE_TIMEOUT",30000);//properties.put("DASHSCOPE_CONNECTION_IDLE_TIME",500000);//configService = NacosFactory.createConfigService(properties);} catch (NacosException e) {e.printStackTrace();}}// 获取配置public String getConfig() {try {return configService.getConfig(DATA_ID, GROUP, 5000);    //5000 是超时ms} catch (NacosException e) {e.printStackTrace();return null;}}// 添加配置监听器public void addListener() {try {configService.addListener(DATA_ID, GROUP, new Listener() {@Overridepublic Executor getExecutor() {return null;}@Overridepublic void receiveConfigInfo(String configInfo) {System.out.println("配置更新: " + configInfo);// 处理配置更新事件}});} catch (NacosException e) {e.printStackTrace();}}// 发布配置public boolean publishConfig(String content) {try {return configService.publishConfig(DATA_ID, GROUP, content);} catch (NacosException e) {e.printStackTrace();return false;}}
}

main入口类

package com.cwgis.mcp;import com.cwgis.mcp.config.NacosConfigManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient // 启用服务发现功能
public class GalaxyMcpApplication {public static void main(String[] args) {// 初始化Nacos配置管理器NacosConfigManager configManager = new NacosConfigManager();// 获取初始配置String config = configManager.getConfig();System.out.println("初始配置: " + config);// 添加配置监听器configManager.addListener();ConfigurableApplicationContext app=SpringApplication.run(GalaxyMcpApplication.class, args);Environment env = app.getEnvironment();String port = env.getProperty("server.port");System.out.println("==============GalaxyMcpApplication启动成功,服务端口为:" + port);}}

运行结果图:

17:10:39.260 [main] INFO com.alibaba.nacos.plugin.auth.spi.client.ClientAuthPluginManager -- [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
17:10:39.260 [main] INFO com.alibaba.nacos.plugin.auth.spi.client.ClientAuthPluginManager -- [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success..   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::                (v3.3.3)2025-06-06T17:10:39.524+08:00  INFO 7536 --- [galaxy-mcp] [           main] com.cwgis.mcp.GalaxyMcpApplication       : Starting GalaxyMcpApplication using Java 17.0.14 with PID 7536 (D:\cwgis_nacos\galaxy-mcp\target\classes started by Administrator in D:\cwgis_nacos\galaxy-mcp)
2025-06-06T17:10:39.525+08:00  INFO 7536 --- [galaxy-mcp] [           main] com.cwgis.mcp.GalaxyMcpApplication       : No active profile set, falling back to 1 default profile: "default"
2025-06-06T17:10:39.562+08:00  WARN 7536 --- [galaxy-mcp] [           main] c.a.c.n.c.NacosConfigDataLoader          : [Nacos Config] config[dataId=192.168.30.33:8848, group=mcp] is empty
2025-06-06T17:10:40.102+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=e809c51a-85b0-3b6f-829c-e7ba8cddc520
2025-06-06T17:10:40.124+08:00  WARN 7536 --- [galaxy-mcp] [           main] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.cloud.nacos.NacosConfigAutoConfiguration' of type [com.alibaba.cloud.nacos.NacosConfigAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). The currently created BeanPostProcessor [nacosAnnotationProcessor] is declared through a non-static factory method on that class; consider declaring it as static instead.
2025-06-06T17:10:40.340+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 9001 (http)
2025-06-06T17:10:40.353+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-06-06T17:10:40.354+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.28]
2025-06-06T17:10:40.414+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-06-06T17:10:40.414+08:00  INFO 7536 --- [galaxy-mcp] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 850 ms
2025-06-06T17:10:40.916+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.s.a.m.s.a.McpServerAutoConfiguration   : Registered tools: 2, notification: true
2025-06-06T17:10:41.065+08:00  INFO 7536 --- [galaxy-mcp] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 9001 (http) with context path '/'
2025-06-06T17:10:41.078+08:00  INFO 7536 --- [galaxy-mcp] [           main] c.a.n.p.a.s.c.ClientAuthPluginManager    : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
2025-06-06T17:10:41.078+08:00  INFO 7536 --- [galaxy-mcp] [           main] c.a.n.p.a.s.c.ClientAuthPluginManager    : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success.
2025-06-06T17:10:41.114+08:00  INFO 7536 --- [galaxy-mcp] [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, mcp galaxy-mcp 192.168.1.82:9001 register finished
2025-06-06T17:10:41.169+08:00  INFO 7536 --- [galaxy-mcp] [           main] com.cwgis.mcp.GalaxyMcpApplication       : Started GalaxyMcpApplication in 2.233 seconds (process running for 3.966)
2025-06-06T17:10:41.175+08:00  INFO 7536 --- [galaxy-mcp] [           main] c.a.c.n.refresh.NacosContextRefresher    : [Nacos Config] Listening config: dataId=192.168.30.33:8848, group=mcp
==============GalaxyMcpApplication启动成功,服务端口为:9001
2025-06-06T17:10:50.829+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2025-06-06T17:10:50.829+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2025-06-06T17:10:50.830+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
2025-06-06T17:12:03.109+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-5] i.m.server.McpAsyncServer                : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=galaxy-mcp-client - server1, version=1.0.0]
2025-06-06T17:13:08.244+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-6] i.m.server.McpAsyncServer                : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=galaxy-mcp-client - server1, version=1.0.0]
2025-06-06T17:42:25.730+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-7] i.m.server.McpAsyncServer                : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=galaxy-mcp-client - server1, version=1.0.0]
2025-06-06T17:43:48.714+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-1] i.m.server.McpAsyncServer                : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=galaxy-mcp-client - server1, version=1.0.0]
2025-06-06T18:40:45.788+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-7] i.m.server.McpAsyncServer                : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=galaxy-mcp-client - server1, version=1.0.0]
2025-06-06T18:43:27.780+08:00  INFO 7536 --- [galaxy-mcp] [nio-9001-exec-6] i.m.server.McpAsyncServer                : Client initialize request - Protocol: 2024-11-05, Capabilities: ClientCapabilities[experimental=null, roots=null, sampling=null], Info: Implementation[name=galaxy-mcp-client - server1, version=1.0.0]

运行结果中显示有这个字符表达mcp服务端运行成功
main] o.s.a.m.s.a.McpServerAutoConfiguration : Registered tools: 2, notification: true

main] o.s.a.m.s.a.McpServerAutoConfiguration   : Registered tools: 2, notification: true

本blog地址:https://blog.csdn.net/hsg77

http://www.xdnf.cn/news/911629.html

相关文章:

  • 提升CPU、内存及磁盘利用率脚本
  • 群晖文件操作API
  • 自然语言处理的发展
  • (33)课54--??:3 张表的 join-on 连接举例,多表查询总结。
  • MyBatis————入门
  • 【SSM】MyBatisPlus笔记:快速上手MyBatisPlus
  • C++算法动态规划3
  • VUE前端实现自动打包成压缩文件
  • Linux缓冲区与glibc封装:入门指南
  • 智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
  • 网络编程及原理(三)
  • 2025最新VMware17如何通过官网进行下载
  • [蓝桥杯]迷宫与陷阱
  • 端游如何反调试
  • 几何引擎对比:OpenCasCade、ACIS、Parasolid和CGM
  • 使用 FastMCP 构建你的第一个 MCP 服务:从零开始的 Python 示例
  • DAX权威指南8:DAX引擎与存储优化
  • 缓解骨质疏松 —— 补钙和补维 D
  • TeamCity Agent 配置完整教程(配合 Docker Compose 快速部署)
  • Steam 搬砖项目深度拆解:从抵触到真香的转型之路
  • 迈向群体智能-具身大小脑协作框架RoboOS及具身大脑RoboBrain
  • vim 替换 字符串 带 斜杠
  • 12-Oracle 23ai Vector 使用ONNX模型生成向量嵌入
  • RK3288项目(三)--linux内核之V4L2框架及ov9281驱动分析(上)
  • 手写muduo网络库(零):多线程中使用 weakptr 跨线程监听生命状态
  • 【android bluetooth 协议分析 02】【bluetooth hal 层详解 8】【高通蓝牙hal-进程被杀之前日志收集流程】
  • jmeter之导出接口
  • 立定跳远-二分
  • 20250606-C#知识:委托和事件
  • 企业引入数字孪生,优化决策,提升市场竞争力的秘诀