Przeglądaj źródła

bug修改 redis连接池配置修改,调试日志去除

张均强 4 lat temu
rodzic
commit
e51f4095fb

+ 7 - 6
common/fire-common/src/main/java/com/fire/common/redis/RedisOrderIdScript.java

@@ -8,6 +8,7 @@ import redis.clients.jedis.JedisCluster;
 import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import java.util.Arrays;
+
 import static com.fire.dto.enums.RedisKey.ORDER_DOWN;
 
 @Slf4j
@@ -22,10 +23,10 @@ public class RedisOrderIdScript {
      *初始化
      */
     @PostConstruct
-    public void initScript(){
+    public void initScript() {
         String slotKey = ORDER_DOWN.key();
         //LUA脚本
-                String SCRIPT = "local key = KEYS[1]" +
+        String SCRIPT = "local key = KEYS[1]" +
                 " local customerOrder = KEYS[2]" +
                 " local orderId = ARGV[1]" +
                 " local res = redis.pcall('HEXISTS', key, customerOrder)" +
@@ -35,10 +36,10 @@ public class RedisOrderIdScript {
                 " redis.pcall('HSET', key, customerOrder, orderId)" +
                 " return 1" +
                 " end";
-        if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)){
+        if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)) {
             //redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
             runCode = jedisCluster.scriptLoad(SCRIPT, slotKey);
-        }else{
+        } else {
             log.info("脚本已加载");
         }
     }
