diff --git a/rights-client-controller/src/main/java/com/cyjd/rights/config/RedisConfig.java b/rights-client-controller/src/main/java/com/cyjd/rights/config/RedisConfig.java new file mode 100644 index 0000000..ec4c963 --- /dev/null +++ b/rights-client-controller/src/main/java/com/cyjd/rights/config/RedisConfig.java @@ -0,0 +1,135 @@ +package com.cyjd.rights.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; +import org.springframework.scheduling.annotation.EnableScheduling; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +/** + * @author jhf + * @date 2020/3/23 12:06 + */ +@Configuration +@EnableCaching//启用缓存,这个注解很重要 +@EnableScheduling // ; +public class RedisConfig extends CachingConfigurerSupport { + + // 失效时间 +// @Value("${spring.cache.redis.time-to-live}") + private Duration timeToLive = Duration.ofHours(12); + + @Value("${spring.redis.host}") + private String host; + + @Value("${spring.redis.port}") + private int port; + + // @Value("${spring.redis.timeout}") + private int timeout = 1000; + + @Value("${spring.redis.jedis.pool.max-idle}") + private int maxIdle; + + @Value("${spring.redis.jedis.pool.max-wait}") + private long maxWaitMillis; + + @Value("${spring.redis.password}") + private String password; + + @Value("${spring.redis.block-when-exhausted}") + private boolean blockWhenExhausted; + + @Bean + public JedisPool redisPoolFactory() throws Exception{ + JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); + // 是否启用pool的jmx管理功能, 默认true + jedisPoolConfig.setMaxIdle(1000); + jedisPoolConfig.setMaxWaitMillis(3000); + // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true + jedisPoolConfig.setBlockWhenExhausted(true); + jedisPoolConfig.setTestOnBorrow(true); + jedisPoolConfig.setTestWhileIdle(true); + jedisPoolConfig.setMinEvictableIdleTimeMillis(60000); + jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000); + jedisPoolConfig.setNumTestsPerEvictionRun(-1); + jedisPoolConfig.setMaxTotal(1200); + jedisPoolConfig.setTestOnReturn(true); + // 是否启用pool的jmx管理功能, 默认true + jedisPoolConfig.setJmxEnabled(true); + + + JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); + return jedisPool; + } + + @Bean(name = "redisTemplate") + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(redisConnectionFactory); + //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); + ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(mapper); + template.setValueSerializer(jackson2JsonRedisSerializer); + + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + //使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(stringRedisSerializer); + // template.setKeySerializer(stringRedisSerializer); + // hash的key也采用String的序列化方式 + template.setHashKeySerializer(stringRedisSerializer); + // value序列化方式采用jackson + template.setValueSerializer(jackson2JsonRedisSerializer); + // hash的value序列化方式采用jackson + template.setHashValueSerializer(jackson2JsonRedisSerializer); + template.afterPropertiesSet(); + //设置缓存过期时间 + template.expire("",3600 * 12L, TimeUnit.SECONDS); + return template; + } + @Bean + public CacheManager cacheManager(RedisConnectionFactory factory) { + RedisSerializer redisSerializer = new StringRedisSerializer(); + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + + //解决查询缓存转换异常的问题 + ObjectMapper om = new ObjectMapper(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(om); + + // 配置序列化(解决乱码的问题) + RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(timeToLive) + .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) + .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) + .disableCachingNullValues(); + + RedisCacheManager cacheManager = RedisCacheManager.builder(factory) + .cacheDefaults(config) + .build(); + return cacheManager; + } +} + diff --git a/rights-client-controller/src/main/java/com/cyjd/rights/controller/AliPayController.java b/rights-client-controller/src/main/java/com/cyjd/rights/controller/AliPayController.java index 8497a6d..d08ac30 100644 --- a/rights-client-controller/src/main/java/com/cyjd/rights/controller/AliPayController.java +++ b/rights-client-controller/src/main/java/com/cyjd/rights/controller/AliPayController.java @@ -470,15 +470,15 @@ public class AliPayController { aliPayOrderService.save(aliPayOrderEntity); log.info("支付订单保存成功"+aliPayOrderEntity); //查看是否发送 - if (sendSuccessMobileService.ifSendDataByType(aliPaySigningOrderEntity.getLinkId().toString())) { + if (sendSuccessMobileService.ifSendDataByType(aliPaySigningOrderEntity.getLinkId().toString(),outTradeNo)) { //回传 1161张敏渠道 if (1161==aliPaySigningOrderEntity.getLinkId()){ //回传成功数据 - sendSuccessMobileService.sendSuccessMobile(aliPaySigningOrderEntity.getMobile(),"zm"); + sendSuccessMobileService.sendSuccessMobile(aliPaySigningOrderEntity.getMobile(),"zm",outTradeNo); } //回传 1160推啊渠道 if (1160==aliPaySigningOrderEntity.getLinkId()){ - sendSuccessMobileService.sendSuccessMobile(aliPaySigningOrderEntity.getSendApiData(),"tuia"); + sendSuccessMobileService.sendSuccessMobile(aliPaySigningOrderEntity.getSendApiData(),"tuia",outTradeNo); } } // if (b){ @@ -521,7 +521,7 @@ public class AliPayController { // @ApiOperation(value = "test", notes = "test") // @PostMapping("/test") // public void tess() { -// for (int i = 0; i < 10; i++) { +// for (int i = 0; i < 20; i++) { // if (sendSuccessMobileService.ifSendDataByType("1059")) { // System.out.println("发送"); // }else { diff --git a/rights-interface/src/main/java/com/cyjd/rights/business/service/SendSuccessMobileService.java b/rights-interface/src/main/java/com/cyjd/rights/business/service/SendSuccessMobileService.java index 290077a..6cf1ff1 100644 --- a/rights-interface/src/main/java/com/cyjd/rights/business/service/SendSuccessMobileService.java +++ b/rights-interface/src/main/java/com/cyjd/rights/business/service/SendSuccessMobileService.java @@ -5,13 +5,14 @@ public interface SendSuccessMobileService { * 回传成功的号码 * @param mobile 手机号 * @param channelName 渠道名称 + * @param outTradeNo 签约号 */ - void sendSuccessMobile(String mobile,String channelName); + void sendSuccessMobile(String mobile,String channelName,String outTradeNo); /** * 判断是否发送核减 * @param linkId 链接Id * @return */ - boolean ifSendDataByType(String linkId); + boolean ifSendDataByType(String linkId,String outTradeNo); } diff --git a/rights-server/src/main/java/com/cyjd/rights/business/service/impl/SendSuccessMobileServiceImpl.java b/rights-server/src/main/java/com/cyjd/rights/business/service/impl/SendSuccessMobileServiceImpl.java index 5bc95e6..06fff17 100644 --- a/rights-server/src/main/java/com/cyjd/rights/business/service/impl/SendSuccessMobileServiceImpl.java +++ b/rights-server/src/main/java/com/cyjd/rights/business/service/impl/SendSuccessMobileServiceImpl.java @@ -24,7 +24,7 @@ public class SendSuccessMobileServiceImpl implements SendSuccessMobileService { private RedisUtil redisUtil; private String baseUrl="http://47.107.84.195:666/manage/business/api/notificationApi/cyCall"; @Override - public void sendSuccessMobile(String mobile, String channelName) { + public void sendSuccessMobile(String mobile, String channelName,String outTradeNo) { if ("zm".equals(channelName)){ HashMap params = new HashMap<>(); params.put("mobile",mobile); @@ -34,11 +34,16 @@ public class SendSuccessMobileServiceImpl implements SendSuccessMobileService { String url="https://activity.tuia.cn/log/effect/v2?a_oId="+mobile+"&advertKey=75FEC8A0D38E22BE3B9695460D1A2BB6&subType=3"; HttpUtils.sendGet(url); } + //设置一个签约号只能回传一次 + redisUtil.set("sendSuccessMobile"+outTradeNo,1,172800); } @Override - public boolean ifSendDataByType(String linkId) { + public boolean ifSendDataByType(String linkId,String outTradeNo) { + if (redisUtil.hasKey("sendSuccessMobile"+outTradeNo)){ + return false; + } ConfigEntity configEntity = configService.getOne(new QueryWrapper().eq("config_name", "hj")); if (configEntity==null){ return false; @@ -49,12 +54,15 @@ public class SendSuccessMobileServiceImpl implements SendSuccessMobileService { public boolean nuclearSubtractionByLinkId(Integer nuclearSubtraction, String linkId) { + + //核减 String key="hejian"+linkId; Integer number= 10; double delNumber=number-nuclearSubtraction; boolean flag=true; + System.out.println(redisUtil.get(key)); long fujianhejian = redisUtil.incr(key, 1);