【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();
相关推荐
Java牛马5 小时前
SpringBoot Starter 依赖相关总结
java·spring·springboot·starter·自动装配·依赖
吴声子夜歌5 小时前
Java面试——Spring Cloud原理及应用(二)
java·spring cloud·面试
SomeB1oody5 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
东小西5 小时前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring
Profile排查笔记6 小时前
指纹浏览器哪个好?从 Profile、代理、权限和自动化能力判断是否适合
前端·人工智能·后端·自动化
用户938515635076 小时前
用 AI 结对编程从 0 搭一个"单词后台管理系统":Next.js + Supabase + Drizzle + shadcn/ui 全记录
后端·postgresql·next.js
爱学习的小邓同学7 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang
小灰灰搞电子8 小时前
Rust+Slint 实现的“DNA双螺旋”加载动画源码分享
后端·rust·slint·加载动画
面向Google编程8 小时前
向量库不再囤数据:Milvus 3.0 零拷贝直读数据湖
后端
IT_陈寒9 小时前
Redis内存警告竟是因为这个不起眼的配置项
前端·人工智能·后端