SpringCould集成OpenFegin步骤

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、查询是否能够调用成功。

http://localhost:8052/order/order/products

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值