news 2026/7/3 2:52:55

Spring AI Alibaba + Nacos 构建 A2A 分布式智能体

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring AI Alibaba + Nacos 构建 A2A 分布式智能体

一、Spring AI Alibaba

在本专栏上一篇文章中,介绍了Python + AgentScope完整实现A2A智能体服务的注册、集群部署、Nacos服务发现与客户端负载调用能力,解决了多智能体框架不统一、智能体无法高效协同的问题。

过程如下图所示:

文章地址:

基于A2A + AgentScope + Nacos 构建分布式高可用异构 Agent 方案

https://mp.weixin.qq.com/s/QAyTbcti_N-hinRvr2iHrw

但在企业真实落地场景中,绝大多数核心业务系统、微服务架构均基于Java技术栈构建。如果AI智能体仅能在Python侧独立运行,无法与Java业务体系打通,会形成新的技术孤岛,无法真正融入企业微服务生态。

因此本篇内容将聚焦Java生态上,基于Spring AI Alibaba快速搭建JavaA2A服务端与客户端,实现Java智能体注册至Nacos、并实现客户端远程调用。

Spring AI Alibaba是阿里开源的Spring AI生态扩展,旨在帮助Java开发者更便捷地构建AI应用。在最新的版本中,Spring AI Alibaba已经全面拥抱A2A协议,提供了开箱即用的Starter组件:

  • spring-ai-alibaba-starter-a2a-server:用于快速将Agent包装为标准的A2A服务端。
  • spring-ai-alibaba-starter-a2a-client:用于在Spring环境中作为客户端,发起A2A协议调用。
  • spring-ai-alibaba-starter-a2a-registry:深度集成Nacos,实现A2A AgentCard的自动注册与发现。

这意味着,Java开发者无需手动处理A2A协议底层的JSON-RPC转换和HTTP路由,只需通过简单的配置和注解,就能让Spring Boot微服务具备Agent互联能力。

核心技术栈版本延续上篇规范,保证前后实验兼容性:

  • • Nacos:3.2.0(最低3.0+支持AI注册中心能力)
  • • Spring Boot:3.4.0
  • • Spring AI Alibaba:1.0.0.4+
  • • 模型服务:ModelScope``Qwen3.5系列开源模型

开始前请提前安装好Nacos服务, 安装过程可参考官方文档:

https://nacos.io/docs/latest/quickstart/quick-start/

二、A2A 服务端实现

首先,我们新建一个Spring Boot项目,将Java端的天气助手Agent封装为A2A服务并注册到Nacos

2.1 项目依赖

pom引入WebFlux、Spring AI Alibaba A2A Server、Registry以及OpenAI模型依赖:

<?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.4.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>a2a-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>a2a-demo</name> <description>a2a-demo</description> <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Spring AI Alibaba版本1.0.0.4及以上 --> <spring.ai.alibaba.version>1.0.0.4</spring.ai.alibaba.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 引入A2A Server starter --> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-starter-a2a-server</artifactId> <version>${spring.ai.alibaba.version}</version> </dependency> <!-- 引入A2A Nacos 注册中心 --> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-starter-a2a-registry</artifactId> <version>${spring.ai.alibaba.version}</version> </dependency> <!-- 引入OpenAI --> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-starter-model-openai</artifactId> <version>1.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.a2ademo.A2aDemoApplication</mainClass> <skip>true</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>

2.2 配置文件

指定模型连接信息、Nacos注册中心以及Agent元数据:

server: port: 8085spring: application: name: a2a-server-example ai: openai: base-url: https://api-inference.modelscope.cn api-key: ms-xxx ## 替换为你的key chat: options: model: Qwen/Qwen3.5-35B-A3B alibaba: a2a: # 配置Nacos信息 nacos: server-addr: ${NACOS_ADDRESS:localhost:8848} username: ${NACOS_USERNAME:nacos} password: ${NACOS_PASSWORD:123456} server: version: 1.0.1 card: # 配置Agent URL url: http://localhost:8085/a2a

这里a2a.server.card.url非常关键,它定义了该AgentA2A协议下暴露的地址。启动时,框架会自动构建AgentCard并注册至Nacos中。

2.3 构建测试工具

使用@Tool注解定义一个简单的天气伪工具,供智能体调用,以测试工具调用能力:

