From e50457892d1eaab957826513423dc88d74d2203c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Forever=E6=9D=A8?= <453190450@qq.com> Date: Sun, 23 Apr 2023 13:21:03 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E5=B9=B3=E5=8F=B0=E9=83=A8=E5=88=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E5=B9=B6=E5=AF=B9=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=96=B0=E5=A2=9E=E5=A4=9A=E7=A7=8D=20redis=20?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E5=AE=9E=E7=8E=B0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx-java-cp-spring-boot-starter/README.md | 2 +- .../wx-java-cp-spring-boot-starter/pom.xml | 12 ++ .../config/WxCpStorageAutoConfiguration.java | 8 +- .../wxjava/cp/properties/WxCpProperties.java | 26 ++- .../cp/properties/WxCpRedisProperties.java | 46 +++++ ...bstractWxCpConfigStorageConfiguration.java | 6 +- ...WxCpInJedisConfigStorageConfiguration.java | 74 +++++++ ...disTemplateConfigStorageConfiguration.java | 41 ++++ ...pInRedissonConfigStorageConfiguration.java | 65 ++++++ .../wx-java-open-spring-boot-starter/pom.xml | 4 - ...OpenInJedisConfigStorageConfiguration.java | 13 +- ...nInRedissonConfigStorageConfiguration.java | 17 +- .../open/properties/WxOpenProperties.java | 4 +- ...erties.java => WxOpenRedisProperties.java} | 2 +- weixin-java-cp/pom.xml | 4 + .../impl/AbstractWxCpInRedisConfigImpl.java | 183 +++++++++++++++++ .../cp/config/impl/WxCpJedisConfigImpl.java | 24 +++ .../impl/WxCpRedisTemplateConfigImpl.java | 23 +++ .../config/impl/WxCpRedissonConfigImpl.java | 185 +----------------- 19 files changed, 527 insertions(+), 212 deletions(-) create mode 100644 spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpRedisProperties.java create mode 100644 spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInJedisConfigStorageConfiguration.java create mode 100644 spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedisTemplateConfigStorageConfiguration.java create mode 100644 spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedissonConfigStorageConfiguration.java rename spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/{RedisProperties.java => WxOpenRedisProperties.java} (91%) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/AbstractWxCpInRedisConfigImpl.java create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpJedisConfigImpl.java create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisTemplateConfigImpl.java diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/README.md b/spring-boot-starters/wx-java-cp-spring-boot-starter/README.md index 439ee5c72..a10ef65bc 100644 --- a/spring-boot-starters/wx-java-cp-spring-boot-starter/README.md +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/README.md @@ -20,7 +20,7 @@ wx.cp.aes-key = @aes-key wx.cp.agent-id = @agent-id # ConfigStorage 配置(选填) - wx.cp.config-storage.type=memory # memory 默认,目前只支持 memory 类型,可以自行扩展 redis 等类型 + wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate # http 客户端配置(选填) wx.cp.config-storage.http-proxy-host= wx.cp.config-storage.http-proxy-port= diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/pom.xml b/spring-boot-starters/wx-java-cp-spring-boot-starter/pom.xml index a37810495..e19567517 100644 --- a/spring-boot-starters/wx-java-cp-spring-boot-starter/pom.xml +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/pom.xml @@ -18,6 +18,18 @@ weixin-java-cp ${project.version} + + redis.clients + jedis + + + org.redisson + redisson + + + org.springframework.data + spring-data-redis + diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpStorageAutoConfiguration.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpStorageAutoConfiguration.java index ac17c8097..1c7d80b84 100644 --- a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpStorageAutoConfiguration.java +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/config/WxCpStorageAutoConfiguration.java @@ -1,6 +1,9 @@ package com.binarywang.spring.starter.wxjava.cp.config; +import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInJedisConfigStorageConfiguration; import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInMemoryConfigStorageConfiguration; +import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInRedisTemplateConfigStorageConfiguration; +import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInRedissonConfigStorageConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -12,7 +15,10 @@ import org.springframework.context.annotation.Import; */ @Configuration @Import({ - WxCpInMemoryConfigStorageConfiguration.class + WxCpInMemoryConfigStorageConfiguration.class, + WxCpInJedisConfigStorageConfiguration.class, + WxCpInRedissonConfigStorageConfiguration.class, + WxCpInRedisTemplateConfigStorageConfiguration.class }) public class WxCpStorageAutoConfiguration { } diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpProperties.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpProperties.java index 681f157b4..b87ddc245 100644 --- a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpProperties.java +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpProperties.java @@ -3,6 +3,7 @@ package com.binarywang.spring.starter.wxjava.cp.properties; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; import java.io.Serializable; @@ -61,6 +62,17 @@ public class WxCpProperties { */ private StorageType type = StorageType.memory; + /** + * 指定key前缀 + */ + private String keyPrefix = "wx:cp"; + + /** + * redis连接配置 + */ + @NestedConfigurationProperty + private WxCpRedisProperties redis = new WxCpRedisProperties(); + /** * http代理主机 */ @@ -104,6 +116,18 @@ public class WxCpProperties { /** * 内存 */ - memory + memory, + /** + * jedis + */ + jedis, + /** + * redisson + */ + redisson, + /** + * redistemplate + */ + redistemplate } } diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpRedisProperties.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpRedisProperties.java new file mode 100644 index 000000000..63a7fe01e --- /dev/null +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpRedisProperties.java @@ -0,0 +1,46 @@ +package com.binarywang.spring.starter.wxjava.cp.properties; + +import lombok.Data; + +import java.io.Serializable; + +/** + * Redis配置. + * + * @author yl + * created on 2023/04/23 + */ +@Data +public class WxCpRedisProperties implements Serializable { + private static final long serialVersionUID = -5924815351660074401L; + + /** + * 主机地址. + */ + private String host; + + /** + * 端口号. + */ + private int port = 6379; + + /** + * 密码. + */ + private String password; + + /** + * 超时. + */ + private int timeout = 2000; + + /** + * 数据库. + */ + private int database = 0; + + private Integer maxActive; + private Integer maxIdle; + private Integer maxWaitMillis; + private Integer minIdle; +} diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/AbstractWxCpConfigStorageConfiguration.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/AbstractWxCpConfigStorageConfiguration.java index f47b2c0f2..0f2995e96 100644 --- a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/AbstractWxCpConfigStorageConfiguration.java +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/AbstractWxCpConfigStorageConfiguration.java @@ -15,8 +15,8 @@ public abstract class AbstractWxCpConfigStorageConfiguration { protected WxCpDefaultConfigImpl config(WxCpDefaultConfigImpl config, WxCpProperties properties) { String corpId = properties.getCorpId(); String corpSecret = properties.getCorpSecret(); - String token = properties.getToken(); Integer agentId = properties.getAgentId(); + String token = properties.getToken(); String aesKey = properties.getAesKey(); // 企业微信,私钥,会话存档路径 String msgAuditPriKey = properties.getMsgAuditPriKey(); @@ -24,12 +24,10 @@ public abstract class AbstractWxCpConfigStorageConfiguration { config.setCorpId(corpId); config.setCorpSecret(corpSecret); + config.setAgentId(agentId); if (StringUtils.isNotBlank(token)) { config.setToken(token); } - if (agentId != null) { - config.setAgentId(agentId); - } if (StringUtils.isNotBlank(aesKey)) { config.setAesKey(aesKey); } diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInJedisConfigStorageConfiguration.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInJedisConfigStorageConfiguration.java new file mode 100644 index 000000000..75980c361 --- /dev/null +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInJedisConfigStorageConfiguration.java @@ -0,0 +1,74 @@ +package com.binarywang.spring.starter.wxjava.cp.storage; + +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties; +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpRedisProperties; +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; +import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl; +import org.apache.commons.lang3.StringUtils; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +/** + * 自动装配基于 jedis 策略配置 + * + * @author yl + * created on 2023/04/23 + */ +@Configuration +@ConditionalOnProperty( + prefix = WxCpProperties.PREFIX + ".config-storage", name = "type", havingValue = "jedis" +) +@RequiredArgsConstructor +public class WxCpInJedisConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration { + private final WxCpProperties wxCpProperties; + private final ApplicationContext applicationContext; + + @Bean + @ConditionalOnMissingBean(WxCpConfigStorage.class) + public WxCpConfigStorage wxOpenConfigStorage() { + WxCpDefaultConfigImpl config = getConfigStorage(); + return this.config(config, wxCpProperties); + } + + private WxCpJedisConfigImpl getConfigStorage() { + WxCpRedisProperties wxCpRedisProperties = wxCpProperties.getConfigStorage().getRedis(); + JedisPool jedisPool; + if (wxCpRedisProperties != null && StringUtils.isNotEmpty(wxCpRedisProperties.getHost())) { + jedisPool = getJedisPool(); + } else { + jedisPool = applicationContext.getBean(JedisPool.class); + } + return new WxCpJedisConfigImpl(jedisPool, wxCpProperties.getConfigStorage().getKeyPrefix()); + } + + private JedisPool getJedisPool() { + WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage(); + WxCpRedisProperties redis = storage.getRedis(); + + JedisPoolConfig config = new JedisPoolConfig(); + if (redis.getMaxActive() != null) { + config.setMaxTotal(redis.getMaxActive()); + } + if (redis.getMaxIdle() != null) { + config.setMaxIdle(redis.getMaxIdle()); + } + if (redis.getMaxWaitMillis() != null) { + config.setMaxWaitMillis(redis.getMaxWaitMillis()); + } + if (redis.getMinIdle() != null) { + config.setMinIdle(redis.getMinIdle()); + } + config.setTestOnBorrow(true); + config.setTestWhileIdle(true); + + return new JedisPool(config, redis.getHost(), redis.getPort(), + redis.getTimeout(), redis.getPassword(), redis.getDatabase()); + } +} diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedisTemplateConfigStorageConfiguration.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedisTemplateConfigStorageConfiguration.java new file mode 100644 index 000000000..46efa050a --- /dev/null +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedisTemplateConfigStorageConfiguration.java @@ -0,0 +1,41 @@ +package com.binarywang.spring.starter.wxjava.cp.storage; + +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties; +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; +import me.chanjar.weixin.cp.config.impl.WxCpRedisTemplateConfigImpl; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.StringRedisTemplate; + +/** + * 自动装配基于 redisTemplate 策略配置 + * + * @author yl + * created on 2023/04/23 + */ +@Configuration +@ConditionalOnProperty( + prefix = WxCpProperties.PREFIX + ".config-storage", name = "type", havingValue = "redistemplate" +) +@RequiredArgsConstructor +public class WxCpInRedisTemplateConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration { + private final WxCpProperties wxCpProperties; + private final ApplicationContext applicationContext; + + @Bean + @ConditionalOnMissingBean(WxCpConfigStorage.class) + public WxCpConfigStorage wxOpenConfigStorage() { + WxCpDefaultConfigImpl config = getConfigStorage(); + return this.config(config, wxCpProperties); + } + + private WxCpRedisTemplateConfigImpl getConfigStorage() { + StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); + return new WxCpRedisTemplateConfigImpl(redisTemplate, wxCpProperties.getConfigStorage().getKeyPrefix()); + } +} diff --git a/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedissonConfigStorageConfiguration.java b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedissonConfigStorageConfiguration.java new file mode 100644 index 000000000..6f3273006 --- /dev/null +++ b/spring-boot-starters/wx-java-cp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/storage/WxCpInRedissonConfigStorageConfiguration.java @@ -0,0 +1,65 @@ +package com.binarywang.spring.starter.wxjava.cp.storage; + +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties; +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpRedisProperties; +import lombok.RequiredArgsConstructor; +import me.chanjar.weixin.cp.config.WxCpConfigStorage; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; +import me.chanjar.weixin.cp.config.impl.WxCpRedissonConfigImpl; +import org.apache.commons.lang3.StringUtils; +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; +import org.redisson.config.TransportMode; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 自动装配基于 redisson 策略配置 + * + * @author yl + * created on 2023/04/23 + */ +@Configuration +@ConditionalOnProperty( + prefix = WxCpProperties.PREFIX + ".config-storage", name = "type", havingValue = "redisson" +) +@RequiredArgsConstructor +public class WxCpInRedissonConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration { + private final WxCpProperties wxCpProperties; + private final ApplicationContext applicationContext; + + @Bean + @ConditionalOnMissingBean(WxCpConfigStorage.class) + public WxCpConfigStorage wxOpenConfigStorage() { + WxCpDefaultConfigImpl config = getConfigStorage(); + return this.config(config, wxCpProperties); + } + + private WxCpRedissonConfigImpl getConfigStorage() { + WxCpRedisProperties redisProperties = wxCpProperties.getConfigStorage().getRedis(); + RedissonClient redissonClient; + if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { + redissonClient = getRedissonClient(); + } else { + redissonClient = applicationContext.getBean(RedissonClient.class); + } + return new WxCpRedissonConfigImpl(redissonClient, wxCpProperties.getConfigStorage().getKeyPrefix()); + } + + private RedissonClient getRedissonClient() { + WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage(); + WxCpRedisProperties redis = storage.getRedis(); + + Config config = new Config(); + config.useSingleServer() + .setAddress("redis://" + redis.getHost() + ":" + redis.getPort()) + .setDatabase(redis.getDatabase()) + .setPassword(redis.getPassword()); + config.setTransportMode(TransportMode.NIO); + return Redisson.create(config); + } +} diff --git a/spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml b/spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml index fd714e9b5..a8f3a111c 100644 --- a/spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml +++ b/spring-boot-starters/wx-java-open-spring-boot-starter/pom.xml @@ -22,18 +22,14 @@ redis.clients jedis - provided org.redisson redisson - provided org.springframework.data spring-data-redis - ${spring.boot.version} - provided diff --git a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInJedisConfigStorageConfiguration.java b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInJedisConfigStorageConfiguration.java index 353b670e6..73a0183d7 100644 --- a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInJedisConfigStorageConfiguration.java +++ b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInJedisConfigStorageConfiguration.java @@ -1,10 +1,8 @@ package com.binarywang.spring.starter.wxjava.open.config.storage; -import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties; import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; +import com.binarywang.spring.starter.wxjava.open.properties.WxOpenRedisProperties; import lombok.RequiredArgsConstructor; -import me.chanjar.weixin.common.redis.JedisWxRedisOps; -import me.chanjar.weixin.common.redis.WxRedisOps; import me.chanjar.weixin.open.api.WxOpenConfigStorage; import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage; import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage; @@ -39,20 +37,19 @@ public class WxOpenInJedisConfigStorageConfiguration extends AbstractWxOpenConfi } private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() { - RedisProperties redisProperties = properties.getConfigStorage().getRedis(); + WxOpenRedisProperties wxOpenRedisProperties = properties.getConfigStorage().getRedis(); JedisPool jedisPool; - if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { + if (wxOpenRedisProperties != null && StringUtils.isNotEmpty(wxOpenRedisProperties.getHost())) { jedisPool = getJedisPool(); } else { jedisPool = applicationContext.getBean(JedisPool.class); } - WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); - return new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); + return new WxOpenInRedisConfigStorage(jedisPool, properties.getConfigStorage().getKeyPrefix()); } private JedisPool getJedisPool() { WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); - RedisProperties redis = storage.getRedis(); + WxOpenRedisProperties redis = storage.getRedis(); JedisPoolConfig config = new JedisPoolConfig(); if (redis.getMaxActive() != null) { diff --git a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedissonConfigStorageConfiguration.java b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedissonConfigStorageConfiguration.java index 85aa1d20e..ea1dce367 100644 --- a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedissonConfigStorageConfiguration.java +++ b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/storage/WxOpenInRedissonConfigStorageConfiguration.java @@ -1,13 +1,11 @@ package com.binarywang.spring.starter.wxjava.open.config.storage; -import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties; import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties; +import com.binarywang.spring.starter.wxjava.open.properties.WxOpenRedisProperties; import lombok.RequiredArgsConstructor; -import me.chanjar.weixin.common.redis.RedissonWxRedisOps; -import me.chanjar.weixin.common.redis.WxRedisOps; import me.chanjar.weixin.open.api.WxOpenConfigStorage; import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage; -import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage; +import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage; import org.apache.commons.lang3.StringUtils; import org.redisson.Redisson; import org.redisson.api.RedissonClient; @@ -40,21 +38,20 @@ public class WxOpenInRedissonConfigStorageConfiguration extends AbstractWxOpenCo return this.config(config, properties); } - private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() { - RedisProperties redisProperties = properties.getConfigStorage().getRedis(); + private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() { + WxOpenRedisProperties wxOpenRedisProperties = properties.getConfigStorage().getRedis(); RedissonClient redissonClient; - if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { + if (wxOpenRedisProperties != null && StringUtils.isNotEmpty(wxOpenRedisProperties.getHost())) { redissonClient = getRedissonClient(); } else { redissonClient = applicationContext.getBean(RedissonClient.class); } - WxRedisOps redisOps = new RedissonWxRedisOps(redissonClient); - return new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix()); + return new WxOpenInRedissonConfigStorage(redissonClient, properties.getConfigStorage().getKeyPrefix()); } private RedissonClient getRedissonClient() { WxOpenProperties.ConfigStorage storage = properties.getConfigStorage(); - RedisProperties redis = storage.getRedis(); + WxOpenRedisProperties redis = storage.getRedis(); Config config = new Config(); config.useSingleServer() diff --git a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenProperties.java b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenProperties.java index adb35c2fa..641c57b00 100644 --- a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenProperties.java +++ b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenProperties.java @@ -58,13 +58,13 @@ public class WxOpenProperties { /** * 指定key前缀. */ - private String keyPrefix = "wx"; + private String keyPrefix = "wx:open"; /** * redis连接配置. */ @NestedConfigurationProperty - private RedisProperties redis = new RedisProperties(); + private WxOpenRedisProperties redis = new WxOpenRedisProperties(); /** * http客户端类型. diff --git a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/RedisProperties.java b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenRedisProperties.java similarity index 91% rename from spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/RedisProperties.java rename to spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenRedisProperties.java index a03d3a47f..0aafc73da 100644 --- a/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/RedisProperties.java +++ b/spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenRedisProperties.java @@ -10,7 +10,7 @@ import java.io.Serializable; * @author someone */ @Data -public class RedisProperties implements Serializable { +public class WxOpenRedisProperties implements Serializable { private static final long serialVersionUID = -5924815351660074401L; /** diff --git a/weixin-java-cp/pom.xml b/weixin-java-cp/pom.xml index 12408ed86..7573d72e5 100644 --- a/weixin-java-cp/pom.xml +++ b/weixin-java-cp/pom.xml @@ -43,6 +43,10 @@ org.redisson redisson + + org.springframework.data + spring-data-redis + org.testng testng diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/AbstractWxCpInRedisConfigImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/AbstractWxCpInRedisConfigImpl.java new file mode 100644 index 000000000..780e722c3 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/AbstractWxCpInRedisConfigImpl.java @@ -0,0 +1,183 @@ +package me.chanjar.weixin.cp.config.impl; + +import lombok.NonNull; +import me.chanjar.weixin.common.bean.WxAccessToken; +import me.chanjar.weixin.common.redis.WxRedisOps; +import org.apache.commons.lang3.StringUtils; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Lock; + +/** + * @author yl + * created on 2023/04/23 + */ +public abstract class AbstractWxCpInRedisConfigImpl extends WxCpDefaultConfigImpl { + private static final long serialVersionUID = 7157341535439380615L; + /** + * The constant LOCK_KEY. + */ + protected static final String LOCK_KEY = "wechat_cp_lock:"; + /** + * The constant CP_ACCESS_TOKEN_KEY. + */ + protected static final String CP_ACCESS_TOKEN_KEY = "wechat_cp_access_token_key:"; + /** + * The constant CP_JSAPI_TICKET_KEY. + */ + protected static final String CP_JSAPI_TICKET_KEY = "wechat_cp_jsapi_ticket_key:"; + /** + * The constant CP_AGENT_JSAPI_TICKET_KEY. + */ + protected static final String CP_AGENT_JSAPI_TICKET_KEY = "wechat_cp_agent_jsapi_ticket_key:"; + + /** + * redis 存储的 key 的前缀,可为空 + */ + protected String keyPrefix; + /** + * The Access token key. + */ + protected String accessTokenKey; + /** + * The Jsapi ticket key. + */ + protected String jsapiTicketKey; + /** + * The Agent jsapi ticket key. + */ + protected String agentJsapiTicketKey; + /** + * The Lock key. + */ + protected String lockKey; + + private final WxRedisOps redisOps; + + /** + * Instantiates a new Wx cp redis config. + * + * @param redisOps the redis ops + * @param keyPrefix the key prefix + */ + public AbstractWxCpInRedisConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) { + this.redisOps = redisOps; + this.keyPrefix = keyPrefix; + } + + /** + * 设置企业微信自研应用ID(整数),同时初始化相关的redis key,注意要先调用setCorpId,再调用setAgentId + * + * @param agentId 应用 agentId + */ + @Override + public void setAgentId(Integer agentId) { + super.setAgentId(agentId); + String ukey; + if (agentId != null) { + ukey = getCorpId().concat(":").concat(String.valueOf(agentId)); + } else { + ukey = getCorpId(); + } + String prefix = StringUtils.isBlank(keyPrefix) ? "" : + (StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":")); + lockKey = prefix + LOCK_KEY.concat(ukey); + accessTokenKey = prefix + CP_ACCESS_TOKEN_KEY.concat(ukey); + jsapiTicketKey = prefix + CP_JSAPI_TICKET_KEY.concat(ukey); + agentJsapiTicketKey = prefix + CP_AGENT_JSAPI_TICKET_KEY.concat(ukey); + } + + /** + * Gets lock by key. + * + * @param key the key + * @return the lock by key + */ + protected Lock getLockByKey(String key) { + return redisOps.getLock(key); + } + + @Override + public Lock getAccessTokenLock() { + return getLockByKey(this.lockKey.concat(":").concat("accessToken")); + } + + @Override + public Lock getAgentJsapiTicketLock() { + return getLockByKey(this.lockKey.concat(":").concat("agentJsapiTicket")); + + } + + @Override + public Lock getJsapiTicketLock() { + return getLockByKey(this.lockKey.concat(":").concat("jsapiTicket")); + } + + @Override + public String getAccessToken() { + return redisOps.getValue(this.accessTokenKey); + } + + @Override + public boolean isAccessTokenExpired() { + Long expire = redisOps.getExpire(this.accessTokenKey); + return expire == null || expire < 2; + } + + @Override + public void updateAccessToken(WxAccessToken accessToken) { + redisOps.setValue(this.accessTokenKey, accessToken.getAccessToken(), accessToken.getExpiresIn(), TimeUnit.SECONDS); + } + + @Override + public void updateAccessToken(String accessToken, int expiresInSeconds) { + redisOps.setValue(this.accessTokenKey, accessToken, expiresInSeconds, TimeUnit.SECONDS); + } + + @Override + public void expireAccessToken() { + redisOps.expire(this.accessTokenKey, 0, TimeUnit.SECONDS); + } + + @Override + public String getJsapiTicket() { + return redisOps.getValue(this.jsapiTicketKey); + } + + @Override + public boolean isJsapiTicketExpired() { + Long expire = redisOps.getExpire(this.jsapiTicketKey); + return expire == null || expire < 2; + } + + @Override + public void expireJsapiTicket() { + redisOps.expire(this.jsapiTicketKey, 0, TimeUnit.SECONDS); + } + + @Override + public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { + redisOps.setValue(this.jsapiTicketKey, jsapiTicket, expiresInSeconds, TimeUnit.SECONDS); + } + + @Override + public void expireAgentJsapiTicket() { + redisOps.expire(this.agentJsapiTicketKey, 0, TimeUnit.SECONDS); + } + + @Override + public void updateAgentJsapiTicket(String agentJsapiTicket, int expiresInSeconds) { + redisOps.setValue(this.agentJsapiTicketKey, agentJsapiTicket, expiresInSeconds, TimeUnit.SECONDS); + } + + @Override + public String getAgentJsapiTicket() { + return redisOps.getValue(this.agentJsapiTicketKey); + } + + @Override + public boolean isAgentJsapiTicketExpired() { + Long expire = redisOps.getExpire(this.agentJsapiTicketKey); + return expire == null || expire < 2; + } +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpJedisConfigImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpJedisConfigImpl.java new file mode 100644 index 000000000..39d3bd896 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpJedisConfigImpl.java @@ -0,0 +1,24 @@ +package me.chanjar.weixin.cp.config.impl; + +import lombok.NonNull; +import me.chanjar.weixin.common.redis.JedisWxRedisOps; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.util.Pool; + +/** + * 基于 jdis 的实现 + * + * @author yl + * created on 2023/04/23 + */ +public class WxCpJedisConfigImpl extends AbstractWxCpInRedisConfigImpl { + private static final long serialVersionUID = -1869372247414407433L; + + public WxCpJedisConfigImpl(Pool jedisPool) { + this(jedisPool, null); + } + + public WxCpJedisConfigImpl(@NonNull Pool jedisPool, String keyPrefix) { + super(new JedisWxRedisOps(jedisPool), keyPrefix); + } +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisTemplateConfigImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisTemplateConfigImpl.java new file mode 100644 index 000000000..15e8a6c8e --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisTemplateConfigImpl.java @@ -0,0 +1,23 @@ +package me.chanjar.weixin.cp.config.impl; + +import lombok.NonNull; +import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; +import org.springframework.data.redis.core.StringRedisTemplate; + +/** + * 基于 RedisTemplate 的实现 + * + * @author yl + * created on 2023/04/23 + */ +public class WxCpRedisTemplateConfigImpl extends AbstractWxCpInRedisConfigImpl { + private static final long serialVersionUID = -1660004125413310620L; + + public WxCpRedisTemplateConfigImpl(@NonNull StringRedisTemplate stringRedisTemplate) { + this(stringRedisTemplate, null); + } + + public WxCpRedisTemplateConfigImpl(@NonNull StringRedisTemplate stringRedisTemplate, String keyPrefix) { + super(new RedisTemplateWxRedisOps(stringRedisTemplate), keyPrefix); + } +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedissonConfigImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedissonConfigImpl.java index 62099f031..f21491d04 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedissonConfigImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedissonConfigImpl.java @@ -1,198 +1,23 @@ package me.chanjar.weixin.cp.config.impl; import lombok.NonNull; -import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.redis.RedissonWxRedisOps; -import me.chanjar.weixin.common.redis.WxRedisOps; -import org.apache.commons.lang3.StringUtils; import org.redisson.api.RedissonClient; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.Lock; - /** * 基于Redisson的实现 * * @author yuanqixun created on 2020 /5/13 + * @author yl */ -public class WxCpRedissonConfigImpl extends WxCpDefaultConfigImpl { - /** - * The constant LOCK_KEY. - */ - protected static final String LOCK_KEY = "wechat_cp_lock:"; - /** - * The constant CP_ACCESS_TOKEN_KEY. - */ - protected static final String CP_ACCESS_TOKEN_KEY = "wechat_cp_access_token_key:"; - /** - * The constant CP_JSAPI_TICKET_KEY. - */ - protected static final String CP_JSAPI_TICKET_KEY = "wechat_cp_jsapi_ticket_key:"; - /** - * The constant CP_AGENT_JSAPI_TICKET_KEY. - */ - protected static final String CP_AGENT_JSAPI_TICKET_KEY = "wechat_cp_agent_jsapi_ticket_key:"; - private final WxRedisOps redisOps; - /** - * redis 存储的 key 的前缀,可为空 - */ - protected String keyPrefix; - /** - * The Access token key. - */ - protected String accessTokenKey; - /** - * The Jsapi ticket key. - */ - protected String jsapiTicketKey; - /** - * The Agent jsapi ticket key. - */ - protected String agentJsapiTicketKey; - /** - * The Lock key. - */ - protected String lockKey; +public class WxCpRedissonConfigImpl extends AbstractWxCpInRedisConfigImpl { + private static final long serialVersionUID = -5674792341070783967L; - /** - * Instantiates a new Wx cp redisson config. - * - * @param redissonClient the redisson client - * @param keyPrefix the key prefix - */ - public WxCpRedissonConfigImpl(@NonNull RedissonClient redissonClient, String keyPrefix) { - this(new RedissonWxRedisOps(redissonClient), keyPrefix); - } - - /** - * Instantiates a new Wx cp redisson config. - * - * @param redissonClient the redisson client - */ public WxCpRedissonConfigImpl(@NonNull RedissonClient redissonClient) { this(redissonClient, null); } - /** - * Instantiates a new Wx cp redisson config. - * - * @param redisOps the redis ops - * @param keyPrefix the key prefix - */ - public WxCpRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) { - this.redisOps = redisOps; - this.keyPrefix = keyPrefix; + public WxCpRedissonConfigImpl(@NonNull RedissonClient redissonClient, String keyPrefix) { + super(new RedissonWxRedisOps(redissonClient), keyPrefix); } - - /** - * 设置企业微信自研应用ID(整数),同时初始化相关的redis key,注意要先调用setCorpId,再调用setAgentId - * - * @param agentId - */ - @Override - public void setAgentId(Integer agentId) { - super.setAgentId(agentId); - String ukey = getCorpId().concat(":").concat(String.valueOf(agentId)); - String prefix = StringUtils.isBlank(keyPrefix) ? "" : - (StringUtils.endsWith(keyPrefix, ":") ? keyPrefix : (keyPrefix + ":")); - lockKey = prefix + LOCK_KEY.concat(ukey); - accessTokenKey = prefix + CP_ACCESS_TOKEN_KEY.concat(ukey); - jsapiTicketKey = prefix + CP_JSAPI_TICKET_KEY.concat(ukey); - agentJsapiTicketKey = prefix + CP_AGENT_JSAPI_TICKET_KEY.concat(ukey); - } - - /** - * Gets lock by key. - * - * @param key the key - * @return the lock by key - */ - protected Lock getLockByKey(String key) { - return redisOps.getLock(key); - } - - @Override - public Lock getAccessTokenLock() { - return getLockByKey(this.lockKey.concat(":").concat("accessToken")); - } - - @Override - public Lock getAgentJsapiTicketLock() { - return getLockByKey(this.lockKey.concat(":").concat("agentJsapiTicket")); - - } - - @Override - public Lock getJsapiTicketLock() { - return getLockByKey(this.lockKey.concat(":").concat("jsapiTicket")); - } - - @Override - public String getAccessToken() { - return redisOps.getValue(this.accessTokenKey); - } - - @Override - public boolean isAccessTokenExpired() { - Long expire = redisOps.getExpire(this.accessTokenKey); - return expire == null || expire < 2; - } - - @Override - public void updateAccessToken(WxAccessToken accessToken) { - redisOps.setValue(this.accessTokenKey, accessToken.getAccessToken(), accessToken.getExpiresIn(), TimeUnit.SECONDS); - } - - @Override - public void updateAccessToken(String accessToken, int expiresInSeconds) { - redisOps.setValue(this.accessTokenKey, accessToken, expiresInSeconds, TimeUnit.SECONDS); - } - - @Override - public void expireAccessToken() { - redisOps.expire(this.accessTokenKey, 0, TimeUnit.SECONDS); - } - - @Override - public String getJsapiTicket() { - return redisOps.getValue(this.jsapiTicketKey); - } - - @Override - public boolean isJsapiTicketExpired() { - Long expire = redisOps.getExpire(this.jsapiTicketKey); - return expire == null || expire < 2; - } - - @Override - public void expireJsapiTicket() { - redisOps.expire(this.jsapiTicketKey, 0, TimeUnit.SECONDS); - } - - @Override - public void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) { - redisOps.setValue(this.jsapiTicketKey, jsapiTicket, expiresInSeconds, TimeUnit.SECONDS); - } - - @Override - public void expireAgentJsapiTicket() { - redisOps.expire(this.agentJsapiTicketKey, 0, TimeUnit.SECONDS); - } - - @Override - public void updateAgentJsapiTicket(String agentJsapiTicket, int expiresInSeconds) { - redisOps.setValue(this.agentJsapiTicketKey, agentJsapiTicket, expiresInSeconds, TimeUnit.SECONDS); - } - - @Override - public String getAgentJsapiTicket() { - return redisOps.getValue(this.agentJsapiTicketKey); - } - - @Override - public boolean isAgentJsapiTicketExpired() { - Long expire = redisOps.getExpire(this.agentJsapiTicketKey); - return expire == null || expire < 2; - } - }