07.http客户端Feign

07.http客户端Feign,第1张

07.http客户端Feign

Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign

1.引入依赖
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
2.在order-service的启动类添加@EnableFeignClients注解开启Feign的功能:
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
3.编写feign客户端
@FeignClient("userservice")
public interface UserClient {
    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}
4.用feign客户端代替RestTemplate
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2.利用Feign远程调用
        User user = userClient.findById(order.getUserId());
        //3.封装User到Order
        order.setUser(user);
        // 4.返回
        return order;
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5691064.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存