7.springCloud 目前存在的问题

2020-04-28  本文已影响0人  呆叔么么

一.存在的问题

1.代码耦合非常高

order服务

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
...
    @Autowired
    private ProductClient productClient;

    @GetMapping("/contact/{goodsId}")
    public ServerResponse contact(@PathVariable("goodsId") Long goodsId) {
        GoodsInfo goodsInfo = productClient.search(goodsId);
        return ServerResponse.createBySuccess(goodsInfo);
    }
...
}

product服务

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
...
    @GetMapping("/search/{goodsId}")
    public GoodsInfo search(@PathVariable("goodsId") Long goodsId){
        GoodsInfo goodsInfo = goodsInfoService.findByGoodsId(goodsId);
        if(goodsInfo == null){
            throw new ProductException(ExceptionCodeEnum.PRODUCT_NOT_EXIT);
        }
        return goodsInfo;
    }
....
}

他们都有一个共同的实体类GoodsInfo ,而 GoodsInfo属于商品服务的。

2.可维护性差

product的访问接口,都定义在order服务中,可维护性非常的差

/**
 * @Author:LovingLiu
 * @Description: FeignClient客户端
 * @Date:Created in 2019-11-28
 */
@FeignClient(name = "PRODUCT")
@RequestMapping("/product")
public interface ProductClient {
    @GetMapping("/search/{goodsId}")
    GoodsInfo search(@PathVariable("goodsId") Long goodsId);
}

2.项目改进

1.模块化改造

image.png
1.1 client

存放的是对外暴露的接口

@FeignClient(name = "product", path = "/product")
public interface ProductClient {
    @PostMapping("/getByIdList")
    List<GoodsInfoOutPut> ListByGoodsIdList(@RequestBody  List<Long> goodsInfoIdList);

    @PostMapping("/decreaseStock")
    Integer decreaseStock (@RequestBody List<DecreaseStockInput> decreaseStockInputList);
}

1.2 common

存放对外使用的公共类
DecreaseStockInput 减库存时由order服务传入

@Data
public class DecreaseStockInput {
    private Long goodsId;
    private Integer goodsStock;
}

GoodsInfoOutPut 商品product传出

@Data
public class GoodsInfoOutPut {

    /**
     * 关联商品id
     *
     * @mbg.generated
     */
    private Long goodsId;

    /**
     * 下单时商品的名称(订单快照)
     *
     * @mbg.generated
     */
    private String goodsName;

    /**
     * 下单时商品的主图(订单快照)
     *
     * @mbg.generated
     */
    private String goodsCoverImg;

    /**
     * 下单时商品的价格(订单快照)
     *
     * @mbg.generated
     */
    private Integer sellingPrice;


    /**
     * 商品描述
     *
     * @mbg.generated
     */
    private String goodsIntro;
}

1.2 server

存放的是该服务的业务逻辑 controller service dao
如:controller

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
    private GoodsInfoService goodsInfoService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @GetMapping("/list")
    public ServerResponse<CommonPage<GoodsInfo>> list(
            @RequestParam(value = "pageNum",defaultValue = "1")
                    Integer pageNum,

            @RequestParam(value = "pageSize",defaultValue = "10")
                    Integer pageSize,

            @RequestParam(value = "orderBy",defaultValue = "create_time")
                    String orderBy,

            @RequestParam(value = "orderType",defaultValue = "desc")
                    String orderType,

            @RequestParam(value = "search",required = false)
                    String keyword){
        List<GoodsInfo> goodsInfoList = goodsInfoService.listByUser(pageNum,pageSize,orderBy,orderType,keyword);
        return ServerResponse.createBySuccess(CommonPage.restPage(goodsInfoList));
    }

    @GetMapping("/category")
    public ServerResponse<List<GoodsCategoryVO>> category(){
        List<GoodsInfo> goodsInfoList = goodsInfoService.listByGoodsSellStatus(CommonCodeEnum.PRODUCT_STATUS_ON_SELL.getCode());
        List<Long> categoryIdList = goodsInfoList.stream().map(e -> e.getGoodsCategoryId()).collect(Collectors.toList());

        List<GoodsCategory> goodsCategoryList = goodsCategoryService.findInIdListAndDeletedStatus(categoryIdList,CommonCodeEnum.PRODUCT_CATEGORY_NO_DELETED.getCode());

        List<GoodsCategoryVO> goodsCategoryVOList = Lists.newArrayList();

        for (GoodsCategory goodsCategory: goodsCategoryList) {
            Long categoryId = goodsCategory.getCategoryId();
            List<GoodsInfo> categoryGoodsInfoList = new ArrayList<>();
            for(GoodsInfo goodsInfo : goodsInfoList){
                if(categoryId == goodsInfo.getGoodsCategoryId()){
                    categoryGoodsInfoList.add(goodsInfo);
                }
            }
            GoodsCategoryVO goodsCategoryVO = new GoodsCategoryVO();
            BeanUtils.copyProperties(goodsCategory, goodsCategoryVO);
            goodsCategoryVO.setGoodsInfoList(categoryGoodsInfoList);

            goodsCategoryVOList.add(goodsCategoryVO);
        }

        return ServerResponse.createBySuccess(goodsCategoryVOList);
    }

    @GetMapping("/search/{goodsId}")
    public GoodsInfo search(@PathVariable("goodsId") Long goodsId){
        GoodsInfo goodsInfo = goodsInfoService.findByGoodsId(goodsId);
        if(goodsInfo == null){
            throw new ProductException(ExceptionCodeEnum.PRODUCT_NOT_EXIT);
        }
        return goodsInfo;
    }

    @PostMapping("/getByIdList")
    public List<GoodsInfoOutPut> ListByGoodsIdList(@RequestBody  List<Long> goodsInfoIdList) {
        List<GoodsInfo> goodsInfoList = goodsInfoService.ListByGoodsIdList(goodsInfoIdList);
        List<GoodsInfoOutPut>  goodsInfoOutPutList = goodsInfoList.stream().map(e -> {
            GoodsInfoOutPut goodsInfoOutPut = new GoodsInfoOutPut();
            goodsInfoOutPut.setGoodsId(e.getGoodsId());
            goodsInfoOutPut.setGoodsName(e.getGoodsName());
            goodsInfoOutPut.setGoodsCoverImg(e.getGoodsCoverImg());
            goodsInfoOutPut.setGoodsIntro(e.getGoodsIntro());
            goodsInfoOutPut.setSellingPrice(e.getSellingPrice());
            return goodsInfoOutPut;
        }).collect(Collectors.toList());

        return goodsInfoOutPutList;
    }
    @PostMapping("/decreaseStock")
    public Integer decreaseStock (@RequestBody List<DecreaseStockInput> decreaseStockInputList) {
        return goodsInfoService.decreaseStock(decreaseStockInputList);
    }
}

上一篇 下一篇

猜你喜欢

热点阅读