diff --git a/README.md b/README.md index d7f1609..3e35ecf 100644 --- a/README.md +++ b/README.md @@ -61,10 +61,14 @@ 批量生成短链API : {{host}}:{{ip}}/send 短链兑换真实地址API : {{host}}:{{ip}}/返回的短链 + 注意事项(原始链接要求), + 1. 前期不支持微信小程序内嵌h5页面 + 2. 跳转后请求地址为 http:://t.p.tuoz.net/短链 + #### 部署方式 by-short-server 目录下 mvn clean install 进行打包 上传到服务器 - 运行 ./script 下的 run-short-server.sh 脚本启动 + 运行 /home/www/ 下的 run-short-server.sh 脚本启动 diff --git a/short-server-common/src/main/java/com/by/constants/SymbolConstant.java b/short-server-common/src/main/java/com/by/constants/SymbolConstant.java index f76f2b4..a915adf 100644 --- a/short-server-common/src/main/java/com/by/constants/SymbolConstant.java +++ b/short-server-common/src/main/java/com/by/constants/SymbolConstant.java @@ -6,4 +6,10 @@ public class SymbolConstant { * 常用的分隔符 */ public static final String SPLIT_VERTICAL = "|"; + + + /** + * 加大写 + */ + public static final String BASE_CHAR_UPCASE_PLUS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } diff --git a/short-server-common/src/main/java/com/by/utils/ShortUrlUtil.java b/short-server-common/src/main/java/com/by/utils/ShortUrlUtil.java index df30c2b..4cb9e37 100644 --- a/short-server-common/src/main/java/com/by/utils/ShortUrlUtil.java +++ b/short-server-common/src/main/java/com/by/utils/ShortUrlUtil.java @@ -2,6 +2,7 @@ package com.by.utils; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.RandomUtil; +import com.by.constants.SymbolConstant; import java.util.*; @@ -15,6 +16,11 @@ public class ShortUrlUtil { */ private static final Integer BASE_LIMIT_LENGTH = 10; + /** + * 生成随机嘛 3位 + */ + private static final Integer BASE_SHORT_LIMIT_LENGTH = 2; + private ShortUrlUtil() { } @@ -23,7 +29,7 @@ public class ShortUrlUtil { * * @return 返回生成的多个短链映射 */ - public static Map makeShortUrl(List oringinShortUrlList) { + public static Map makeShortUrl(List oringinShortUrlList, Boolean tag) { if (CollectionUtil.isEmpty(oringinShortUrlList)) { return CollectionUtil.newHashMap(); @@ -32,7 +38,18 @@ public class ShortUrlUtil { oringinShortUrlList.forEach( each -> { - resultMap.put(RandomUtil.randomString(BASE_LIMIT_LENGTH), each); + if (tag){ + resultMap.put(RandomUtil.randomString( + SymbolConstant.BASE_CHAR_UPCASE_PLUS + RandomUtil.BASE_CHAR_NUMBER, BASE_SHORT_LIMIT_LENGTH), + each + ); + }else { + resultMap.put(RandomUtil.randomString( + SymbolConstant.BASE_CHAR_UPCASE_PLUS + RandomUtil.BASE_CHAR_NUMBER, BASE_LIMIT_LENGTH), + each + ); + } + } ); diff --git a/short-server-service/pom.xml b/short-server-service/pom.xml index 369fc6d..e9c8d6b 100644 --- a/short-server-service/pom.xml +++ b/short-server-service/pom.xml @@ -15,6 +15,9 @@ 8 8 + UTF-8 + UTF-8 + UTF-8 diff --git a/short-server-service/src/main/java/com/by/api/ShortServerOpenApiController.java b/short-server-service/src/main/java/com/by/api/ShortServerOpenApiController.java index 71b7265..e82875e 100644 --- a/short-server-service/src/main/java/com/by/api/ShortServerOpenApiController.java +++ b/short-server-service/src/main/java/com/by/api/ShortServerOpenApiController.java @@ -19,6 +19,7 @@ import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.view.RedirectView; import javax.servlet.http.HttpServletRequest; import java.util.List; @@ -34,7 +35,7 @@ import static com.by.api.constants.RequestInfoConstant.CUSTOMER_IP_ADDR_KEY; public class ShortServerOpenApiController { // TODO: 2021/4/14 0014 添加接口限流 - // TODO: 2021/4/14 0014 添加接口相关的验证 丰富VO层 加入 appid secret_key 及 时间戳验证 + // TODO: 2021/4/14 0014 添加接口相关的验证 丰富VO层 加入 appId secret_key 及 时间戳验证 private final ShortServerService shortServerService; @@ -48,24 +49,28 @@ public class ShortServerOpenApiController { @ApiOperation("批量生成短链接") @PostMapping(value = "/trans") @ResponseBody - public CommonResponse parseContentToShortChain(@Validated @RequestBody ShortChainRequestVO shortChainRequestVO, HttpServletRequest request) { + public CommonResponse parseContentToShortChain(@Validated @RequestBody ShortChainRequestVO shortChainRequestVO, HttpServletRequest request) { log.info("=== [ShortServerOpenApiController|parseContentToShortChain, one request is coming, request vo is {} ] ===", shortChainRequestVO.toString()); ShortChainDTO shortChainDTO = ShortChainVOToDTOConvert.convertShortChainRequestVOToDTO(shortChainRequestVO); // 拿到远程请求客户端的Ip信息 String remoteAddr = request.getRemoteAddr(); - if (StringUtils.isNotBlank(remoteAddr)){ + if (StringUtils.isNotBlank(remoteAddr)) { ThreadLocalUtil.set(CUSTOMER_IP_ADDR_KEY, remoteAddr); - }else { + } else { ThreadLocalUtil.set(CUSTOMER_IP_ADDR_KEY, SystemConstant.DEFAULT_IP); } - shortChainDTO = shortServerService.handleOriginUrlsToShortUrls(shortChainDTO); + // 参数不存在的时候,给一个标识,来缩短短链 + if (CollectionUtil.isEmpty(shortChainRequestVO.getVariableList())){ + shortChainDTO = shortServerService.handleOriginUrlsToShortUrls(shortChainDTO, true); + } + List shortChainResult = shortChainDTO.getShortChainResult(); ShortChainResponseVO shortChainResponseVO = new ShortChainResponseVO(); - if (CollectionUtil.isNotEmpty(shortChainResult)){ + if (CollectionUtil.isNotEmpty(shortChainResult)) { shortChainResponseVO.setShortChainResult(shortChainResult); } @@ -74,19 +79,36 @@ public class ShortServerOpenApiController { @ApiOperation("短链接兑换长链接并进行") @GetMapping(value = "/s/{redeem}") - public ModelAndView redeemShortChainClick(@PathVariable("redeem") String redeem){ + @CrossOrigin + public ModelAndView redeemShortChainClick(@PathVariable("redeem") String redeem) { log.info("=== [ShortServerOpenApiController|redeemShortChainClick, one request is coming, request param is {} ] ===", redeem); ShortUrl shortUrl = shortServerService.handleOnceShortUrlToRedirectOriginUrlAndRecord(redeem); - String originUrl = ""; - if (!Objects.isNull(shortUrl)){ + String originUrl = ""; + if (!Objects.isNull(shortUrl)) { + // 异步进行更新数据库中的点击记录及推送给之前的调用方(可以走消息) - shortChainTask.doRunTask(shortUrl); - originUrl = StringUtils.substringBefore(shortUrl.getOriginUrl(), SymbolConstant.SPLIT_VERTICAL); + originUrl = shortUrl.getOriginUrl(); + + // 增加如果是不带参数的url就不调用task进行分发 + if (StringUtils.contains(originUrl, SymbolConstant.SPLIT_VERTICAL)){ + shortChainTask.doRunTask(shortUrl); + } + originUrl = StringUtils.substringBefore(originUrl, SymbolConstant.SPLIT_VERTICAL); + } return new ModelAndView("redirect:" + originUrl); } + @ApiOperation("用于测试转发") + @GetMapping(value = "/t/{redeem}") + @CrossOrigin + @Deprecated + public ModelAndView testRedirect(@PathVariable("redeem") String redeem) { + System.out.println(redeem); + return new ModelAndView(new RedirectView("http://www.baidu.com")); + } + } diff --git a/short-server-service/src/main/java/com/by/api/convert/ShortChainVOToDTOConvert.java b/short-server-service/src/main/java/com/by/api/convert/ShortChainVOToDTOConvert.java index 0f6c6ee..e61085a 100644 --- a/short-server-service/src/main/java/com/by/api/convert/ShortChainVOToDTOConvert.java +++ b/short-server-service/src/main/java/com/by/api/convert/ShortChainVOToDTOConvert.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil; import com.by.api.vo.ShortChainRequestVO; import com.by.constants.SymbolConstant; import com.by.dto.ShortChainDTO; +import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.HashSet; @@ -40,6 +41,11 @@ public class ShortChainVOToDTOConvert { ); } + // 不带参数的短链支持 + if (CollectionUtil.isEmpty(shortChainRequestVO.getVariableList()) && StringUtils.isNotBlank(shortChainRequestVO.getBaseUrlAddr())){ + originsUrlSet.add(baseUrlAddr); + } + if (CollectionUtil.isNotEmpty(originsUrlSet)) { ShortChainDTO shortChainDTO = new ShortChainDTO(); shortChainDTO.setShortChainOrigins(new ArrayList<>(originsUrlSet)); diff --git a/short-server-service/src/main/java/com/by/service/ShortServerService.java b/short-server-service/src/main/java/com/by/service/ShortServerService.java index 6a449e9..586990e 100644 --- a/short-server-service/src/main/java/com/by/service/ShortServerService.java +++ b/short-server-service/src/main/java/com/by/service/ShortServerService.java @@ -13,9 +13,10 @@ public interface ShortServerService { * 原始链接处理成短链 * * @param shortChainDTO + * @param tag 简化短链标识 true 标识为使用短链标识 * @return */ - ShortChainDTO handleOriginUrlsToShortUrls(ShortChainDTO shortChainDTO); + ShortChainDTO handleOriginUrlsToShortUrls(ShortChainDTO shortChainDTO, Boolean tag); /** diff --git a/short-server-service/src/main/java/com/by/service/impl/ShortServerServiceImpl.java b/short-server-service/src/main/java/com/by/service/impl/ShortServerServiceImpl.java index ff72e22..4ddc4a2 100644 --- a/short-server-service/src/main/java/com/by/service/impl/ShortServerServiceImpl.java +++ b/short-server-service/src/main/java/com/by/service/impl/ShortServerServiceImpl.java @@ -40,10 +40,10 @@ public class ShortServerServiceImpl implements ShortServerService { @Override @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW) - public ShortChainDTO handleOriginUrlsToShortUrls(ShortChainDTO shortChainDTO) { + public ShortChainDTO handleOriginUrlsToShortUrls(ShortChainDTO shortChainDTO, Boolean tag) { // 1. 拼接请求调用短链生成器进行所有的短链请求 - Map makeShortUrlResult = ShortUrlUtil.makeShortUrl(shortChainDTO.getShortChainOrigins()); + Map makeShortUrlResult = ShortUrlUtil.makeShortUrl(shortChainDTO.getShortChainOrigins(), tag); // 2. 生成的短链请求批量入库 List shortUrls = new ArrayList<>(); diff --git a/short-server-service/src/main/java/com/by/task/ShortChainTask.java b/short-server-service/src/main/java/com/by/task/ShortChainTask.java index 0c3b08a..136d043 100644 --- a/short-server-service/src/main/java/com/by/task/ShortChainTask.java +++ b/short-server-service/src/main/java/com/by/task/ShortChainTask.java @@ -39,11 +39,11 @@ public class ShortChainTask { @Async(value = "shortChainTaskPool") public void doRunTask(ShortUrl shortUrl) { - Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); - log.info("====== [ task start running, task name is {} ] ======", "ABDownTask"); + Long startMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start running, task name is {} ] ======", "shortChainTask"); runTask(shortUrl); Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); - log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "ABDownTask", (endMilliSecond - satrtMilliSecond)); + log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "shortChainTask", (endMilliSecond - startMilliSecond)); } private void runTask(ShortUrl shortUrl) { @@ -65,18 +65,19 @@ public class ShortChainTask { ShortChainPointJsonDTO shortChainPointJsonDTO = new ShortChainPointJsonDTO(shortUrl.getShortUrl(), DBDefaultConstant.ONE_NUM_TAG); String jsonSendContent = JSON.toJSONString(shortChainPointJsonDTO); + log.info("=== [pre send msg, the pre json-msg is {} ] ====", jsonSendContent); // 失败重发请求3次 while (count <= RequestInfoConstant.RETRY_COUNT) { // 调用HTTP请求发送数据 HttpResponse httpResponse = sendReq(jsonSendContent, uploudPlatformUrl); - if (httpResponse.isOk() && httpResponse.body().contains("0")) { - log.info("========== [liehe request success, response is {} ] ==========", httpResponse.body()); + if (httpResponse.isOk() && httpResponse.body().contains("SUCCESS")) { + log.info("========== [shortChainTask update request success, response is {} ] ==========", httpResponse.body()); shortServerService.updateShortUrlToPlatformSendtatus(shortUrl.getId(), Boolean.TRUE); break; } else { count++; - log.error("========== [liehe request fail, response is {} ] ==========", httpResponse.body()); + log.error("========== [shortChainTask update request fail, response is {} ] ==========", httpResponse.body()); } } if (count > RequestInfoConstant.RETRY_COUNT) { diff --git a/short-server-service/src/main/resources/application-dev.yml b/short-server-service/src/main/resources/application-dev.yml index 3326e66..9f3669e 100644 --- a/short-server-service/src/main/resources/application-dev.yml +++ b/short-server-service/src/main/resources/application-dev.yml @@ -24,7 +24,7 @@ spring: test-while-idle: true test-on-borrow: false test-on-return: false -# # 检测连接是否有效 + # # 检测连接是否有效 validation-query: select 1 # 配置监控统计 webStatFilter: diff --git a/short-server-service/src/main/resources/application.yml b/short-server-service/src/main/resources/application.yml index f695fe5..da909b4 100644 --- a/short-server-service/src/main/resources/application.yml +++ b/short-server-service/src/main/resources/application.yml @@ -2,7 +2,7 @@ server: port: 6666 spring: profiles: - active: test + active: prod jackson: time-zone: GMT+8 @@ -39,4 +39,4 @@ short: # 远程调用地址 upload: platform: - url: '118.178.137.129:8000/api/tbSendSms/url/callback' \ No newline at end of file + url: 'http://118.178.137.129:8800/api/tbSendSms/url/callback' \ No newline at end of file