【SpringCloud】01-远程调用

1. RestTemplate

    1. 注册Bean
java 复制代码
@SpringBootApplication
public class CartServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(CartServiceApplication.class, args);
        System.out.println("cart启动成功");
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
    1. 在代码中引入Bean
java 复制代码
// 推荐使用构造函数注入, 使用lombook的@RequiredArgsConstructor进行注入
    private final RestTemplate restTemplate;
    1. 远程调用
java 复制代码
// 1.获取商品id
        Set<Long> itemIds = vos.stream().map(CartVO::getItemId).collect(Collectors.toSet());
        // 2.查询商品
        ResponseEntity<List<ItemDTO>> response = restTemplate.exchange(
                "http://localhost:8081/items?ids={ids}",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ItemDTO>>() {
                },
                Map.of("ids", CollUtil.join(itemIds, ","))
        );
        if (!response.getStatusCode().is2xxSuccessful()) {
            return;
        }
        List<ItemDTO> items = response.getBody();
相关推荐
颜进强4 分钟前
01 · NestJS 是什么:用途、解决什么问题、与热门框架对比
前端·后端·ai编程
isfox11 分钟前
MapReduce Shuffle 深度解析:数据从 Map 到 Reduce 的完整旅程
后端
颜进强19 分钟前
04 · NestJS 依赖注入:你在 `@Module` 写的 providers,和构造参数里那个类型,是怎么"对上"的?
前端·后端·ai编程
vx-程序开发28 分钟前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
谁在黄金彼岸30 分钟前
Ollama-使用速查
后端
ServBay31 分钟前
Jev是什么?哑巴模型居然全网爆火
后端·aigc·ai编程
烈风逍遥1 小时前
第六篇:RAG 知识库构建与检索全链路
前端·人工智能·后端
花椒技术1 小时前
Agent 沙箱怎么接入生产?花椒的选型、持久化与执行协议实践
人工智能·后端·agent
独泪了无痕1 小时前
Hutool之ArrayUtil:解锁数组操作的新境界
后端
杨运交1 小时前
[074][示例]基于Redisson的分布式锁在定时任务中的实践与异常模拟
java·后端·spring