微服务之间openfeign相互调用
- 一、引入依赖
- 二、在启动类上配置扫描feign 包这个重要
- 三、服务调用 例子
一、引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
二、在启动类上配置扫描feign 包这个重要
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages = "com.yang.yimall.user.feign") //fegin包扫描
public class YimallUserApplication {
public static void main(String[] args) {
SpringApplication.run(YimallUserApplication.class, args);
}
}
三、服务调用 例子
A服务调用B服务 在A新建feign包,在B服务controller然后写上接口
B服务
@RestController
@AllArgsConstructor
@RequestMapping("/goods/manager/productCategory")
public class ProductCategoryController extends AbstractCrudController {
private final ProductCategoryService productCategoryService;
@RequestMapping("/listWithTree")
public Ret<List<ProductCategory>> listWithTree() {
List<ProductCategory> categoryEntities = productCategoryService.listWithTree();
return Ret.success("", categoryEntities);
}
}
A服务调用B服务接口
@FeignClient(value = "yimall-product")
public interface ProductFeignService {
@RequestMapping("/goods/manager/productCategory/listWithTree")
Ret<List<ProductCategory>> listWithTree();
}