Feign是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign
1.引入依赖2.在order-service的启动类添加@EnableFeignClients注解开启Feign的功能:org.springframework.cloud spring-cloud-starter-openfeign
@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; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)