Przeglądaj źródła

将刷新缓存开放为接口

wuxw7 7 lat temu
rodzic
commit
6894a93a99

+ 1 - 1
CenterService/docker/bin/start_center.sh

@@ -10,6 +10,6 @@
 
 
 #### normal dev model
 #### normal dev model
 #nohup java -jar -Dspring.profiles.active=dev $1 target/CenterService.jar > center.log $1 2>&1 &
 #nohup java -jar -Dspring.profiles.active=dev $1 target/CenterService.jar > center.log $1 2>&1 &
-nohup java -jar -Dspring.profiles.active=$1 $2 target/CenterService.jar > center.log $2 2>&1 &
+nohup java -jar -Dspring.profiles.active=$1 $2 target/CenterService.jar > center.log 2>&1 &
 
 
 tail -100f center.log
 tail -100f center.log

+ 36 - 4
CenterService/src/main/java/com/java110/center/api/CacheApi.java

@@ -2,17 +2,23 @@ package com.java110.center.api;
 
 
 import com.java110.center.smo.ICenterServiceCacheSMO;
 import com.java110.center.smo.ICenterServiceCacheSMO;
 import com.java110.common.constant.ResponseConstant;
 import com.java110.common.constant.ResponseConstant;
+import com.java110.core.base.controller.BaseController;
 import com.java110.core.factory.DataQueryFactory;
 import com.java110.core.factory.DataQueryFactory;
 import com.java110.core.factory.DataTransactionFactory;
 import com.java110.core.factory.DataTransactionFactory;
-import com.java110.core.base.controller.BaseController;
 import com.java110.entity.service.DataQuery;
 import com.java110.entity.service.DataQuery;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
 
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
 
 
 /**
 /**
  * 主要提供给内部刷缓存用
  * 主要提供给内部刷缓存用
@@ -20,14 +26,24 @@ import javax.servlet.http.HttpServletRequest;
  */
  */
 @RestController
 @RestController
 public class CacheApi extends BaseController{
 public class CacheApi extends BaseController{
+    protected final static Logger logger = LoggerFactory.getLogger(CacheApi.class);
     @Autowired
     @Autowired
     ICenterServiceCacheSMO centerServiceCacheSMOImpl;
     ICenterServiceCacheSMO centerServiceCacheSMOImpl;
 
 
     @RequestMapping(path = "/cacheApi/flush",method= RequestMethod.GET)
     @RequestMapping(path = "/cacheApi/flush",method= RequestMethod.GET)
-    public String flushGet(HttpServletRequest request) {
+    public ResponseEntity<String> flushGet(HttpServletRequest request) {
+        ResponseEntity<String> responseEntity = null;
+        try {
+            Map<String, String> headers = new HashMap<String, String>();
+            this.getRequestInfo(request, headers);
+            centerServiceCacheSMOImpl.flush(headers);
+            responseEntity = new ResponseEntity<String>("刷新缓存成功", HttpStatus.OK);
+        }catch (Exception e){
+            logger.error("刷新缓存失败,",e);
+            responseEntity = new ResponseEntity<String>("刷新缓存失败,"+e.getMessage(), HttpStatus.BAD_REQUEST);
 
 
-        return DataTransactionFactory.createOrderResponseJson(ResponseConstant.NO_TRANSACTION_ID,
-                ResponseConstant.RESULT_CODE_ERROR,"不支持Get方法请求").toJSONString();
+        }
+        return responseEntity;
     }
     }
 
 
     @RequestMapping(path = "/cacheApi/flush",method= RequestMethod.POST)
     @RequestMapping(path = "/cacheApi/flush",method= RequestMethod.POST)
@@ -42,6 +58,22 @@ public class CacheApi extends BaseController{
         }
         }
     }
     }
 
 
+    /**
+     * 获取请求信息
+     * @param request
+     * @param headers
+     * @throws RuntimeException
+     */
+    private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
+        try{
+            super.initHeadParam(request,headers);
+            super.initUrlParam(request,headers);
+        }catch (Exception e){
+            logger.error("加载头信息失败",e);
+            throw e;
+        }
+    }
+
     public ICenterServiceCacheSMO getCenterServiceCacheSMOImpl() {
     public ICenterServiceCacheSMO getCenterServiceCacheSMOImpl() {
         return centerServiceCacheSMOImpl;
         return centerServiceCacheSMOImpl;
     }
     }

+ 8 - 0
CenterService/src/main/java/com/java110/center/smo/ICenterServiceCacheSMO.java

@@ -2,6 +2,8 @@ package com.java110.center.smo;
 
 
 import com.java110.entity.service.DataQuery;
 import com.java110.entity.service.DataQuery;
 
 
+import java.util.Map;
+
 /**
 /**
  * Created by wuxw on 2018/4/18.
  * Created by wuxw on 2018/4/18.
  */
  */
