From a0c370ba6654e3d25ddea3cb342ac57771c27a9b Mon Sep 17 00:00:00 2001 From: miemieYaho Date: Thu, 18 Jun 2020 16:09:54 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#1627=20=E4=BC=98=E5=8C=96=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8Fstarter=EF=BC=8C=E9=81=BF=E5=85=8D=E4=BE=9D?= =?UTF-8?q?=E8=B5=96jedis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miniapp/config/WxMaAutoConfiguration.java | 51 +++++------- .../miniapp/properties/ConfigStorage.java | 65 --------------- .../miniapp/properties/RedisProperties.java | 52 ------------ .../miniapp/properties/WxMaProperties.java | 81 ++++++++++++++++++- 4 files changed, 101 insertions(+), 148 deletions(-) delete mode 100644 spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/ConfigStorage.java delete mode 100644 spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/RedisProperties.java diff --git a/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/config/WxMaAutoConfiguration.java b/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/config/WxMaAutoConfiguration.java index 0aad25f143..7c727c9687 100644 --- a/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/config/WxMaAutoConfiguration.java +++ b/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/config/WxMaAutoConfiguration.java @@ -9,8 +9,6 @@ import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl; import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType; -import com.binarywang.spring.starter.wxjava.miniapp.properties.ConfigStorage; -import com.binarywang.spring.starter.wxjava.miniapp.properties.RedisProperties; import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties; import lombok.AllArgsConstructor; import me.chanjar.weixin.common.redis.JedisWxRedisOps; @@ -89,7 +87,7 @@ public WxMaConfig wxMaConfig() { config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey())); config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat())); - ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage(); + WxMaProperties.ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage(); config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); @@ -104,10 +102,27 @@ private WxMaDefaultConfigImpl wxMaDefaultConfigStorage() { } private WxMaDefaultConfigImpl wxMaJedisConfigStorage() { - RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis(); + WxMaProperties.RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis(); JedisPool jedisPool; - if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) { - jedisPool = getJedisPool(); + if (StringUtils.isNotEmpty(redisProperties.getHost())) { + JedisPoolConfig config = new JedisPoolConfig(); + if (redisProperties.getMaxActive() != null) { + config.setMaxTotal(redisProperties.getMaxActive()); + } + if (redisProperties.getMaxIdle() != null) { + config.setMaxIdle(redisProperties.getMaxIdle()); + } + if (redisProperties.getMaxWaitMillis() != null) { + config.setMaxWaitMillis(redisProperties.getMaxWaitMillis()); + } + if (redisProperties.getMinIdle() != null) { + config.setMinIdle(redisProperties.getMinIdle()); + } + config.setTestOnBorrow(true); + config.setTestWhileIdle(true); + + jedisPool = new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(), + redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase()); } else { jedisPool = applicationContext.getBean(JedisPool.class); } @@ -120,28 +135,4 @@ private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage() { WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix()); } - - private JedisPool getJedisPool() { - ConfigStorage storage = wxMaProperties.getConfigStorage(); - RedisProperties 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-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/ConfigStorage.java b/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/ConfigStorage.java deleted file mode 100644 index 921075d1d3..0000000000 --- a/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/ConfigStorage.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.binarywang.spring.starter.wxjava.miniapp.properties; - -import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType; -import com.binarywang.spring.starter.wxjava.miniapp.enums.StorageType; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.experimental.Accessors; - -import java.io.Serializable; - -/** - * 存储策略. - * - * @author Binary Wang - * @date 2020-05-25 - */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Accessors(chain = true) -public class ConfigStorage implements Serializable { - private static final long serialVersionUID = 4815731027000065434L; - - /** - * 存储类型. - */ - private StorageType type = StorageType.Memory; - - /** - * 指定key前缀. - */ - private String keyPrefix = "wa"; - - /** - * redis连接配置. - */ - private RedisProperties redis; - - /** - * http客户端类型. - */ - private HttpClientType httpClientType = HttpClientType.HttpClient; - - /** - * http代理主机. - */ - private String httpProxyHost; - - /** - * http代理端口. - */ - private Integer httpProxyPort; - - /** - * http代理用户名. - */ - private String httpProxyUsername; - - /** - * http代理密码. - */ - private String httpProxyPassword; - -} diff --git a/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/RedisProperties.java b/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/RedisProperties.java deleted file mode 100644 index 5e630c727f..0000000000 --- a/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/RedisProperties.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.binarywang.spring.starter.wxjava.miniapp.properties; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.experimental.Accessors; - -import java.io.Serializable; - -/** - * redis配置. - * - * @author Binary Wang - * @date 2020-05-25 - */ -@Data -@NoArgsConstructor -@AllArgsConstructor -@Accessors(chain = true) -public class RedisProperties 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-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/WxMaProperties.java b/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/WxMaProperties.java index 0bf267d6c5..0139215ea7 100644 --- a/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/WxMaProperties.java +++ b/spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/WxMaProperties.java @@ -1,5 +1,7 @@ package com.binarywang.spring.starter.wxjava.miniapp.properties; +import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType; +import com.binarywang.spring.starter.wxjava.miniapp.enums.StorageType; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -40,6 +42,83 @@ public class WxMaProperties { /** * 存储策略 */ - private ConfigStorage configStorage = new ConfigStorage(); + private final ConfigStorage configStorage = new ConfigStorage(); + @Data + public static class ConfigStorage { + + /** + * 存储类型. + */ + private StorageType type = StorageType.Memory; + + /** + * 指定key前缀. + */ + private String keyPrefix = "wa"; + + /** + * redis连接配置. + */ + private final RedisProperties redis = new RedisProperties(); + + /** + * http客户端类型. + */ + private HttpClientType httpClientType = HttpClientType.HttpClient; + + /** + * http代理主机. + */ + private String httpProxyHost; + + /** + * http代理端口. + */ + private Integer httpProxyPort; + + /** + * http代理用户名. + */ + private String httpProxyUsername; + + /** + * http代理密码. + */ + private String httpProxyPassword; + } + + @Data + public static class RedisProperties { + + /** + * 主机地址.不填则从spring容器内获取JedisPool + */ + 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; + } }