@Componentpublic class WeatherTools { @Tool(description = "查看某个地区的最新天气情况") public String weather(String local) { if (StringUtils.isBlank(local)){ return "参数:local 不可为空!"; } return local + ",地区的最新天气为多云转晴,30-35摄氏度。"; }}

2.4 构建 ReactAgent 智能体

通过ReactAgent将大模型与工具整合构造智能体:

@Configurationpublic class RootAgentConfiguration { @Bean public List<ToolCallback> tools(WeatherTools tools) { return new ArrayList<ToolCallback>(List.of(ToolCallbacks.from(tools))); } @Bean @Primary public BaseAgent rootAgent(OpenAiChatModel chatModel, List<ToolCallback> tools) throws GraphStateException { return ReactAgent.builder() .name("xiaobichao") .description("小毕超天气助手") .model(chatModel) .instruction("你叫小毕超,是一个天气智能助手!") .inputKey("input") .tools(tools) .build(); }}

2.5 启动服务端

启动Spring Boot项目:

观察Nacos注册情况,可以看到名为 xiaobichao 的智能体已自动注册:

点进详情,可以看到注册的URL等信息,与配置一致:

至此,A2A服务端已经就绪。它与上一篇用AgentScope编写的Python服务端在协议层面完全互通,可以被任意支持A2A的客户端调用。

三、A2A 客户端实现并调用测试

接下来我们创建一个新的Spring Boot客户端项目,通过Nacos发现刚才的xiaobichao服务,并进行流式调用。

3.1 项目依赖

客户端pom需要引入spring-ai-alibaba-starter-a2a-clientspring-ai-alibaba-starter-a2a-registry

