Feign 远程调用:调用的是对方项目的 Controller,不是 Service
目录一、核心结论二、举完整例子1提供者服务user-serviceController对外入口2消费者 Feign 接口对齐 Controller3消费者业务 Service 调用 Feign三、关键区分四、常见误区补充如果是 Dubbo 就不一样一、核心结论服务提供方被调用方对外暴露接口写在Controller接收 HTTP 请求 Service 只是内部业务实现不对外暴露网络接口。服务消费方调用方Feign 这边Feign 定义的接口签名必须和对方 Controller 完全对应模拟 HTTP 请求去访问对方 Controller。Service 只存在于各自服务内部跨服务不能直接调用 Service。二、举完整例子1提供者服务user-serviceController对外入口RestController RequestMapping(/user) public class UserController { Autowired private UserService userService; GetMapping(/get/{id}) public User getById(PathVariable Long id) { // 内部调用自己的Service return userService.getById(id); } }2消费者 Feign 接口对齐 Controller// Feign接口 远程Controller的镜像 FeignClient(name user-service) public interface UserFeignClient { // 路径、请求方式、参数、注解 完全和对方Controller一致 GetMapping(/user/get/{id}) User getUser(PathVariable Long id); }3消费者业务 Service 调用 FeignService public class OrderService { Autowired private UserFeignClient userFeignClient; public void createOrder(Long userId) { // 通过Feign发起HTTP远程访问 user-service 的 UserController User user userFeignClient.getUser(userId); } }三、关键区分Controller网络出入口处理 HTTP跨服务通信唯一入口Feign 实际访问的目标。Service本地业务层仅本项目内部使用没有 HTTP 能力无法远程访问。Feign 本质是 HTTP 客户端走网络请求只能访问 HTTP 接口Controller。四、常见误区❌ 错误Feign 对应对方 Service Service 没有请求路径、没有 GetMapping无法接收网络调用✅ 正确Feign 是远程 Controller 的镜像接口用来拼装 HTTP 请求。补充如果是 Dubbo 就不一样Dubbo 走 RPC直接暴露 Service 接口远程调用 Feign 基于 OpenFeign HTTP是 REST 风格只能对接 Controller。