@@ -50,9 +51,9 @@ public class RedisOrderIdScript {
      * 3 OrderId 订单ID号
      * 返回 1 成功加入 0 下单号重复
      */
-    public String checkOrderId(String customerOrder,String OrderId) {
+    public String checkOrderId(String customerOrder, String OrderId) {
         String orderName = ORDER_DOWN.key();
-        Object res = jedisCluster.evalsha(runCode, Arrays.asList(orderName,customerOrder),Arrays.asList(OrderId));
+        Object res = jedisCluster.evalsha(runCode, Arrays.asList(orderName, orderName + customerOrder), Arrays.asList(OrderId));
         return res.toString();
     }
 }

+ 2 - 0
common/fire-common/src/main/java/com/fire/common/redis/config/RedisConfig.java

@@ -1,6 +1,7 @@
 package com.fire.common.redis.config;
 
 import lombok.extern.slf4j.Slf4j;
+import org.elasticsearch.common.inject.Singleton;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
@@ -24,6 +25,7 @@ public class RedisConfig {
     private String clusterNodes;
 
     @Bean
+    @Singleton
     public JedisCluster getJedisCluster() {
         Set<HostAndPort> nodes = new HashSet<>();
 

+ 6 - 1
common/fire-dto/src/main/java/com/fire/dto/CustomerInfo.java

@@ -5,8 +5,11 @@ import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
+import lombok.NoArgsConstructor;
+
 import java.math.BigDecimal;
 
 
@@ -14,10 +17,12 @@ import java.math.BigDecimal;
 @Data
 @TableName("customer_info")
 @Builder
+@NoArgsConstructor
+@AllArgsConstructor
 public class CustomerInfo {
 
     @ApiModelProperty(value = "客户ID")
-    @TableId(value = "customer_id",type = IdType.AUTO)
+    @TableId(value = "customer_id", type = IdType.AUTO)
     private Long customerId;
 
     @ApiModelProperty(value = "合作伙伴[中间人]ID")

+ 24 - 0
common/fire-dto/src/main/java/com/fire/dto/enums/RocketQueue.java

@@ -0,0 +1,24 @@
+package com.fire.dto.enums;
+
+public enum RocketQueue {
+
+    ORDER_QUEUE("order", "order队列");
+
+    private final String queue;
+
+    private final String desc;
+
+    RocketQueue(String queue, String desc) {
+        this.queue = queue;
+        this.desc = desc;
+    }
+
+    public String queue() {
+        return queue;
+    }
+
+    public String desc() {
+        return desc;
+    }
+
+}

+ 24 - 0
common/fire-dto/src/main/java/com/fire/dto/enums/RocketTopic.java

@@ -0,0 +1,24 @@
+package com.fire.dto.enums;
+
+public enum RocketTopic {
+
+    MAKE_ORDER_TOPIC("order:make", "下单的订单topic");
+
+    private final String topic;
+
+    private final String desc;
+
+    RocketTopic(String topic, String desc) {
+        this.topic = topic;
+        this.desc = desc;
+    }
+
+    public String topic() {
+        return topic;
+    }
+
+    public String desc() {
+        return desc;
+    }
+
+}

+ 4 - 0
common/fire-dto/src/main/java/com/fire/dto/enums/Status.java

@@ -24,6 +24,10 @@ public enum Status {
     OPERATOR_ERROR("13", "运营商类型错误"),
     OVER_TIME("14", "TIMESTAMP超过时限"),
     SECRET_KEY_ERROR("15", "SECRET_KEY校验错误"),
+    PARAM_LOSS("16", "请求参数缺失"),
+    HEADER_LOSS("17", "请求参数HEADER节点缺失"),
+    PHONE_ZONE_ERROR("18", "号码归属和所选产品归属不一致"),
+    PRICE_ERROR("19", "结算价格不一致"),
     PROVINCE_MAINTAIN("30", "省份维护"),
     PRODUCT_MAINTAIN("205", "产品维护"),
     BALANCE_NOT_ENOUGH("301", "可用余额不足"),

+ 26 - 7
modules/make-order/src/main/java/com/fire/order/rest/MakeOrderRest.java

@@ -1,9 +1,8 @@
 package com.fire.order.rest;
 
-import com.fire.dto.FlowAppInfo;
-import com.fire.order.data.DataPool;
 import com.fire.order.request.OrderRequestParam;
 import com.fire.order.response.OrderResponseDto;
+import com.fire.order.service.CacheService;
 import com.fire.order.service.MakeOrderService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -13,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
-import java.util.Map;
 
 /**
  * 下单接口rest类
@@ -26,6 +24,8 @@ public class MakeOrderRest {
 
     @Resource
     private MakeOrderService makeOrderService;
+    @Resource
+    private CacheService cacheService;
 
     /**
      * 下单接口
@@ -39,9 +39,28 @@ public class MakeOrderRest {
         return makeOrderService.makeOrder(makeOrderParam);
     }
 
-    @GetMapping("/getCache")
-    @ApiOperation(value = "测试客户缓存接口")
-    public Map<String, FlowAppInfo> cache() {
-        return DataPool.flowAppInfoMap;
+    @GetMapping("/cache/customer")
+    @ApiOperation(value = "更新客户+产品缓存")
+    public void cacheCustomer() {
+        cacheService.cacheCustomer();
+    }
+
+    @GetMapping("/cache/blacklist")
+    @ApiOperation(value = "更新黑名单缓存")
+    public void cacheBlacklist() {
+        cacheService.cacheBlacklist();
+    }
+
+    @GetMapping("/cache/virtualNum")
+    @ApiOperation(value = "更新虚拟号段缓存")
+    public void cacheVirtualNum() {
+        cacheService.cacheVirtualNum();
+    }
+
+    @GetMapping("/cache/maintenance")
+    @ApiOperation(value = "更新维护信息缓存")
+    public void cacheMaintenance() {
+        cacheService.cacheMaintenance();
     }
+
 }

+ 53 - 31
modules/make-order/src/main/java/com/fire/order/service/impl/MakeOrderServiceImpl.java

@@ -3,7 +3,6 @@ package com.fire.order.service.impl;
 
 import com.alibaba.nacos.api.utils.StringUtils;
 import com.alibaba.nacos.common.utils.CollectionUtils;
-import com.alibaba.nacos.common.utils.MD5Utils;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fire.common.redis.RedisAmountScript;
@@ -21,7 +20,10 @@ import com.fire.param.HeaderDto;
 import com.fire.utils.date.DateUtils;
 import com.fire.utils.string.FireStringUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.spring.core.RocketMQTemplate;
+import org.springframework.messaging.support.MessageBuilder;
 import org.springframework.stereotype.Service;
+import org.springframework.util.DigestUtils;
 import redis.clients.jedis.JedisCluster;
 
 import javax.annotation.Resource;
@@ -37,6 +39,8 @@ import static com.fire.dto.enums.OrderStatus.ORDER_SENT;
 import static com.fire.dto.enums.PriceCheck.CHECK;
 import static com.fire.dto.enums.Province.QG_ALL;
 import static com.fire.dto.enums.RedisKey.*;
+import static com.fire.dto.enums.RocketQueue.ORDER_QUEUE;
+import static com.fire.dto.enums.RocketTopic.MAKE_ORDER_TOPIC;
 import static com.fire.dto.enums.Status.*;
 import static com.fire.dto.enums.ValidStatus.INVALID;
 
@@ -55,6 +59,8 @@ public class MakeOrderServiceImpl implements MakeOrderService {
     private RedisOrderIdScript redisOrderIdScript;
     @Resource
     private RedisAmountScript redisAmountScript;
+    @Resource
+    private RocketMQTemplate rocketMQTemplate;
 
     /**
      * 下单方法
@@ -71,15 +77,15 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         responseDto.setMsgBody(msgBody);
         //判断请求参数是否为空
         if (null == orderRequestParam) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("请求参数为空");
+            orderResp.setRCode(PARAM_LOSS.status());
+            orderResp.setRMsg(PARAM_LOSS.message());
             return responseDto;
         }
         HeaderDto header = orderRequestParam.getHeader();
         //判断头信息是否为空
         if (null == header) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("请求参数头信息为空");
+            orderResp.setRCode(HEADER_LOSS.status());
+            orderResp.setRMsg(HEADER_LOSS.message());
             return responseDto;
         }
         responseDto.setHeader(header);
@@ -87,14 +93,14 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         long now = System.currentTimeMillis();
         String orderTime = header.getTimestamp();
         if (null == orderTime) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("时间戳为空");
+            orderResp.setRCode(DATE_ERROR.status());
+            orderResp.setRMsg(DATE_ERROR.message());
             return responseDto;
         }
         long orderT = DateUtils.String2Long(orderTime);
         if (orderT == 0) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("时间戳格式错误");
+            orderResp.setRCode(DATE_ERROR.status());
+            orderResp.setRMsg(DATE_ERROR.message());
             return responseDto;
         }
 
@@ -106,22 +112,22 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         //校验seqNo是否为空
         String seqNo = header.getSeqNo();
         if (StringUtils.isEmpty(seqNo)) {
-            orderResp.setRCode(OVER_TIME.status());
-            orderResp.setRMsg("流水号为空");
+            orderResp.setRCode(PARAM_LOSS.status());
+            orderResp.setRMsg(seqNo + PARAM_LOSS.message());
             return responseDto;
         }
         //校验消息体
         OrderRequestMsgBody requestMsgBody = orderRequestParam.getMsgBody();
         if (null == requestMsgBody) {
             orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("请求消息体为空");
+            orderResp.setRMsg("body" + PARAM_LOSS.message());
             return responseDto;
         }
         //参数正文校验
         OrderRequestContent content = requestMsgBody.getContent();
         if (null == content) {
             orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("请求参数正文为空");
+            orderResp.setRMsg("content" + PARAM_LOSS.message());
             return responseDto;
         }
         //校验packageId是否为空
@@ -134,8 +140,8 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         //校验orderType是否为空
         String orderType = content.getOrderType();
         if (StringUtils.isEmpty(orderType)) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("订单类型为空");
+            orderResp.setRCode(PARAM_LOSS.status());
+            orderResp.setRMsg(orderType + PARAM_LOSS.message());
             return responseDto;
         }
         //校验订单号是否为空
@@ -171,16 +177,16 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         //校验appId
         String appId = header.getAppId();
         if (StringUtils.isEmpty(appId)) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("APPID为空");
+            orderResp.setRCode(APP_ID_LOSE.status());
+            orderResp.setRMsg(APP_ID_LOSE.message());
             return responseDto;
         }
 
         //appId是否存在
         FlowAppInfo flowAppInfo = DataPool.flowAppInfoMap.get(appId);
         if (flowAppInfo == null) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("APPID不存在");
+            orderResp.setRCode(APP_ID_LOSE.status());
+            orderResp.setRMsg(APP_ID_LOSE.message());
             return responseDto;
         }
         //appId是否失效
@@ -190,7 +196,7 @@ public class MakeOrderServiceImpl implements MakeOrderService {
             return responseDto;
         }
         String secretStr = header.getTimestamp() + seqNo + appId + flowAppInfo.getAppKey();
-        String secretKey = MD5Utils.encodeHexString(secretStr.getBytes(StandardCharsets.UTF_8));
+        String secretKey = DigestUtils.md5DigestAsHex(secretStr.getBytes(StandardCharsets.UTF_8));
         //secretKey校验
         if (!secretKey.equals(header.getSecretKey())) {
             orderResp.setRCode(SECRET_KEY_ERROR.status());
@@ -198,7 +204,7 @@ public class MakeOrderServiceImpl implements MakeOrderService {
             return responseDto;
         }
         String signStr = flowAppInfo.getAppKey() + phoneNo + packageId + orderType + extOrder;
-        String sign = MD5Utils.encodeHexString(signStr.getBytes(StandardCharsets.UTF_8));
+        String sign = DigestUtils.md5DigestAsHex(signStr.getBytes(StandardCharsets.UTF_8));
         //sign校验
         if (!sign.equals(content.getSign())) {
             orderResp.setRCode(SIGN_FAIL.status());
@@ -219,8 +225,8 @@ public class MakeOrderServiceImpl implements MakeOrderService {
             return responseDto;
         }
         //校验产品是否有效
-        Integer isEffective = product.getIsValid();
-        if (INVALID.status().equals(isEffective)) {
+        Integer isValid = product.getIsValid();
+        if (INVALID.status().equals(isValid)) {
             orderResp.setRCode(PACKAGE_ERROR.status());
             orderResp.setRMsg(PACKAGE_ERROR.message());
             return responseDto;
@@ -229,19 +235,25 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         //根据号码前七位,获取归属地信息
         String start7 = phoneNo.substring(0, 7);
         PhoneZone phoneZone = DataPool.phoneZoneMap.get(start7);
+        if (phoneZone == null) {
+            orderResp.setRCode(PHONE_ZONE_ERROR.status());
+            orderResp.setRMsg("归属地信息未取到,需要供应商初始化归属信息");
+            return responseDto;
+        }
+        //如果归属不一样,那么不通过校验
         if (!QG_ALL.getCode().equals(product.getAreaNum())) {
             //如果归属不一样,那么不通过校验
-            if (phoneZone == null || !phoneZone.getProvinceCode().equals(product.getAreaNum())) {
-                orderResp.setRCode(FAILURE_SERVER.status());
-                orderResp.setRMsg("号码归属和所选产品归属不一致");
+            if (!phoneZone.getProvinceCode().equals(product.getAreaNum())) {
+                orderResp.setRCode(PHONE_ZONE_ERROR.status());
+                orderResp.setRMsg(PHONE_ZONE_ERROR.message());
                 return responseDto;
             }
         }
         //如果要求校验结算价格
         CustomerInfo customerInfo = flowAppInfo.getCustomerInfo();
         if (customerInfo == null || CHECK.status().equals(customerInfo.getPriceCheck()) && !product.getPrice().equals(content.getPrice())) {
-            orderResp.setRCode(FAILURE_SERVER.status());
-            orderResp.setRMsg("结算价格不一致");
+            orderResp.setRCode(PRICE_ERROR.status());
+            orderResp.setRMsg(PRICE_ERROR.message());
             return responseDto;
         }
         //校验维护信息
@@ -283,12 +295,15 @@ public class MakeOrderServiceImpl implements MakeOrderService {
 
             }
         }
+
+
         //自增id 取自redis,长度17位,后面加两位00 为主订单号,后面分发订单号后两位从 01开始
         Long orderId = jedisCluster.incr(ORDER_ID_INCR.key()) * 100;
 
         //客户订单id校验 这里订单id拼接上客户id,保证唯一
         String customerOrder = content.getExtOrder() + "." + flowAppInfo.getCustomerId();
         String checkOrder = redisOrderIdScript.checkOrderId(customerOrder, String.valueOf(orderId));
+
         //0表示订单号重复
         if ("0".equals(checkOrder)) {
             orderResp.setRCode(ORDER_ID_ERROR.status());
@@ -331,12 +346,20 @@ public class MakeOrderServiceImpl implements MakeOrderService {
             return responseDto;
         }
         //扣减
-        redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(), flowAppInfo.getCustomerId(), SUB.oper(), product.getPrice(), customerInfo.getCreditAmount());
+        Long afterAmount = redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(), flowAppInfo.getCustomerId(), SUB.oper(), product.getPrice(), customerInfo.getCreditAmount());
+        if (afterAmount == null) {
+            orderResp.setRCode(BALANCE_NOT_ENOUGH.status());
+            orderResp.setRMsg(BALANCE_NOT_ENOUGH.message());
+            return responseDto;
+        }
+
         try {
             //订单入redis
+
             jedisCluster.hset(ORDER_INFO.key(), String.valueOf(orderId), orderStr);
-            //订单入队列
 
+            //订单入队列
+            rocketMQTemplate.syncSendOrderly(MAKE_ORDER_TOPIC.topic(), MessageBuilder.withPayload(orderStr).build(), ORDER_QUEUE.queue());
         } catch (Exception e) {
             //其他异常 扣减补偿
             log.error("订单存储或者发送到队列失败", e);
@@ -345,7 +368,6 @@ public class MakeOrderServiceImpl implements MakeOrderService {
             orderResp.setRMsg(FAILURE_SERVER.message());
             return responseDto;
         }
-
         return responseDto;
     }
 

+ 11 - 5
modules/make-order/src/main/resources/bootstrap.yml

@@ -6,17 +6,23 @@ spring:
   cloud:
     nacos:
       config:
-        server-addr: 192.168.101.104:8848
+        server-addr: 127.0.0.1:8848
         file-extension: yaml
         namespace: fire
       discovery:
-        server-addr: 192.168.101.104:8848
+        server-addr: 127.0.0.1:8848
         namespace: fire
         service: make-order
   redis:
+    jedis:
+      pool:
+        max-active: 100
+        max-idle: 100
+        min-idle: 0
+        max-wait: 2000
     cluster:
-      nodes: 192.168.101.101:7001,192.168.101.101:7002,192.168.101.101:7003
+      nodes: 127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003
 rocketmq:
-  name-server: 192.168.101.12:9876
+  name-server: 127.0.0.1:9876
   producer:
-    group: make-order
+    group: make-order-group

+ 3 - 3
modules/make-order/src/main/resources/mapper/CustomerInfoMapper.xml

@@ -3,7 +3,7 @@
 <mapper namespace="com.fire.order.mapper.CustomerInfoMapper">
 
     <resultMap type="com.fire.dto.CustomerInfo" id="CustomerInfoMap">
-        <result property="customerId" column="customer_id" jdbcType="INTEGER"/>
+        <result property="customerId" column="customer_id" jdbcType="BIGINT"/>
         <result property="partnerId" column="partner_id" jdbcType="INTEGER"/>
         <result property="customerName" column="customer_name" jdbcType="VARCHAR"/>
         <result property="shorterName" column="shorter_name" jdbcType="VARCHAR"/>
@@ -21,8 +21,8 @@
         <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
         <result property="updator" column="updator" jdbcType="VARCHAR"/>
         <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
-        <result property="partnerCommission" column="partner_commission" jdbcType="VARCHAR"/>
-        <result property="userId" column="user_id" jdbcType="INTEGER"/>
+        <result property="partnerCommission" column="partner_commission"/>
+        <result property="userId" column="user_id" jdbcType="BIGINT"/>
         <result property="priceCheck" column="price_check" jdbcType="INTEGER"/>
     </resultMap>
 

+ 3 - 1
modules/make-order/src/main/resources/mapper/CustomerProductMapper.xml

@@ -13,6 +13,7 @@
         <result property="isValid" column="is_valid" jdbcType="INTEGER"/>
         <result property="areaName" column="area_name" jdbcType="VARCHAR"/>
         <result property="operator" column="operator" jdbcType="INTEGER"/>
+        <result property="areaNum" column="area_num" jdbcType="VARCHAR"/>
     </resultMap>
 
     <!--查询单个-->
@@ -26,7 +27,8 @@
                a.face_price,
                a.is_valid,
                b.area_name,
-               b.operator
+               b.operator,
+               b.area_num
         from customer_product a
                  left join flow_product_info b on a.package_id = b.package_id
     </select>