轮播接口

本次目标:

1.学习如何看接口文档(技术!)
2.查询图片接口(基础微服务)
3.轮播接口(商品微服务)
4.商品详情接口(商品微服务)

1、启动虚拟机
su

docker ps -a 

docker start id
2、打开portainer (admin/12345678)

http://192.168.133.129:9010/

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XYSffgcx-1572791223613)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572782928860.png)]

3、在containers中启动mysql

mysql -uroot -p123456

4、建库,导入脚本

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1MubGQly-1572791223614)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572674664514.png)]

dm-common 工具工程(任何模块去调的公共组件)

dm-base 基础微服

各种功能模块调用,如:

item-provider —> item-consumer

user-provider —> user-consumer

order-provider —> order-consumer

itemName 节目名称

电商:用户量极大,高并发,多线程!性能一定要高!

redis,es,springcloud,springboot…

4.mavende 本地构建

素材:common工具工程
步骤1: idea直接导入dm-common
步骤2:Lifecycle install

RedisUtils类 作用:CIUD

//redis分布式上锁
public boolean lock(String key) {
boolean flag = false;
try {
    String lockKey = generateLockKey(key);
    flag = setnx(lockKey, "lock");
    if (flag) {
        System.out.println(expire(lockKey, Constants.Redis_Expire.DEFAULT_EXPIRE));
    }
    return flag;
} catch (Exception e) {
    e.printStackTrace();
}
return flag;
}

//解锁
public void unlock(String key) {
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    //设置序列化Value的实例化对象
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    String lockKey = generateLockKey(key);
    RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
    connection.del(lockKey.getBytes());
    connection.close();
}
4.1、创建注册中心eureka

步骤1:创建工程dm-eureka-server

启动eureka,http://localhost:7776查看心跳

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HVa4Zom2-1572791223615)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572783972371.png)]

docker访问的两种方式
(1.Nginx反向代理
(2.端口映射

创建hosts映射(访问域名就可以访问ip)

在C:\Windows\System32\drivers\etc\hosts修改

修改两个提供者的映射地址(dm-base-provider和dm-item-provider)

192.168.133.129 base2.local.com

192.168.133.129 item2.local.com

4.2、创建基础微服务

步骤2:创建工程dm-base-provider

然后修改【基础微服务】中yml文件中的url

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q9tenziI-1572791223616)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572678582456.png)]

分析数据库:需要单个参数(tartetId , type , category)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-T6G23Fhf-1572791223617)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572680967421.png)]

5、查询图片接口

步骤3:创建基础服务dm-base-provider
===RestDmImageService.java
思路1:
接口路径:/queryDmImageList

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6v5AJyLt-1572791223618)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572785179095.png)]

==入口
targetId:关联表ID (dm_item中item_id)
type :图片类型(0:无类型 1:轮播图 2:海报图)
category:图片分类(0:用户头像 1:商品图片)
==出口
List集合

//轮播图
@RequestMapping(value = "/queryDmImageList", method = RequestMethod.POST)
public List<DmImage> queryDmImageList(@RequestParam("targetId") Long targetId,
                                      @RequestParam("type") Integer type,
                                      @RequestParam("category") Integer category) throws Exception {
    Map<String, Object> imageParam = new HashMap<String, Object>();
    imageParam.put("targetId", targetId);
    imageParam.put("type", type);
    imageParam.put("category", category);
    List<DmImage> dmImageList = dmImageMapper.getDmImageListByMap(imageParam);
    return dmImageList;
}
5、测试查询图片接口的URL:

http://localhost:7002/queryDmImageList

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xan1h664-1572791223619)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572680185763.png)]

在postman测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-baTygrHP-1572791223619)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572784899355.png)]

“imgUrl”:null 此时不合理,解决:

//轮播图
return setDefaultImageList(dmImageList);
//设置图片为空时默认显示图片
public List<DmImage> setDefaultImageList(List<DmImage> dmImageList) {

    for(DmImage dmImage:dmImageList){
        if (EmptyUtils.isEmpty(dmImage.getImgUrl())) {
            dmImage.setImgUrl(Constants.DEFAULT_CAROUSEL);
        }
    }
    return dmImageList;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jEYG1noe-1572791223619)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572785324198.png)]

mybatis自动生成插件(增删改查)

步骤4:dm-item-provider/application.yml
修改url: jdbc:mysql://item2.local.com:3306/xxxx