<?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.4.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>a2a-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>a2a-demo</name> <description>a2a-demo</description> <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Spring AI Alibaba版本1.0.0.4及以上 --> <spring.ai.alibaba.version>1.0.0.4</spring.ai.alibaba.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 引入A2A Client starter --> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-starter-a2a-client</artifactId> <version>${spring.ai.alibaba.version}</version> </dependency> <!-- 引入A2A Nacos 注册中心 --> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-starter-a2a-registry</artifactId> <version>${spring.ai.alibaba.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.a2ademo.A2aDemoApplication</mainClass> <skip>true</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>

3.2 配置文件

application.yml 只要指定Nacos地址:

server: port: 8086spring: application: name: a2a-client-example ai: alibaba: a2a: nacos: # 开启从Nacos中自动发现Agent discovery: enabled: true server-addr: ${NACOS_ADDRESS:localhost:8848} username: ${NACOS_USERNAME:nacos} password: ${NACOS_PASSWORD:123456}

3.3 构建远程智能体代理

创建一个A2aRemoteAgent,用于代理远程智能体。

@Configurationpublic class RootAgentConfiguration { @Bean public BaseAgent rootAgent(AgentCardProvider agentCardProvider) throws GraphStateException { return A2aRemoteAgent.builder() .agentCardProvider(agentCardProvider) .name("xiaobichao").description("小毕超天气智能助手").build(); }}

AgentCardProvider会自动根据nameNacos中查找对应的AgentCard,并建立通信通道。整个过程开发者无需关心远程服务地址和协议细节。

3.4 构建测试接口

@RestController@RequestMapping("/")public class TestController { private final BaseAgent rootAgent; public TestController(BaseAgent rootAgent) { this.rootAgent = rootAgent; } @GetMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<ServerSentEvent<String>> stream(@RequestParam("question") String question) throws GraphStateException, GraphRunnerException { return rootAgent.stream(Map.of("input", List.of(new UserMessage(question)))) .mapNotNull(output -> { System.out.println("stream agent invoke : " + output.toString()); if (output.isSTART() || output.isEND()) { return null; } if (output instanceof StreamingOutput) { String chunk = ((StreamingOutput) output).chunk(); return ServerSentEvent.<String>builder() .data(chunk) .build(); } return null; }); }}

启动服务:

3.5 远程调用测试

学AI大模型的正确顺序,千万不要搞错了

🤔2026年AI风口已来!各行各业的AI渗透肉眼可见,超多公司要么转型做AI相关产品,要么高薪挖AI技术人才,机遇直接摆在眼前!

有往AI方向发展,或者本身有后端编程基础的朋友,直接冲AI大模型应用开发转岗超合适!

就算暂时不打算转岗,了解大模型、RAG、Prompt、Agent这些热门概念,能上手做简单项目,也绝对是求职加分王🔋

📝给大家整理了超全最新的AI大模型应用开发学习清单和资料,手把手帮你快速入门!👇👇

学习路线:

✅大模型基础认知—大模型核心原理、发展历程、主流模型(GPT、文心一言等)特点解析
✅核心技术模块—RAG检索增强生成、Prompt工程实战、Agent智能体开发逻辑
✅开发基础能力—Python进阶、API接口调用、大模型开发框架(LangChain等)实操
✅应用场景开发—智能问答系统、企业知识库、AIGC内容生成工具、行业定制化大模型应用
✅项目落地流程—需求拆解、技术选型、模型调优、测试上线、运维迭代
✅面试求职冲刺—岗位JD解析、简历AI项目包装、高频面试题汇总、模拟面经

以上6大模块,看似清晰好上手,实则每个部分都有扎实的核心内容需要吃透!

我把大模型的学习全流程已经整理📚好了!抓住AI时代风口,轻松解锁职业新可能,希望大家都能把握机遇,实现薪资/职业跃迁~

这份完整版的大模型 AI 学习资料已经上传CSDN,朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/3 2:52:11

企业AI转型困境与能力建设实战指南

1. AI投资热潮下的真实困境&#xff1a;为什么99%的企业仍在摸索&#xff1f;2023年全球AI投资规模突破3000亿美元&#xff0c;但麦肯锡最新调研显示&#xff0c;只有1%的企业认为自己达到了"成熟应用"阶段。这个惊人的差距背后&#xff0c;隐藏着一个被多数决策者忽…

作者头像 李华
网站建设 2026/7/3 2:49:39

三进制太玄经·八十一首(坤至乾·每行一卦·配原文)

三进制太玄经八十一首&#xff08;坤至乾每行一卦配原文&#xff09;符号&#xff1a;阳/天 ○中/人 −阴/地 &#xff5c; 次序&#xff1a;方州部首 &#xff5c; 中极&#xff1a;○○○○&#xff08;太极&#xff09;&#xff5c; 每首附《太玄经》核心原文测曰1 −−−−…

作者头像 李华
网站建设 2026/7/3 2:48:23

企业基础设施的标准抽象

在 2020 年&#xff0c;没有人再会去质疑一个平台团队采纳 Kubernetes 作为自己的基础设施的合理性。事实上&#xff0c;2020 年的 Kubernetes 项目已经非常接近于地完成了它最重要的使命&#xff0c;即&#xff1a;为云计算基础设施带来一层可以让平台团队基于此构造“一切”的…

作者头像 李华
网站建设 2026/7/3 2:48:07

Windows 10系统自动化清理方案:模块化配置与性能优化指南

Windows 10系统自动化清理方案&#xff1a;模块化配置与性能优化指南 【免费下载链接】Windows10Debloater Script to remove Windows 10 bloatware. 项目地址: https://gitcode.com/gh_mirrors/wi/Windows10Debloater Windows10Debloater是一款基于PowerShell开发的Win…

作者头像 李华
网站建设 2026/7/3 2:47:41

WebSocket 重连后 K 线还缺?Python 检测缺口 + REST 回补 + gap_report 留痕**

摘要 WebSocket 行情断流后重连成功&#xff0c;系统显示一切正常——但中间缺的那几根 K 线不会自己回来。连接恢复不等于数据连续&#xff0c;断流窗口必须通过 REST K 线独立回补&#xff0c;并用 gap_report 把每一次回补留痕。本文给出一套完整的 Python 方案&#xff1a…

作者头像 李华
网站建设 2026/7/3 2:44:49

计算机Java毕设实战-基于 SpringBoot 的水务灾害应急处置决策支持系统的设计与实现 智慧水务调度监控与应急响应系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】

博主介绍&#xff1a;✌️码农一枚 &#xff0c;专注于大学生项目实战开发、讲解和毕业&#x1f6a2;文撰写修改等。全栈领域优质创作者&#xff0c;博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围&#xff1a;&am…

作者头像 李华