开源了!AgentMesh —— 一个让多 Agent 智能协作的 Java 框架
开源了!AgentMesh —— 一个让多 Agent 智能协作的 Java 框架
不止于 Tool Calling,而是让多个 Agent 智能路由、并行协作、容错转移。
为什么又造一个轮子?
用 Spring AI、LangChain4j 做单 Agent + Tool Calling 很爽,但一旦涉及多 Agent 协作,问题就来了:
- 用户问"帮我规划北京三日游"——该找哪个 Agent?天气?旅行攻略?酒店预订?
- 某个 Agent 挂了怎么办?直接报错给用户?
- 多个 Agent 并行执行,结果怎么汇总?
市面上的框架要么不支持多 Agent 编排,要么路由靠硬编码规则,要么没有故障转移。于是就有了 AgentMesh。
AgentMesh 是什么?
一个 Java 生态的多 Agent 智能协作框架,构建在 Spring Boot 3.3.5 + JDK 21 之上,提供从 Agent 注册、智能路由、编排执行到可观测性的完整链路。
核心功能
1. 智能路由 —— 自动选择最合适的 Agent
两阶段 LLM 路由策略:先用关键词快速粗筛候选 Agent,再让 LLM 精排打分,选出最优解。支持 A/B 测试对比效果,LRU + TTL 缓存降低延迟,低置信度时自动回退到关键词路由。
agentmesh:
routing:
strategy: llm # keyword | llm | ab
llm:
top-k: 5
confidence-threshold: 0.6
timeout: 5s
cache:
enabled: true
max-size: 100
ttl: 300s
2. 故障转移(Failover)—— Agent 挂了自动切换
标记 Agent 为 retryable,超时(TimeoutException)、连接失败(ConnectException)、5xx 错误自动切换到备用 Agent。流式响应中途失败不触发重试,避免重复执行。
AgentConfig.builder()
.agentId("primary-agent")
.retryable(true) // 启用 failover
.build();
3. 多 Agent 编排 —— 顺序、并行、条件
三种编排模式,覆盖不同场景:
// 顺序编排:Agent1 → Agent2 → Agent3
new SequentialAgentOrchestrator(factory, agentClient);
// 并行编排:多个 Agent 同时执行,结果汇总
new ParallelOrchestrator(factory, agentClient, metrics);
// 条件路由 + failover:LLM 选最优 Agent,失败自动切换
new ConditionalOrchestrator(factory, agentClient, routingStrategy);
4. A2A 协议(Agent-to-Agent)
支持 AgentCard、AgentSkill、Task、Chat、SSE 流式响应,让不同 Agent 之间通过标准协议通信和协作。
5. 远程工具发现
自动从远程 Agent 拉取工具列表(Skills),注册到本地 ToolRegistry,无需手动配置跨 Agent 的工具调用。
6. 四家 LLM 适配
一套接口,多模型切换:
| 模型 | 适配器 |
|---|---|
| 阿里云百炼(DashScope) | DashScopeAdapter |
| OpenAI | OpenAiAdapter |
| Ollama(本地部署) | OllamaAdapter |
| DeepSeek | DeepSeekAdapter |
7. ReAct 推理引擎
基于 LLM Function Calling 的 Thought → Action → Observation 推理循环,支持多轮工具调用和结果收敛。
8. 可观测性
所有指标自动注册到 Micrometer,对接 Prometheus + Grafana:
| 指标 | 说明 |
|---|---|
agentmesh.agent.invocations | Agent 调用次数 |
agentmesh.agent.latency | Agent 延迟 P50/P95/P99 |
agentmesh.tool.executions | 工具执行次数 |
agentmesh.llm.calls | LLM 调用次数 |
agentmesh.llm.tokens | Token 消耗 |
agentmesh.routing.cache | 路由缓存命中率 |
agentmesh.routing.ab.consistency | A/B 测试一致率 |
9. 记忆与规划(Memory & Planning)
- 短期记忆:滑动窗口保留最近消息 + LLM 摘要压缩
- 长期记忆:LLM 提取记忆信息 + 向量存储
- 任务规划:LLM 分解复杂任务为子任务 DAG,按依赖关系执行
10. 会话与任务管理
- 会话管理:内存和 JDBC 两种实现,支持多轮对话上下文
- 任务管理:异步任务提交与轮询,乐观锁防并发冲突
和同类框架对比
| 能力 | Spring AI | LangChain4j | AgentScope | AgentMesh |
|---|---|---|---|---|
| 多 Agent 编排 | 基础 | 实验性 | 基础 | 顺序/并行/条件 |
| 智能路由 | ❌ | ❌ | ❌ | 两阶段 LLM + A/B |
| Failover | ❌ | ❌ | ❌ | 异常类型判断自动切换 |
| 路由缓存 | ❌ | ❌ | ❌ | LRU + TTL + SHA-256 |
| A2A 协议 | ❌ | ❌ | ✅ | ✅ |
| 远程工具发现 | ❌ | ❌ | ❌ | 自动拉取注册 |
| 可观测性 | 基础 | 需自建 | 基础 | P50/P95/P99 + traceId |
| 记忆与规划 | ❌ | ❌ | ❌ | 短期+长期记忆 + DAG 规划 |
模块结构
agentmesh/
├── agentmesh-bom/ # 版本管理(Bill of Materials)
├── agentmesh-core/ # 核心框架
│ ├── agent/ # Agent 定义与编排引擎
│ ├── routing/ # 路由策略(关键词/LLM/A/B)
│ ├── llm/ # LLM 客户端与适配器
│ ├── tool/ # 工具抽象与注册中心
│ ├── remote/ # 远程 Agent 调用与工具发现
│ ├── protocol/ # A2A 协议(AgentCard/Task/SSE)
│ ├── registry/ # Agent 注册中心
│ ├── infrastructure/ # 可观测性(指标/traceId)
│ ├── session/ # 会话管理
│ ├── task/ # 任务管理
│ ├── memory/ # 短期/长期记忆
│ ├── planning/ # 任务规划
│ └── prompt/ # Prompt 模板引擎
├── agentmesh-rag/ # RAG 知识库模块(Beta)
└── agentmesh-examples/ # 示例项目
快速开始
环境要求
- JDK 21+
- Maven 3.9+
1. 添加依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.github.xserein666</groupId>
<artifactId>agentmesh-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.github.xserein666</groupId>
<artifactId>agentmesh-core</artifactId>
</dependency>
</dependencies>
2. 配置
dashscope:
api-key: ${DASHSCOPE_API_KEY} # 阿里云百炼 API Key
agentmesh:
registry:
self:
agent-id: my-agent
name: "我的智能助手"
description: "一个支持天气查询和知识检索的 Agent"
version: "1.0.0"
url: http://localhost:8080
routing:
strategy: keyword # keyword | llm | ab
3. 定义 Agent 和工具
@Configuration
public class AgentConfig {
@Bean
public Tool weatherTool() {
return Tool.of("weather", "查询指定城市的天气",
input -> {
String city = (String) input.get("city");
return Map.of("result", city + ":晴,25°C");
});
}
@Bean
public AgentConfig myAgent() {
return AgentConfig.builder()
.agentId("weather-agent")
.description("天气查询助手,可以查询全国各城市的天气")
.systemPrompt("你是一个专业的天气助手,请根据用户需求查询天气。")
.routingTags(List.of("天气", "温度", "降雨"))
.retryable(true)
.build();
}
}
4. 运行 Demo
git clone https://github.com/XSerein666/AgentMesh.git
cd AgentMesh/agentmesh-examples/agentmesh-demo
set DASHSCOPE_API_KEY=sk-your-key
mvn spring-boot:run
访问 http://localhost:8080/chat?msg=北京今天天气怎么样
开源地址
- GitHub:https://github.com/XSerein666/AgentMesh
- License:Apache 2.0
- 欢迎 Star、Issue、PR!—
如果你在做多 Agent 相关的项目,或者对智能路由、故障转移感兴趣,欢迎来试试。如果觉得有用,赏个 Star 就是最大的鼓励。
更多推荐

所有评论(0)