启动dm-item-provider

6、查询首页轮播图接口

思路1:查询 "首页.md"文档,查询入口出口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g0ZyG0Oz-1572791223621)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572682843440.png)]

思路2:dm-item-consumer消费者创建【接口,实现类】

思路3:轮播涉及到的商品表(dm-item中isBanner字段(是否推荐(0:默认 1:热推)))
思路4:同一个消费者调用不同的生产者(通过Feign调用公共服务模块中的不同生产者接口)
思路5:组装VO(重点学习!)

①service/HomeService

public interface HomeService {
    //查询首页轮播图
    public List<HotItemVo> queryanner() throws Exception;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5c8GP8mQ-1572791223622)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572684273356.png)]
要求写四个,所以实体类字段为四个:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oEmtmVtW-1572791223622)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572683568377.png)]

创建HomeServiceImpl实现类:

先在工具类找【查询所有商品】

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SC8I4ZMd-1572791223623)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572684545508.png)]

然后在mapper.xml查看

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MBjVb0XJ-1572791223624)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572684843030.png)]

②Service/impl/HomeServiceImpl

@Component
public class HomeServiceImpl implements HomeService {
    @Autowired
    private RestDmItemClient restDmItemClient;
    @Autowired
    private RestDmImageClient restDmImageClient;

    @Override
    public List<HotItemVo> queryBanner() throws Exception {
        //查询轮播图前5个
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isBanner", 1);
        param.put("beginPos", 0);
        param.put("pageSize", 5);
        List<DmItem> dmItemList = restDmItemClient.getDmItemListByMap(param);
        //组装接口返回数据(商品数目显示后进行校验)
        List<HotItemVo> hotItemVoList = new ArrayList<HotItemVo>();
        if (EmptyUtils.isEmpty(dmItemList)) {
            return null;
        }
        for (DmItem dmItem : dmItemList) {
            HotItemVo hotItemVo = new HotItemVo();
            BeanUtils.copyProperties(dmItem, hotItemVo);//copyProperties【jdk1.8】
	    //把前面的对象存到hotItemVo里,然后调用查询图片的方法
            //查询图片信息
			//查询图片接口queryDmImageList被再次调用,因为轮播接口调用公有接口(查询图片接口,此接口成为feign组件,并且可以实现默认图片如果为空的小算法),所以用BeanUtils组装好后得到dmImageList,将dmImageList放到dmImageList.get(0).getImgUrl()里
            List<DmImage> dmImageList = restDmImageClient.queryDmImageList(dmItem.getId(),
                    Constants.Image.ImageType.carousel,
                    Constants.Image.ImageCategory.item);
            //组装图片信息
            //setImgUrl有可能为空,所以要另外的feign组件为默认图片
            hotItemVo.setImgUrl(EmptyUtils.isEmpty(dmImageList) ? null : dmImageList.get(0).getImgUrl());
            hotItemVoList.add(hotItemVo);
        }
        //所以~Vo为~List对象
        return hotItemVoList;
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kfdoL2j8-1572791223624)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572686400126.png)]

③controller/HomeController

@RestController
@RequestMapping("api/p/index")
public class HomeController {
    @Resource
    private HomeService homeService;

    @RequestMapping(value = "/queryBanner", method = RequestMethod.POST)
    public Dto<HotItemVo> queryBanner() throws Exception {
        List<HotItemVo> hotItemVoList = homeService.queryBanner();
        return DtoUtil.returnDataSuccess(hotItemVoList);
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KiOATcHq-1572791223625)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572698328316.png)]

完成,启动dm-item-consumer

**分析:**Dto与前端交互,Dto相当于壳子

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HJIwarZc-1572791223626)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572698483267.png)]

在DtoUtil类中,如果获得数据成功,data属性代表hotItemVoList集合

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7145ECpH-1572791223626)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572698744795.png)]

修改dm-item-provider的返回值

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g792BvtJ-1572791223627)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572790166467.png)]

总结:

④启动服务列表:
(1)dm-common本地构建(7个)
(2)启动注册中心dm-eureka-server
(3)启动基础微服务dm-base-provider
(4)启动商品生产者dm-item-provider
(5)启动商品消费者dm-item-consumer

⑥postman测试URL

http://localhost:7201/api/p/index/queryBanner

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q9Zlo3TE-1572791223628)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\1572789597509.png)]

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