pull/5/head
parent
7fea2a9be2
commit
3565a12c8b
@ -0,0 +1,11 @@
|
||||
package com.infincash.statistics.risk;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO;
|
||||
|
||||
public interface RiskService {
|
||||
List<RiskStatsDTO> countRecentRisk();
|
||||
|
||||
int writeRecentRisk(List<RiskStatsDTO> list);
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.infincash.statistics.risk;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.infincash.statistics.risk.mapper.prd.TRiskRuleMapper;
|
||||
import com.infincash.statistics.risk.mapper.stats.TStatsRiskDetailMapper;
|
||||
import com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO;
|
||||
|
||||
@Service
|
||||
public class RiskServiceImpl implements RiskService {
|
||||
|
||||
@Autowired
|
||||
private TRiskRuleMapper readMapper;//这里会报错,但是并不会影响
|
||||
|
||||
@Autowired
|
||||
private TStatsRiskDetailMapper writeMapper;
|
||||
|
||||
@Override
|
||||
public List<RiskStatsDTO> countRecentRisk() {
|
||||
return readMapper.countRiskRule();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int writeRecentRisk(List<RiskStatsDTO> list) {
|
||||
return writeMapper.insertBatch(list);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.infincash.statistics.risk.mapper.prd;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO;
|
||||
|
||||
public interface TRiskRuleMapper {
|
||||
List<RiskStatsDTO> countRiskRule();
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.infincash.statistics.risk.mapper.stats;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO;
|
||||
|
||||
|
||||
public interface TStatsRiskDetailMapper {
|
||||
int insertBatch(List<RiskStatsDTO> list);
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.infincash.statistics.risk.table.prd.extend;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class RiskStatsDTO {
|
||||
String riskRuleId;
|
||||
int count;
|
||||
Date time;
|
||||
|
||||
public Date getTime() {
|
||||
return time;
|
||||
}
|
||||
public void setTime(Date time) {
|
||||
this.time = time;
|
||||
}
|
||||
public String getRiskRuleId() {
|
||||
return riskRuleId;
|
||||
}
|
||||
public void setRiskRuleId(String riskRuleId) {
|
||||
this.riskRuleId = riskRuleId;
|
||||
}
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.xxl.job.executor.core.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceConfig {
|
||||
|
||||
@Bean(name = "prd")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.prd") // application.properteis中对应属性的前缀
|
||||
public DataSource dataSource1() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Bean(name = "stats")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.stats") // application.properteis中对应属性的前缀
|
||||
public DataSource dataSource2() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xxl.job.executor.core.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = {"com.infincash.statistics.risk.mapper.prd"}, sqlSessionFactoryRef = "sqlSessionFactoryPrd")
|
||||
public class PrdDbConfig {
|
||||
static final String MAPPER_LOCATION = "classpath:mapping/prd/*.xml";
|
||||
|
||||
@Autowired
|
||||
@Qualifier("prd")
|
||||
private DataSource prd;
|
||||
|
||||
@Bean
|
||||
public SqlSessionFactory sqlSessionFactoryPrd() throws Exception {
|
||||
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
||||
factoryBean.setDataSource(prd);
|
||||
factoryBean.setMapperLocations(
|
||||
new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION)
|
||||
);
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionTemplate sqlSessionTemplatePrd() throws Exception {
|
||||
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryPrd());
|
||||
return template;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.xxl.job.executor.core.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = {"com.infincash.statistics.risk.mapper.stats"}, sqlSessionFactoryRef = "sqlSessionFactoryStats")
|
||||
public class StatsDbConfig {
|
||||
static final String MAPPER_LOCATION = "classpath:mapping/stats/*.xml";
|
||||
|
||||
@Autowired
|
||||
@Qualifier("stats")
|
||||
private DataSource stats;
|
||||
|
||||
@Bean
|
||||
public SqlSessionFactory sqlSessionFactoryStats() throws Exception {
|
||||
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
|
||||
factoryBean.setDataSource(stats);
|
||||
factoryBean.setMapperLocations(
|
||||
new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION)
|
||||
);
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SqlSessionTemplate sqlSessionTemplateStats() throws Exception {
|
||||
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryStats()); // 使用上面配置的Factory
|
||||
return template;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.xxl.job.executor.core.config;
|
||||
|
||||
import com.xxl.job.core.executor.XxlJobExecutor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* xxl-job config
|
||||
*
|
||||
* @author xuxueli 2017-04-28
|
||||
*/
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.xxl.job.executor.service.jobhandler,com.infincash.*")
|
||||
public class XxlJobConfig {
|
||||
private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
|
||||
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${xxl.job.executor.appname}")
|
||||
private String appName;
|
||||
|
||||
@Value("${xxl.job.executor.ip}")
|
||||
private String ip;
|
||||
|
||||
@Value("${xxl.job.executor.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${xxl.job.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${xxl.job.executor.logpath}")
|
||||
private String logPath;
|
||||
|
||||
@Value("${xxl.job.executor.logretentiondays}")
|
||||
private int logRetentionDays;
|
||||
|
||||
|
||||
@Bean(initMethod = "start", destroyMethod = "destroy")
|
||||
public XxlJobExecutor xxlJobExecutor() {
|
||||
logger.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
|
||||
xxlJobExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobExecutor.setAppName(appName);
|
||||
xxlJobExecutor.setIp(ip);
|
||||
xxlJobExecutor.setPort(port);
|
||||
xxlJobExecutor.setAccessToken(accessToken);
|
||||
xxlJobExecutor.setLogPath(logPath);
|
||||
xxlJobExecutor.setLogRetentionDays(logRetentionDays);
|
||||
|
||||
return xxlJobExecutor;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
//package com.xxl.job.executor.mvc.controller;
|
||||
//
|
||||
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
//import org.springframework.stereotype.Controller;
|
||||
//import org.springframework.web.bind.annotation.RequestMapping;
|
||||
//import org.springframework.web.bind.annotation.ResponseBody;
|
||||
//
|
||||
//@Controller
|
||||
//@EnableAutoConfiguration
|
||||
//public class IndexController {
|
||||
//
|
||||
// @RequestMapping("/")
|
||||
// @ResponseBody
|
||||
// String index() {
|
||||
// return "xxl job executor running.";
|
||||
// }
|
||||
//
|
||||
//}
|
@ -0,0 +1,39 @@
|
||||
package com.xxl.job.executor.service.jobhandler;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.annotation.JobHandler;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import com.xxl.job.core.util.ShardingUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
/**
|
||||
* 分片广播任务
|
||||
*
|
||||
* @author xuxueli 2017-07-25 20:56:50
|
||||
*/
|
||||
@JobHandler(value="shardingJobHandler")
|
||||
@Service
|
||||
public class ShardingJobHandler extends IJobHandler {
|
||||
|
||||
@Override
|
||||
public ReturnT<String> execute(String param) throws Exception {
|
||||
|
||||
// 分片参数
|
||||
ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
|
||||
XxlJobLogger.log("分片参数:当前分片序号 = {0}, 总分片数 = {1}", shardingVO.getIndex(), shardingVO.getTotal());
|
||||
|
||||
// 业务逻辑
|
||||
for (int i = 0; i < shardingVO.getTotal(); i++) {
|
||||
if (i == shardingVO.getIndex()) {
|
||||
XxlJobLogger.log("第 {0} 片, 命中分片开始处理", i);
|
||||
} else {
|
||||
XxlJobLogger.log("第 {0} 片, 忽略", i);
|
||||
}
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
spring.datasource.prd.url=jdbc:mysql://172.16.16.98:3306/microfinance?autoReconnect=true&useUnicode=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&rewriteBatchedStatements=true&useSSL=false
|
||||
spring.datasource.prd.username=jobopr
|
||||
spring.datasource.prd.password=jobopr666
|
||||
spring.datasource.prd.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
spring.datasource.stats.url=jdbc:mysql://172.16.16.99:3306/microfinance1?autoReconnect=true&useUnicode=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&rewriteBatchedStatements=true&useSSL=false
|
||||
spring.datasource.stats.username=jobopr
|
||||
spring.datasource.stats.password=jobopr666
|
||||
spring.datasource.stats.driver-class-name=com.mysql.jdbc.Driver
|
||||
|
||||
# multi datasource no need declare here
|
||||
#mybatis.mapper-locations=classpath:mapping/*.xml
|
||||
#mybatis.type-aliases-package=com.infincash.statistics.risk.table
|
||||
# web port
|
||||
server.port=9001
|
||||
|
||||
# log config
|
||||
logging.config=classpath:logback.xml
|
||||
|
||||
|
||||
### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
|
||||
xxl.job.admin.addresses=http://127.0.0.1:8090
|
||||
#xxl.job.admin.addresses=http://dispatch-center.infincash.com
|
||||
|
||||
### xxl-job executor address
|
||||
xxl.job.executor.appname=executor-001
|
||||
xxl.job.executor.ip=
|
||||
xxl.job.executor.port=9002
|
||||
|
||||
### xxl-job, access token
|
||||
xxl.job.accessToken=
|
||||
|
||||
### xxl-job log path
|
||||
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
|
||||
### xxl-job log retention days
|
||||
xxl.job.executor.logretentiondays=-1
|
@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration debug="false" scan="true" scanPeriod="1 seconds">
|
||||
|
||||
<contextName>logback</contextName>
|
||||
<property name="log.path" value="/data/applogs/xxl-job/xxl-job-executor-sample-springboot.log"/>
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter" >
|
||||
<level>WARN</level>
|
||||
</filter>-->
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>${log.path}.%d{yyyy-MM-dd}.zip</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>%date %level [%thread] %logger{36} [%file : %line] %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console"/>
|
||||
<appender-ref ref="file"/>
|
||||
</root>
|
||||
|
||||
<!--<logger name="com.xxl.job.executor.service.info" level="WARN" additivity="false">
|
||||
<appender-ref ref="console"/>
|
||||
<appender-ref ref="file"/>
|
||||
</logger>-->
|
||||
|
||||
</configuration>
|
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.infincash.statistics.risk.mapper.prd.TRiskRuleMapper">
|
||||
<resultMap id="riskStatsDTOMap" type="com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO">
|
||||
<result property="riskRuleId" column="risk_rule_id" />
|
||||
<result property="count" column="count1" />
|
||||
<result property="time" column="time" />
|
||||
</resultMap>
|
||||
<!-- id, risk_rule_id, count(1) as count1, now() as time resultType="com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO" -->
|
||||
<select id="countRiskRule" resultMap="riskStatsDTOMap" >
|
||||
<![CDATA[
|
||||
select
|
||||
risk_rule_id, count(1) as count1, now() as time
|
||||
from t_user_risk
|
||||
where user_id in (select user_id from t_user where DATE_SUB(NOW(),INTERVAL 1 HOUR) <= register_time)
|
||||
GROUP by risk_rule_id order by risk_rule_id;
|
||||
]]>
|
||||
</select>
|
||||
</mapper>
|
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.infincash.statistics.risk.mapper.stats.TStatsRiskDetailMapper">
|
||||
<resultMap id="BaseResultMap" type="com.infincash.statistics.risk.table.prd.extend.RiskStatsDTO">
|
||||
<result property="riskRuleId" column="risk_rule_id" />
|
||||
<result property="count" column="count1" />
|
||||
<result property="time" column="time" />
|
||||
</resultMap>
|
||||
|
||||
<insert id="insertBatch" parameterType="java.util.List">
|
||||
insert into t_stats_risk_detail
|
||||
(stats_time, a_hour_count, risk_rule_id)
|
||||
VALUES
|
||||
<foreach collection ="list" item="oneItem" index= "index" separator =",">
|
||||
(
|
||||
#{oneItem.time}, #{oneItem.count},#{oneItem.riskRuleId}
|
||||
)
|
||||
</foreach >
|
||||
</insert>
|
||||
</mapper>
|
@ -0,0 +1,24 @@
|
||||
package com.xxl.job.executor.test;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.xxl.job.executor.Application;
|
||||
import com.xxl.job.executor.service.jobhandler.RiskCountStatisticsJobHandler;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class ,webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class XxlJobExecutorExampleBootApplicationTests {
|
||||
|
||||
@Autowired
|
||||
RiskCountStatisticsJobHandler jobHandler;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
jobHandler.execute("");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue