短链接day-06
http请求端口当用户端需要请求一些有关短链接管理的接口时我们不能让他直接请求写在8002里面的方法而是需要用http请求到8001做一个中台请求。定义service指向http://127.0.0.1:8001package com.nageoffer.shortlink.admin.remote.dto; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.TypeReference; import com.baomidou.mybatisplus.core.metadata.IPage; import com.nageoffer.shortlink.admin.convention.result.Result; import com.nageoffer.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO; import com.nageoffer.shortlink.admin.remote.dto.req.ShortLinkPageReqDTO; import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO; import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; /** * 短链接中台远程调用服务 */ Service public interface ShortLinkRemoteService { /** * 创建短链接 * param shortLinkCreateReqDTO 创建短链接请求参数 * return 创建响应 */ default ResultShortLinkCreateRespDTO createShortLink(ShortLinkCreateReqDTO shortLinkCreateReqDTO){ String post HttpUtil.post(http://127.0.0.1:8001/api/short-link/v1/create, JSON.toJSONString(shortLinkCreateReqDTO)); return JSON.parseObject(post, new TypeReference(){}); }; /** * 分页查找短链接 * param shortLinkPageReqDTO * return */ default ResultIPageShortLinkPageRespDTO pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO){ MapString, Object requestMap new HashMap(); //第一步创建查询条件 requestMap.put(gid, shortLinkPageReqDTO.getGid()); requestMap.put(current, shortLinkPageReqDTO.getCurrent()); requestMap.put(size, shortLinkPageReqDTO.getSize()); //第二步http发送请求到中台获取结果 String resultPageStr HttpUtil.get(http://127.0.0.1:8001/api/short-link/v1/page, requestMap); //第三步将结果转换成Result return JSON.parseObject(resultPageStr, new TypeReference(){}); } }接着用controller调用即可。然后还要设置yaml文件的url路径url: jdbc:shardingsphere:classpath:shardingsphere-config-${database.env:dev}.yaml在这里面增加了一个database.env:dev,意思是可以设置连接dev中的数据库地址和密码还是prod里的默认是dev里的如何配置统计分组下短链接总数接口Override public ListShortLinkCountQueryRespDTO listGroupShortLinkCount(ListString requestParam) { QueryWrapperShortLinkDO queryWrapper Wrappers.query(new ShortLinkDO()) .select(gid as gid, count(*) as shortLinkCount) .in(gid, requestParam) .eq(enable_status, 0) .groupBy(gid); ListMapString, Object shortLinkDOList baseMapper.selectMaps(queryWrapper); return BeanUtil.copyToList(shortLinkDOList, ShortLinkCountQueryRespDTO.class); }接着将这个方法添加到之前的短链接分组集合查询中增加数量显示。Override public ListShortLinkGroupRespDTO listGroups() { LambdaQueryWrapperGroupDO queryWrapper Wrappers.lambdaQuery(GroupDO.class) .eq(GroupDO::getDelFlag, 0) .eq(GroupDO::getUsername, UserContext.getUsername()) .orderByDesc(GroupDO::getSortOrder, GroupDO::getUpdateTime); ListGroupDO groupDOList baseMapper.selectList(queryWrapper); //通过steam流获取每一个group的gid然后用http请求拿到短链接数量 ResultListShortLinkCountQueryRespDTO listResult shortLinkRemoteService .listGroupShortLinkCount(groupDOList.stream().map(GroupDO::getGid).toList()); ListShortLinkGroupRespDTO shortLinkGroupRespDTOList BeanUtil.copyToList(groupDOList, ShortLinkGroupRespDTO.class); //遍历分组把「短链接数量」填进去 shortLinkGroupRespDTOList.forEach(each - { OptionalShortLinkCountQueryRespDTO first listResult.getData().stream() //筛选出gid相同的 .filter(item - Objects.equals(item.getGid(), each.getGid())) .findFirst(); first.ifPresent(item - each.setShortLinkCount(first.get().getShortLinkCount())); }); return shortLinkGroupRespDTOList; }在用户注册的方法里也插入userRegisterCachePenetrationBloomFilter.add(userRegisterReqDTO.getUsername()); UserInfoDTO UserInfoBuild UserInfoDTO.builder() .username(userRegisterReqDTO.getUsername()) .build(); UserContext.setUser(UserInfoBuild); groupService.saveGroup(默认分组);在注册的时候将username存入线程中使saveGroup能获取到username并创建默认分组新增短链接第一步查询确认短链接是否存在第二步创建修改do第三步确认是否有修改gid如果修改则删除原来的并更新一个新的分片路径指向新的短链接数据。Transactional(rollbackFor Exception.class) Override public void updateShortLink(ShortLinkUpdateReqDTO requestParam) { //第一步查询确认是否存在短链接 LambdaQueryWrapperShortLinkDO queryWrapper Wrappers.lambdaQuery(ShortLinkDO.class) .eq(ShortLinkDO::getGid, requestParam.getGid()) .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl()) .eq(ShortLinkDO::getDelFlag, 0) .eq(ShortLinkDO::getEnableStatus, 0); ShortLinkDO hasShortLinkDO baseMapper.selectOne(queryWrapper); if (hasShortLinkDO null) { throw new ClientException(短链接记录不存在); } //第二步创建DO ShortLinkDO shortLinkDO ShortLinkDO.builder() .domain(hasShortLinkDO.getDomain()) .shortUri(hasShortLinkDO.getShortUri()) .clickNum(hasShortLinkDO.getClickNum()) .favicon(hasShortLinkDO.getFavicon()) .createdType(hasShortLinkDO.getCreatedType()) .gid(requestParam.getGid()) .originUrl(requestParam.getOriginUrl()) .describe(requestParam.getDescribe()) .validDateType(requestParam.getValidDateType()) .validDate(requestParam.getValidDate()) .build(); //第三步更新,确认分组一致如果gid也修改了则删掉原本的短链接再生成新的短链接实现分片 // TODO: 这里需要新建一个原始短链接用于查询新增短链接用于修改短链接。 if (Objects.equals(hasShortLinkDO.getGid(), requestParam.getGid())) { LambdaUpdateWrapperShortLinkDO updateWrapper Wrappers.lambdaUpdate(ShortLinkDO.class) .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl()) .eq(ShortLinkDO::getGid, requestParam.getGid()) .eq(ShortLinkDO::getDelFlag, 0) .eq(ShortLinkDO::getEnableStatus, 0) .set(Objects.equals(requestParam.getValidDateType(), VailDateTypeEnum.PERMANENT.getType()), ShortLinkDO::getValidDate, null); baseMapper.update(shortLinkDO, updateWrapper); } else { LambdaUpdateWrapperShortLinkDO updateWrapper Wrappers.lambdaUpdate(ShortLinkDO.class) .eq(ShortLinkDO::getFullShortUrl, requestParam.getFullShortUrl()) .eq(ShortLinkDO::getGid, hasShortLinkDO.getGid()) .eq(ShortLinkDO::getDelFlag, 0) .eq(ShortLinkDO::getEnableStatus, 0); baseMapper.delete(updateWrapper); baseMapper.insert(shortLinkDO); } }之后在admin层用http请求写入修改的路径和方法。