Sfoglia il codice sorgente

产品拦截列表放到数据库,并且加载到缓存

gaodm 5 anni fa
parent
commit
7cad8bdd85

+ 0 - 3
config-server/src/main/resources/shared/gateway-service-dev.yml

@@ -181,6 +181,3 @@ spring:
 server:
   port: 5050
 
-lantone:
-  product: triagett,143;icsstt,2
-

+ 0 - 3
config-server/src/main/resources/shared/gateway-service-local.yml

@@ -181,6 +181,3 @@ spring:
 server:
   port: 5050
 
-lantone:
-  product: triage33,1;icsstt,2
-

+ 0 - 2
config-server/src/main/resources/shared/gateway-service-pre.yml

@@ -191,6 +191,4 @@ spring:
 server:
   port: 5050
 
-lantone:
-  product: triagett,143;icsstt,2
 

+ 0 - 2
config-server/src/main/resources/shared/gateway-service-pro.yml

@@ -181,6 +181,4 @@ spring:
 server:
   port: 5050
 
-lantone:
-  product: triagett,143;icsstt,2
 

+ 0 - 2
config-server/src/main/resources/shared/gateway-service-test.yml

@@ -181,6 +181,4 @@ spring:
 server:
   port: 5050
 
-lantone:
-  product: triage33,1;icssyz,147
 

+ 11 - 2
diagbotman-service/src/main/java/com/diagbot/facade/ServiceFilterFacade.java

@@ -3,9 +3,13 @@ package com.diagbot.facade;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.entity.ServiceFilter;
 import com.diagbot.service.impl.ServiceFilterServiceImpl;
+import com.diagbot.util.ListUtil;
 import org.springframework.stereotype.Component;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * @Description: 产品服务过滤
