SpringBoot3 整合阿里云百炼大模型|实现普通对话+流式输出
一、前言
当下 AI 飞速迭代,行业早已印证一个真理:AI 取代不了人,但不用 AI 的人将会被用 AI 的人取代。AI 本质是提升效率的生产力工具,不会替代人类的思考、判断与创新,但会大幅拉开人与人之间的工作效率差距。
目前基于 Spring AI 快速接入大模型已经成为 Java 开发的主流方案。本文基于 SpringBoot3.x + Spring AI Alibaba 快速整合阿里云百炼(DashScope)大模型,实现普通同步对话 与 SSE 流式逐字输出 能力。同时解决新手最常见的 SocketTimeout 接口超时 问题,全程采用环境变量配置、零硬编码 Key,安全规范,可直接用于项目开发与生产环境,帮助开发者借助 AI 赋能业务、提升开发效率。
二、环境要求
-
SpringBoot 3.x
-
JDK 17+
-
Spring AI Alibaba 1.0.0.2 版本
-
阿里云百炼 DashScope API Key
三、项目依赖引入(Maven)
引入阿里百炼大模型核心依赖、Web 依赖、日志依赖及工具类依赖,排除默认日志框架,统一使用 log4j2。
xml
<dependencies>
<!-- Spring AI Alibaba 百炼大模型核心依赖 -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
<version>1.0.0.2</version>
</dependency>
<!-- Web 核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<version>3.4.0</version>
</dependency>
<!-- log4j2 日志框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>3.4.0</version>
</dependency>
<!-- 工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>
四、配置阿里云百炼 API Key(环境变量方式)
为了安全,禁止代码硬编码 Key,采用系统环境变量配置,适配 Windows / Linux 双环境。
4.1 Windows 环境配置
打开 CMD 执行如下命令,替换为自己的百炼 APIKey:
bash
setx DASHSCOPE_API_KEY "YOUR_DASHSCOPE_API_KEY"
配置完成后,必须重新打开 CMD 窗口,执行以下命令验证是否生效:
bash
echo %DASHSCOPE_API_KEY%
4.2 Linux 环境配置
将环境变量写入用户环境配置文件:
bash
echo "export DASHSCOPE_API_KEY='YOUR_DASHSCOPE_API_KEY'" >> ~/.bashrc
刷新环境变量生效:
bash
source ~/.bashrc
验证配置:
bash
echo $DASHSCOPE_API_KEY
五、application.yml 核心配置(解决超时报错)
默认超时时间极短,极易出现 SocketTimeoutException,此处手动拉长超时时间,彻底解决大模型响应超时问题。
yaml
spring:
application:
name: demo
ai:
dashscope:
api-key: ${DASHSCOPE_API_KEY}
chat:
client:
enabled: true
options:
timeout: 60s # 整体请求超时
connect-timeout: 10s # 连接超时
read-timeout: 60s # 读取响应超时
六、完整接口代码(普通对话 + 流式输出)
提供两种接口:
-
普通同步接口:全部生成完成后一次性返回
-
流式 SSE 接口:逐字实时推送,解决长时间响应超时问题
java
package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
@RequestMapping("/ai")
public class ChatController {
private static final Logger log = LoggerFactory.getLogger(ChatController.class);
private final ChatClient chatClient;
// 自动注入 ChatClient 构建器
public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
/**
* 普通同步对话接口
* 全部内容生成完毕后统一返回
*/
@GetMapping("/chat")
public String chat(String input) {
log.info("人工提问:{}", input);
String reply = this.chatClient.prompt()
.user(input)
.call()
.content();
log.info("大模型回复:{}", reply);
return reply;
}
/**
* 流式对话接口
* SSE 实时逐字输出,解决超时问题
*/
@GetMapping(value = "/chatStream", produces = "text/event-stream;charset=UTF-8")
public Flux<String> chatStream(String input) {
log.info("人工提问:{}", input);
// stream 流式调用,实时返回内容
return chatClient.prompt()
.user(input)
.stream()
.content();
}
}
七、核心知识点讲解
7.1 普通对话 VS 流式对话
-
call\(\):同步阻塞,等待大模型全部生成完成后统一返回,长文本极易超时 -
stream\(\):异步流式,边生成边推送,彻底规避超时异常
7.2 流式接口关键配置
produces = \&\#34;text/event\-stream;charset=UTF\-8\&\#34;
声明接口为 SSE 服务器推送事件,浏览器/前端可实时监听数据流,实现打字机效果。
7.3 超时问题解决方案
报错:java\.net\.SocketTimeoutException: timeout
原因:大模型生成文本耗时较长,默认超时时间过短。
解决:手动配置 timeout、read-timeout 60秒,配合流式接口双保险。
八、接口测试方式
启动项目后直接浏览器访问测试:
-
普通接口:
http://localhost:8080/ai/chat?input=你好,介绍一下SpringAI -
流式接口:
http://localhost:8080/ai/chatStream?input=你好,介绍一下SpringAI
流式接口可明显看到逐字实时输出,不再长时间空白等待。
九、总结
- 本文基于 SpringBoot3 + Spring AI Alibaba 快速接入百炼大模型,代码极简、开箱即用
- 采用环境变量配置 APIKey,安全规范,适合生产环境
- 同时实现同步/流式两种对话模式,适配不同业务场景
(注:文档部分内容可能由 AI 生成)