Selaa lähdekoodia

FeignClientInvoker 优化
风控 ScheduledExecutor调度资源优化

marxjaw 1 kuukausi sitten
vanhempi
commit
a775e99f63

+ 10 - 1
yt-risk/risk-manage/src/main/java/com/ytpm/RiskManageApplication.java

@@ -5,18 +5,27 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.Bean;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+
 @RestController
 @SpringBootApplication
 @EnableDiscoveryClient
 @MapperScan(basePackages = "com.ytpm.dao")
-@EnableFeignClients(basePackages = {"com.ytpm.feign","com.ytpm.*.feign","com.ytpm.*.*.feign"})
+@EnableFeignClients(basePackages = {"com.ytpm.feign","com.ytpm.*.feign"})
 public class RiskManageApplication
 {
     public static void main( String[] args )
     {
         SpringApplication.run(RiskManageApplication.class, args);
     }
+    @Bean(destroyMethod = "shutdown")
+    public ScheduledExecutorService riskScheduledExecutor() {
+        return new ScheduledThreadPoolExecutor(5, r -> new Thread(r, "risk-scheduler"), new ThreadPoolExecutor.DiscardPolicy());
+    }
 }

+ 3 - 4
yt-risk/risk-manage/src/main/java/com/ytpm/service/impl/RiskServiceImpl.java

@@ -116,6 +116,8 @@ public class RiskServiceImpl implements RiskService {
 
     @Value("${risk.visitor.validity-period:48}")
     private Integer loginCheckValidityPeriod;
+    @Resource
+    private ScheduledExecutorService scheduledExecutorService;
 
 
     /**
@@ -747,9 +749,7 @@ public class RiskServiceImpl implements RiskService {
         addBannedRecord(Collections.singletonList(dyzUser.getUserId()),param);
         YtApp app = appMapper.selectRiskApp(dyzUser.getAppId());
         YtPlatformUserApp appInfo = appMapper.selectParentApp(app.getSuperiorId());
-
-        ScheduledExecutorService scheduled  = Executors.newSingleThreadScheduledExecutor();
-        scheduled.schedule(()->{
+        scheduledExecutorService.schedule(()->{
             YtDyzUser next = new YtDyzUser();
             next.setUserId(dyzUser.getUserId());
             next.setUserStatus(UserStatusEnum.LOCK.getCode());
@@ -759,7 +759,6 @@ public class RiskServiceImpl implements RiskService {
         },300, TimeUnit.MILLISECONDS);
         //修改为解锁用户存入redis 24小时后进行解锁
         redisService.setTimeOutHoursStr("unlock_"+dyzUser.getUserId(),dyzUser.getAppId(), 24);
-        scheduled.shutdown();
         throw new CustomerException(errMsg);
     }
 

+ 3 - 3
yt-risk/risk-manage/src/main/java/com/ytpm/util/FeignClientInvoker.java

@@ -5,8 +5,8 @@ import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Component;
 
 import java.lang.reflect.Method;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * FeignClient公共调用类
@@ -17,8 +17,8 @@ import java.util.Map;
 public class FeignClientInvoker {
 
     private final ApplicationContext applicationContext;
-    private final Map<String, Object> feignClientCache = new HashMap<>();
-    private final Map<String, Method> methodCache = new HashMap<>();
+    private final Map<String, Object> feignClientCache = new ConcurrentHashMap<>();
+    private final Map<String, Method> methodCache = new ConcurrentHashMap<>();
 
     public FeignClientInvoker(ApplicationContext applicationContext) {
         this.applicationContext = applicationContext;