@@ -20,7 +24,12 @@ public class ServiceFilterFacade extends ServiceFilterServiceImpl {
      *
      * @return 服务列表
      */
-    public RespDTO<List<ServiceFilter>> getServiceFilter() {
-        return this.getAll();
+    public  Map<String, Long> getServiceFilter() {
+        Map<String, Long> resMap = new HashMap<>();
+        List<ServiceFilter> list = this.getAll();
+        if (ListUtil.isNotEmpty(list)){
+            resMap = list.stream().collect(Collectors.toMap(ServiceFilter::getName, ServiceFilter::getProductId));
+        }
+        return resMap;
     }
 }

+ 1 - 1
diagbotman-service/src/main/java/com/diagbot/service/ServiceFilterService.java

@@ -18,5 +18,5 @@ public interface ServiceFilterService extends IService<ServiceFilter> {
      * @Author: ztg
      * @Date: 2018/9/18 14:57
      */
-    public RespDTO<List<ServiceFilter>> getAll();
+    public List<ServiceFilter> getAll();
 }

+ 2 - 3
diagbotman-service/src/main/java/com/diagbot/service/impl/ServiceFilterServiceImpl.java

@@ -24,9 +24,8 @@ public class ServiceFilterServiceImpl extends ServiceImpl<ServiceFilterMapper, S
      * @return
      */
     @Override
-    public RespDTO<List<ServiceFilter>> getAll() {
-        List<ServiceFilter> list = baseMapper.getAll();
-        return RespDTO.onSuc(list);
+    public List<ServiceFilter> getAll() {
+        return baseMapper.getAll();
     }
 
 

+ 3 - 4
diagbotman-service/src/main/java/com/diagbot/web/ServiceFilterController.java

@@ -1,7 +1,6 @@
 package com.diagbot.web;
 
 import com.diagbot.dto.RespDTO;
-import com.diagbot.entity.ServiceFilter;
 import com.diagbot.facade.ServiceFilterFacade;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -10,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import springfox.documentation.annotations.ApiIgnore;
 
-import java.util.List;
+import java.util.Map;
 
 /**
  * @Description: 产品服务过滤
@@ -30,8 +29,8 @@ public class ServiceFilterController {
     @ApiOperation(value = "获取需要appkey和secret的服务信息[by:zhoutg]",
             notes = "接口未用,直接从配置项中读取服务")
     @ApiIgnore
-    public RespDTO<List<ServiceFilter>> getServiceFilter() {
-        return serviceFilterFacade.getAll();
+    public RespDTO<Map<String, Long>> getServiceFilter() {
+        return RespDTO.onSuc(serviceFilterFacade.getServiceFilter());
     }
 
 

+ 17 - 1
docs/031.20200325记录产品操作记录/init_diagbotman.sql

@@ -1,4 +1,5 @@
 use `sys-diagbotman`;
+
 DROP TABLE IF EXISTS `diag_opt_info`;
 CREATE TABLE `diag_opt_info` (
   `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
@@ -16,4 +17,19 @@ CREATE TABLE `diag_opt_info` (
   `city` varchar(50) NOT NULL DEFAULT '' COMMENT '城市',
   `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
   PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='产品操作记录表';
+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='产品操作记录表';
+
+DROP TABLE IF EXISTS `diag_service_filter`;
+CREATE TABLE `diag_service_filter` (
+  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
+  `is_deleted` char(1) NOT NULL DEFAULT 'N' COMMENT '是否删除,N:未删除,Y:删除',
+  `gmt_create` datetime NOT NULL DEFAULT '1970-01-01 12:00:00' COMMENT '记录创建时间',
+  `gmt_modified` datetime NOT NULL DEFAULT '1970-01-01 12:00:00' COMMENT '记录修改时间,如果时间是1970年则表示纪录未修改',
+  `creator` varchar(20) NOT NULL DEFAULT '0' COMMENT '创建人,0表示无创建人值',
+  `modifier` varchar(20) NOT NULL DEFAULT '0' COMMENT '修改人,如果为0则表示纪录未修改',
+  `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名称',
+  `product_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '产品id',
+  `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注',
+  PRIMARY KEY (`id`),
+  UNIQUE KEY `name` (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='产品网关过滤(需要使用appkey和secret的url地址)';

+ 50 - 0
gateway-service/src/main/java/com/diagbot/facade/ServiceFilterFacade.java

@@ -0,0 +1,50 @@
+package com.diagbot.facade;
+
+import com.diagbot.dto.RespDTO;
+import com.diagbot.exception.CommonErrorCode;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.Map;
+
+/**
+ * @Description: 产品服务过滤
+ * @Author: ztg
+ * @Date: 2018/9/18 14:45
+ */
+@Component
+@Slf4j
+public class ServiceFilterFacade {
+    private static final String SERVICEFILTER = "ServiceFilter";
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    /**
+     * 获取需要appkey和secret的服务,未用
+     *
+     * @return 服务列表
+     */
+    @Cacheable(value = SERVICEFILTER, key = "'ServiceFilter'")
+    public RespDTO<Map<String, Long>> getServiceFilterRpc() {
+        try {
+            RespDTO<Map<String, Long>> res
+                    = restTemplate.postForObject("http://diagbotman-service/service_filter/getAll", null, RespDTO.class);
+            log.info("读取了拦截产品线缓存!");
+            return res;
+        } catch (Exception ex) {
+            return getServiceFilterFallback();
+        }
+    }
+
+    public RespDTO getServiceFilterFallback() {
+        log.error("【hystrix】调用{}异常", "getServiceFilter");
+        RespDTO respDTO = new RespDTO();
+        respDTO.msg = CommonErrorCode.RPC_ERROR.getMsg();
+        respDTO.code = CommonErrorCode.RPC_ERROR.getCode();
+        return respDTO;
+    }
+}

+ 22 - 31
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -8,12 +8,12 @@ import com.diagbot.entity.ServiceFilter;
 import com.diagbot.entity.ServiceToken;
 import com.diagbot.enums.SysTypeEnum;
 import com.diagbot.exception.CommonErrorCode;
+import com.diagbot.facade.ServiceFilterFacade;
 import com.diagbot.rabbit.MySender;
 import com.diagbot.util.EnDecodeUtil;
 import com.diagbot.util.GsonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.cloud.gateway.filter.GatewayFilterChain;
 import org.springframework.cloud.gateway.filter.GlobalFilter;
@@ -52,8 +52,8 @@ public class GlobalGatewayFilter implements GlobalFilter {
     private static Boolean IS_GENERATE = false;
     private static Map<String, Long> SERVICE_FILTER = new HashMap<>();
 
-    @Value("${lantone.product}")
-    private String lantonePrduct;
+    //    @Value("${lantone.product}")
+    //    private String lantonePrduct;
 
     //    @Autowired
     //    DiagbotmanServiceClient diagbotmanServiceClient;
@@ -61,6 +61,8 @@ public class GlobalGatewayFilter implements GlobalFilter {
     private MySender mySender;
     @Autowired
     private RestTemplate restTemplate;
+    @Autowired
+    private ServiceFilterFacade serviceFilterFacade;
 
 
     @Override
@@ -82,23 +84,13 @@ public class GlobalGatewayFilter implements GlobalFilter {
                 }
             }
         }
-        //        log.info("APIURL:{}", requestUri);
-        //        log.info("SERVICENAME:{}", serviceName);
-
-        //        if(!IS_GENERATE) {
-        //            RespDTO<List<ServiceFilter>> filter = diagbotmanServiceClient.getAll();
-        //            if (filter != null){
-        //                List<ServiceFilter> list =  filter.data;
-        //                if (ListUtil.isNotEmpty(list)){
-        //                    for(ServiceFilter bean : list) {
-        //                        SERVICE_FILTER.put(bean.getName(), bean.getProductId());
-        //                    }
-        //                    IS_GENERATE = true;
-        //                }
-        //            }
-        //        }
-
-        Map<String, Long> serviceFilters = this.dealServiceFilter();
+
+        // Map<String, Long> serviceFilters = this.dealServiceFilter();
+        RespDTO<Map<String, Long>> resFilter = serviceFilterFacade.getServiceFilterRpc();
+        if (resFilter == null || !RespDTO.TRUE_CODE.equals(resFilter.code)) {
+            return getVoidMono(serverWebExchange, resFilter);
+        }
+        Map<String, Long> serviceFilters = resFilter.data;
         if (serviceFilters.get(serviceName) != null) {
             String appkey = request.getHeaders().getFirst("appKeyId");
             String secret = request.getHeaders().getFirst("appKeySecret");
@@ -127,7 +119,7 @@ public class GlobalGatewayFilter implements GlobalFilter {
             st.setAppkey(appkey);
             st.setSecret(secret);
             st.setProductId(productId);
-            //            RespDTO<HasPermissionDTO> res = diagbotmanServiceClient.hasPermission(st);
+            //权限验证
             RespDTO<LinkedHashMap> res = hasPermission(st);
             if (res == null || !RespDTO.TRUE_CODE.equals(res.code)) {
                 return getVoidMono(serverWebExchange, res);
@@ -175,16 +167,15 @@ public class GlobalGatewayFilter implements GlobalFilter {
      *
      * @return 过滤的列表
      */
-    private Map<String, Long> dealServiceFilter() {
-        Map<String, Long> serviceFilters = new HashMap<>();
-        String[] set = lantonePrduct.split(";");
-        for (String s : set) {
-            String[] d = s.split(",");
-            serviceFilters.put(d[0], Long.valueOf(d[1]));
-        }
-        return serviceFilters;
-    }
-
+    //    private Map<String, Long> dealServiceFilter() {
+    //        Map<String, Long> serviceFilters = new HashMap<>();
+    //        String[] set = lantonePrduct.split(";");
+    //        for (String s : set) {
+    //            String[] d = s.split(",");
+    //            serviceFilters.put(d[0], Long.valueOf(d[1]));
+    //        }
+    //        return serviceFilters;
+    //    }
     public RespDTO<LinkedHashMap> hasPermission(ServiceToken st) {
         try {
             RespDTO<LinkedHashMap> res

+ 33 - 0
gateway-service/src/main/java/com/diagbot/web/CacheController.java

@@ -0,0 +1,33 @@
+package com.diagbot.web;
+
+import com.diagbot.dto.RespDTO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @Description: 清理缓存控制层
+ * @Author:gaodm
+ * @time: 2019/5/22 15:07
+ */
+@RestController
+@RequestMapping("/cache")
+@Api(value = "缓存相关API", tags = { "缓存相关API" })
+@SuppressWarnings("unchecked")
+@Slf4j
+public class CacheController {
+    private static final String SERVICEFILTER = "ServiceFilter";
+
+    @ApiOperation(value = "清空缓存[by:gaodm]",
+            notes = "")
+    @GetMapping("/clear")
+    @CacheEvict(cacheNames = SERVICEFILTER, allEntries = true)
+    public RespDTO<Boolean> clearCache() {
+        log.info("清理了拦截产品线缓存!");
+        return RespDTO.onSuc(true);
+    }
+}