1、项目最基本的结构介绍,现在我要实现订单项目调用商品项目里面的方法。
mall-order 订单项目
mall-product 商品项目
2、mall-product中被调用的方法实现如下
/**
* 品牌
*
* @author hxx
* @email hxx@gmail.com
* @date 2023-04-12 16:49:21
*/
@RestController
@RequestMapping("product/brand")
public class BrandController {
/**
* 品牌查询
*/
@RequestMapping("/all")
public R queryAllBrand(){
BrandEntity entity = new BrandEntity();
entity.setName("苹果");
return R.ok().put("brands", entity);
}
}
3、mall-order 通过OpenFegin客户端调用 mall-product中的queryAllBrand方法。
3.1 引入相关的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--必须增加负载均衡包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
3.2 创建对应的Fegin接口
@FeignClient(name="mall-product")
public interface ProductService {
/**
* 需要访问的远程方法
* @return
*/
@GetMapping("/product/brand/all")
public R queryAllBrand();
}
3.3 启动类中开启远程调用
@EnableFeignClients(basePackages = "com.hxx.mall.order.rpc"
3.4 订单调用方法
/**
* 订单
*
* @author hxx
* @email hxx@gmail.com
* @date 2023-04-12 15:25:59
*/
@RestController
@RequestMapping("order/order")
public class OrderController {
@Autowired
ProductService productService;
@GetMapping("/products")
public R queryProduct() {
// OpenFegin 远程调用服务
return R.ok().put("products", productService.queryAllBrand());
}
}
4、我启动的时候报错了,主要原因是还是jar包冲突,去掉spring-cloud-starter-netflix-ribbon依赖,采用工具dependency analyzer(没有本插件的自行下载)排除掉这个包。
5、查询是否能够调用成功。