You need to enable JavaScript to run this app. 如何使用Java部署SageMaker模型并创建可供Spring Boot调用的REST接口
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java部署SageMaker模型并创建可供Spring Boot调用的REST接口

整体实现流程

你已经完成了模型训练和SageMaker Model资源创建,接下来只需要完成端点部署、Spring Boot侧适配两步即可实现预测功能:

第一步:部署SageMaker在线预测端点

你可以通过SageMaker客户端API或者控制台完成部署,以下是API操作流程:

    1. 创建端点配置
      调用sageMakerClient.createEndpointConfig接口,配置参数包含:
    • 端点配置名称(自定义,后续创建端点需要用到)
    • 模型名称(填写你之前调用createModel时指定的模型名)
    • 在线预测实例规格(比如ml.t2.medium,根据你的模型体积和性能需求选择)
    • 实例数量(至少配置1个实例用于服务)
    1. 创建在线端点
      调用sageMakerClient.createEndpoint接口,传入自定义的端点名称、上一步创建的端点配置名称,等待5-15分钟,待端点状态变为InService即可调用。

你也可以直接在SageMaker控制台的「模型」模块,选中你训练好的模型,点击「部署」→「到端点」,按页面引导完成配置,可视化查看端点部署状态。
原训练成功模型参考截图:
训练成功模型截图

第二步:Spring Boot应用对接预测端点

SageMaker在线端点需要通过AWS官方的SageMaker Runtime SDK调用,你可以直接在业务代码中集成,也可以封装成自定义的REST接口供内部其他服务调用:

  1. 首先在pom.xml中引入AWS SDK依赖:
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>sagemakerruntime</artifactId>
    <version>2.20.0</version> <!-- 替换为你使用的SDK版本 -->
</dependency>
  1. 构造SageMaker Runtime调用工具类/业务方法:
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sagemakerruntime.SageMakerRuntimeClient;
import software.amazon.awssdk.services.sagemakerruntime.model.InvokeEndpointRequest;
import software.amazon.awssdk.services.sagemakerruntime.model.InvokeEndpointResponse;
import java.nio.charset.StandardCharsets;

public class SageMakerPredictor {
    // 替换为你自己的SageMaker端点名称、部署区域
    private static final String ENDPOINT_NAME = "your-endpoint-name";
    private static final Region REGION = Region.US_EAST_1;

    public String predict(String requestBody) {
        // 初始化SageMaker Runtime客户端,默认会读取环境变量、EC2实例角色等位置的凭证
        SageMakerRuntimeClient runtimeClient = SageMakerRuntimeClient.builder()
                .region(REGION)
                .credentialsProvider(DefaultCredentialsProvider.create())
                .build();

        // 构造调用请求,ContentType根据你模型接受的请求格式调整,比如application/json、text/csv等
        InvokeEndpointRequest endpointRequest = InvokeEndpointRequest.builder()
                .endpointName(ENDPOINT_NAME)
                .contentType("application/json")
                .body(SdkBytes.fromString(requestBody, StandardCharsets.UTF_8))
                .build();

        // 发起调用并解析响应
        InvokeEndpointResponse response = runtimeClient.invokeEndpoint(endpointRequest);
        return response.body().asUtf8String();
    }
}
  1. (可选)封装为自定义REST接口
    如果需要对外提供统一的REST预测接口,可以在Spring Boot中新增Controller层:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
public class PredictController {
    @Resource
    private SageMakerPredictor sageMakerPredictor;

    @PostMapping("/api/predict")
    public String predict(@RequestBody String predictRequest) {
        return sageMakerPredictor.predict(predictRequest);
    }
}
  1. 权限配置
    确保Spring Boot应用运行的环境(本地开发环境、EC2、ECS等)拥有sagemaker:InvokeEndpoint的IAM权限,否则会调用失败。本地开发可以通过配置~/.aws/credentials文件填写有权限的AK/SK,云环境可以直接绑定对应IAM角色。

内容的提问来源于stack exchange,提问作者codeworks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 04:54:04