feat:多数据源模块定时器存储

master
hbr 2024-06-06 19:57:05 +08:00
parent 3fccdf3da0
commit 2d4153261b
6 changed files with 54 additions and 12 deletions

View File

@ -0,0 +1,37 @@
package com.zhiLian.business.Job;
import com.zhiLian.business.domain.Entinfo;
import com.zhiLian.business.service.impl.EntinfoServiceImpl;
import com.zhiLian.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* BingRui.Hou
*
* @Description
* @ClassName ManyJob
* @Date 2024/06/06 18:32
*/
@Component
@Log4j2
public class ManyJob {
@Autowired
private RedisService redisService;
@Autowired
private EntinfoServiceImpl entinfoService;
@Scheduled(cron = "*/10 * * * * *") // 每10分钟执行一次
public void manyJob(){
redisService.deleteObject("entinfo");
List<Entinfo> list = entinfoService.list();
redisService.setCacheList("entinfo",list);
}
}

View File

@ -7,6 +7,7 @@ import com.zhiLian.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* BingRui.Hou * BingRui.Hou
@ -19,6 +20,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableCustomSwagger2 @EnableCustomSwagger2
@EnableMyFeignClients @EnableMyFeignClients
@SpringBootApplication @SpringBootApplication
@EnableScheduling
public class ZhiLianBusinessApplication { public class ZhiLianBusinessApplication {
public static void main (String[] args) { public static void main (String[] args) {
SpringApplication.run(ZhiLianBusinessApplication.class, args); SpringApplication.run(ZhiLianBusinessApplication.class, args);

View File

@ -8,6 +8,7 @@ import com.zhiLian.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* BingRui.Hou * BingRui.Hou

View File

@ -33,8 +33,6 @@ import java.util.Map;
@AutoConfigureBefore(RedisService.class) @AutoConfigureBefore(RedisService.class)
public class ManyDataSource { public class ManyDataSource {
@Autowired
private RedisService redisService;
@Autowired @Autowired
private RedisTemplate<String,String> redisTemplate; private RedisTemplate<String,String> redisTemplate;
@ -90,4 +88,8 @@ public class ManyDataSource {
dynamicDataSource.setDefineTargetDataSources(dataSourceMap); dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
return dynamicDataSource; return dynamicDataSource;
} }
public void init(){
}
} }

View File

@ -33,7 +33,7 @@ public class DruidDataSourceFactory {
druidDataSource.setConnectionErrorRetryAttempts(0); druidDataSource.setConnectionErrorRetryAttempts(0);
try { try {
druidDataSource.getConnection(2000); druidDataSource.getConnection(2000);
log.info("{} -> 数据源连接成功", dataSourceInfo.getKey()); log.info("{} -> 数据源连接成功", dataSourceInfo);
return druidDataSource; return druidDataSource;
} catch (SQLException throwables) { } catch (SQLException throwables) {
log.error("数据源 {} 连接失败,用户名:{},密码 {}",dataSourceInfo.getUrl(),dataSourceInfo.getUserName(),dataSourceInfo.getPassword()); log.error("数据源 {} 连接失败,用户名:{},密码 {}",dataSourceInfo.getUrl(),dataSourceInfo.getUserName(),dataSourceInfo.getPassword());

View File

@ -31,18 +31,18 @@ public class DataSourceService {
@Resource @Resource
private DynamicDataSource dynamicDataSource; private DynamicDataSource dynamicDataSource;
public void addDataSource(DataSourceInfo dataSourceInfo){ // public void addDataSource(DataSourceInfo dataSourceInfo){
addDefineDynamicDataSource(druidDataSourceFactory.create(dataSourceInfo), dataSourceInfo.getKey()); // addDefineDynamicDataSource(druidDataSourceFactory.create(dataSourceInfo), dataSourceInfo.getKey());
} // }
/** /**
* @Description: map * @Description: map
* @Author Dongzl * @Author Dongzl
*/ */
public void addDefineDynamicDataSource(DruidDataSource druidDataSource, String dataSourceName){ // public void addDefineDynamicDataSource(DruidDataSource druidDataSource, String dataSourceName){
Map<Object, Object> defineTargetDataSources = dynamicDataSource.getDefineTargetDataSources(); // Map<Object, Object> defineTargetDataSources = dynamicDataSource.getDefineTargetDataSources();
defineTargetDataSources.put(dataSourceName, druidDataSource); // defineTargetDataSources.put(dataSourceName, druidDataSource);
dynamicDataSource.setTargetDataSources(defineTargetDataSources); // dynamicDataSource.setTargetDataSources(defineTargetDataSources);
dynamicDataSource.afterPropertiesSet(); // dynamicDataSource.afterPropertiesSet();
} // }
} }