@@ -12,6 +14,12 @@ public interface ICenterServiceCacheSMO {
      */
      */
     public void flush(DataQuery dataQuery);
     public void flush(DataQuery dataQuery);
 
 
+    /**
+     * 根据缓存类别刷新缓存
+     * @param headers 缓存类别
+     */
+    public void flush(Map<String,String> headers);
+
     /**
     /**
      * 系统启动刷新
      * 系统启动刷新
      */
      */

+ 59 - 0
CenterService/src/main/java/com/java110/center/smo/impl/CenterServiceCacheSMOImpl.java

@@ -9,6 +9,7 @@ import com.java110.common.cache.ServiceSqlCache;
 import com.java110.common.constant.CommonConstant;
 import com.java110.common.constant.CommonConstant;
 import com.java110.common.constant.ResponseConstant;
 import com.java110.common.constant.ResponseConstant;
 import com.java110.common.exception.SMOException;
 import com.java110.common.exception.SMOException;
+import com.java110.common.util.Assert;
 import com.java110.core.factory.DataTransactionFactory;
 import com.java110.core.factory.DataTransactionFactory;
 import com.java110.entity.center.AppRoute;
 import com.java110.entity.center.AppRoute;
 import com.java110.entity.mapping.Mapping;
 import com.java110.entity.mapping.Mapping;
@@ -53,6 +54,20 @@ public class CenterServiceCacheSMOImpl implements ICenterServiceCacheSMO {
         dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,"刷新成功"));
         dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,"刷新成功"));
     }
     }
 
 
+    /**
+     * 根据缓存类别刷新缓存
+     * @param headers 缓存类别
+     */
+    public void flush(Map<String,String> headers) throws SMOException{
+
+        flushAppRoute(headers);
+
+        flushMapping(headers);
+
+        //3.0 分装 ServiceSql
+        flushServiceSql(headers);
+    }
+
     /**
     /**
      * 用来系统启动刷新
      * 用来系统启动刷新
      */
      */
@@ -87,6 +102,20 @@ public class CenterServiceCacheSMOImpl implements ICenterServiceCacheSMO {
         doFlushServiceSql();
         doFlushServiceSql();
     }
     }
 
 
+    /**
+     * 3.0 分装 ServiceSql
+     */
+    private void flushServiceSql(Map<String,String> headers) {
+
+        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"未包含cache参数"+headers.toString());
+        if(!CommonConstant.CACHE_SERVICE_SQL.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
+            return ;
+        }
+        // 刷新
+        doFlushServiceSql();
+    }
+
     private void doFlushServiceSql() {
     private void doFlushServiceSql() {
         List<ServiceSql> serviceSqls = queryServiceDAOImpl.qureyServiceSqlAll();
         List<ServiceSql> serviceSqls = queryServiceDAOImpl.qureyServiceSqlAll();
 
 
@@ -116,6 +145,21 @@ public class CenterServiceCacheSMOImpl implements ICenterServiceCacheSMO {
         doFlushMapping();
         doFlushMapping();
     }
     }
 
 
+    /**
+     * 刷新 Mapping 数据
+     */
+    private void flushMapping(Map<String,String> headers) {
+
+        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"未包含cache参数"+headers.toString());
+
+        if(!CommonConstant.CACHE_MAPPING.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
+            return ;
+        }
+
+        doFlushMapping();
+    }
+
     private void doFlushMapping() {
     private void doFlushMapping() {
         List<Mapping> mappings = centerServiceDAOImpl.getMappingInfoAll();
         List<Mapping> mappings = centerServiceDAOImpl.getMappingInfoAll();
         //删除原始数据
         //删除原始数据
@@ -156,6 +200,21 @@ public class CenterServiceCacheSMOImpl implements ICenterServiceCacheSMO {
 
 
     }
     }
 
 
+    /**
+     * 刷新AppRoute数据
+     */
+    private void flushAppRoute(Map<String,String> headers){
+
+        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"未包含cache参数"+headers.toString());
+
+        if(!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
+            return ;
+        }
+        doFlushAppRoute();
+
+    }
+
     private void doFlushAppRoute() {
     private void doFlushAppRoute() {
         List<Map> appInfos = centerServiceDAOImpl.getAppRouteAndServiceInfoAll();
         List<Map> appInfos = centerServiceDAOImpl.getAppRouteAndServiceInfoAll();
         Map<String,List<AppRoute>> appRoustsMap = new HashMap<String,List<AppRoute>>();
         Map<String,List<AppRoute>> appRoustsMap = new HashMap<String,List<AppRoute>>();

+ 3 - 0
java110-common/src/main/java/com/java110/common/constant/CommonConstant.java

@@ -105,6 +105,7 @@ public class CommonConstant {
 
 
     public final static String CACHE_PARAM_NAME = "cacheName";
     public final static String CACHE_PARAM_NAME = "cacheName";
 
 
+    public final static String CACHE_PARAM = "cache";
     /**
     /**
      * 组件路由 服务 缓存常量
      * 组件路由 服务 缓存常量
      */
      */
@@ -120,6 +121,8 @@ public class CommonConstant {
      */
      */
     public final static String CACHE_SERVICE_SQL = "SERVICE_SQL";
     public final static String CACHE_SERVICE_SQL = "SERVICE_SQL";
 
 
+    public final static String CACHE_ALL = "All";
+
 
 
     public final static String INSTANCE_Y = "Y";
     public final static String INSTANCE_Y = "Y";
 
 

+ 6 - 0
java110-common/src/main/java/com/java110/common/constant/ServiceCodeConstant.java

@@ -186,6 +186,12 @@ public class ServiceCodeConstant {
      * 检查用户登录服务处理
      * 检查用户登录服务处理
      */
      */
     public static final String SERVICE_CODE_CHECK_SERVICE_LOGIN = "check.service.login";
     public static final String SERVICE_CODE_CHECK_SERVICE_LOGIN = "check.service.login";
+    /**
+     * 刷新缓存
+     */
+    public static final String SERVICE_CODE_CACHE_SERVICE_FLUSH = "cache.service.flush";
+
+