在orderservice子工程中
<!--feign的远程--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
启动类加上这个注解
@EnableFeignClients //自动装配的开关
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
@EnableFeignClients //自动装配的开关
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}}
在orderservice中华编写接口加上调用服务的名称@FeignClient(“userservice”) 调用userservice
@FeignClient("userservice")
public interface UserClient {@GetMapping("/user/{id}")User findById(@PathVariable("id") Long id);}
@Service
public class OrderService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate UserClient userClient;public Order queryOrderById(Long orderId) {// 1.查询订单Order order = orderMapper.findById(orderId);User user= userClient.findById(order.getUserId());order.setUser(user);System.out.println(user);// 4.返回return order;}
}