OpenFeign 服务调用

OpenFeign 服务调用

  • 使用 nacos 仍然需要手写 http 请求
  • openFeign 简化手写

cart-service中,定义一个新的接口,编写Feign客户端:

package com.hmall.cart.client;

import com.hmall.cart.domain.dto.ItemDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@FeignClient("item-service")
public interface ItemClient {

    @GetMapping("/items")
    List<ItemDTO> queryItemByIds(@RequestParam("ids") Collection<Long> ids);
}

openFeign 连接池

openFeign 最佳实践

  • 效果很好, 大型服务可以使用
  • 结构复杂, 增加了工作量
  • 如果每个微服务是一个 Project 可以使用这种方式

  • 代码耦合度增加了
  • 如果微服务是通过 maven聚合实现的, 可以使用这种方法
demo

因为 cart 服务的包是 com.hmall.cart , 而 clientcom.hmall.api.client , 因此不会扫描到, 所以

openFeign 日志

总结

openfeign 网关

跨包