Browse Source

增加服务集群调用

hurugang 2 years ago
parent
commit
0373f70e41

+ 24 - 0
src/main/java/cn/hnthyy/thmz/controller/mz/DiscountController.java

@@ -279,4 +279,28 @@ public class DiscountController {
             return resultMap;
         }
     }
+
+
+
+    /**
+     * 集群调用刷新缓存
+     *
+     * @return
+     */
+    @RequestMapping(value = "/refreshDiscount", method = {RequestMethod.GET})
+    public Map<String, Object> refreshDiscount() {
+        Map<String, Object> resultMap = new HashMap<>();
+        try {
+            discountService.getCacheMap();
+            resultMap.put("code", 0);
+            resultMap.put("message", "集群调用刷新缓存成功");
+            return resultMap;
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("集群调用刷新缓存失败,错误信息{}", e);
+            resultMap.put("code", -1);
+            resultMap.put("message", "集群调用刷新缓存失败");
+            return resultMap;
+        }
+    }
 }

+ 1 - 1
src/main/java/cn/hnthyy/thmz/mapper/thmz/ConfigMapper.java

@@ -11,7 +11,7 @@ public interface ConfigMapper {
      * @return
      */
     @Select("select id,config_key, config_value, create_id, create_date from t_config where config_key=#{configKey}")
-    Config selectUserByKey(@Param("configKey") String configKey);
+    Config selectConfigByKey(@Param("configKey") String configKey);
     /**
      * 新增文件
      *

+ 2 - 2
src/main/java/cn/hnthyy/thmz/service/impl/his/mz/MzChargeDetailServiceImpl.java

@@ -2682,7 +2682,7 @@ public class MzChargeDetailServiceImpl implements MzChargeDetailService {
      * @throws MzException
      */
     private void checkHybrid(String title, String hybirdNumberKey, String hybirdTimeKey, int totalNum) throws MzException {
-        Config config = configMapper.selectUserByKey(hybirdNumberKey);
+        Config config = configMapper.selectConfigByKey(hybirdNumberKey);
         if (config == null || StringUtils.isBlank(config.getConfigValue())) {
             throw new MzException(title + "最大数参数为空!");
         }
@@ -2690,7 +2690,7 @@ public class MzChargeDetailServiceImpl implements MzChargeDetailService {
         if (totalNum >= hybirdNumber) {
             throw new MzException("抱歉," + title + "人数以达到最大数值,无法继续采样!");
         }
-        config = configMapper.selectUserByKey(hybirdTimeKey);
+        config = configMapper.selectConfigByKey(hybirdTimeKey);
         if (config == null || StringUtils.isBlank(config.getConfigValue())) {
             throw new MzException(title + "时间参数为空!");
         }

+ 1 - 1
src/main/java/cn/hnthyy/thmz/service/impl/his/mz/MzyReqrecServiceImpl.java

@@ -681,7 +681,7 @@ public class MzyReqrecServiceImpl implements MzyReqrecService {
         Calendar cal = Calendar.getInstance();
         cal.setTime(requestDayD);
         //设置了免费 周末自助挂号免费
-        Config config = configMapper.selectUserByKey("weekend_fee");
+        Config config = configMapper.selectConfigByKey("weekend_fee");
         if ((config != null && YesNoEnum.YES.code.toString().equals(config.getConfigValue()))
                 && ((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && Constants.PM.equals(ampm)) || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
             return true;

+ 1 - 1
src/main/java/cn/hnthyy/thmz/service/impl/thmz/ConfigServiceImpl.java

@@ -13,7 +13,7 @@ public class ConfigServiceImpl implements ConfigService {
     private ConfigMapper configMapper;
     @Override
     public Config queryUserByKey(String key) {
-        return configMapper.selectUserByKey(key);
+        return configMapper.selectConfigByKey(key);
     }
 
     @Override

+ 53 - 1
src/main/java/cn/hnthyy/thmz/service/impl/thmz/DiscountServiceImpl.java

@@ -1,18 +1,29 @@
 package cn.hnthyy.thmz.service.impl.thmz;
 
+import cn.hnthyy.thmz.Utils.HttpUtil;
+import cn.hnthyy.thmz.entity.thmz.Config;
 import cn.hnthyy.thmz.entity.thmz.Discount;
+import cn.hnthyy.thmz.mapper.thmz.ConfigMapper;
 import cn.hnthyy.thmz.mapper.thmz.DiscountMapper;
 import cn.hnthyy.thmz.service.thmz.DiscountService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.net.InetAddress;
+import java.net.URLEncoder;
+import java.net.UnknownHostException;
 import java.util.*;
-
+@Slf4j
 @Service
 public class DiscountServiceImpl implements DiscountService {
     @SuppressWarnings("all")
     @Autowired
     private DiscountMapper discountMapper;
+    @SuppressWarnings("all")
+    @Autowired
+    private ConfigMapper configMapper;
     /**
      * 缓存
      */
@@ -23,6 +34,7 @@ public class DiscountServiceImpl implements DiscountService {
         discount.setCreateTime(new Date());
         int num = discountMapper.insert(discount);
         getCacheMap();
+        refreshGroupDiscount();
         return num;
     }
     @Override
@@ -51,6 +63,7 @@ public class DiscountServiceImpl implements DiscountService {
         discount.setUpdateTime(new Date());
         int num = discountMapper.update(discount);
         getCacheMap();
+        refreshGroupDiscount();
         return num;
     }
 
@@ -78,6 +91,7 @@ public class DiscountServiceImpl implements DiscountService {
     public int removeById(Long id) {
         int num = discountMapper.deleteById(id);
         getCacheMap();
+        refreshGroupDiscount();
         return num;
     }
 
@@ -92,4 +106,42 @@ public class DiscountServiceImpl implements DiscountService {
     }
 
 
+    /**
+     * 刷新服务集群缓存
+     */
+    private void refreshGroupDiscount(){
+        String localhost=null;
+        try {
+            localhost=InetAddress.getLocalHost().getHostAddress();
+        } catch (UnknownHostException e) {
+            e.printStackTrace();
+            return;
+        }
+        if(localhost==null || StringUtils.isBlank(localhost)){
+            return;
+        }
+        Config config= configMapper.selectConfigByKey("thmz_group");
+        if(config==null || config.getConfigValue()==null || StringUtils.isBlank(config.getConfigValue())){
+            return;
+        }
+        String [] arr=config.getConfigValue().split(",");
+        if(arr==null || arr.length==0){
+            return;
+        }
+        for (int i=0;i<arr.length;i++){
+            String ip=arr[i];
+            if(ip==null || StringUtils.isBlank(ip)){
+                continue;
+            }
+            if(localhost.equals(ip)){
+                continue;
+            }
+            try {
+                HttpUtil.sendHttpGet(ip+"/thmz/refreshDiscount", "utf-8", 3000);
+            } catch (Exception e) {
+                e.printStackTrace();
+                log.error("刷新服务集群缓存失败!");
+            }
+        }
+    }
 }