From 838a352122f53143be6c994d01f4cf00e7b4d74b Mon Sep 17 00:00:00 2001 From: Dream2Land <346570926@qq.com> Date: Sun, 27 Sep 2020 11:25:56 +0800 Subject: [PATCH 1/3] add mp host config & redis sentinel config add mp host config & redis sentinel config --- .../config/WxMpStorageAutoConfiguration.java | 228 ++++++++++-------- .../wxjava/mp/properties/HostConfig.java | 18 ++ .../wxjava/mp/properties/RedisProperties.java | 10 + .../wxjava/mp/properties/WxMpProperties.java | 5 + 4 files changed, 158 insertions(+), 103 deletions(-) create mode 100644 spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java index ad4fb31e4b..77ad17e4c6 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java @@ -1,24 +1,32 @@ package com.binarywang.spring.starter.wxjava.mp.config; -import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; +import java.util.Set; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.StringRedisTemplate; + import com.binarywang.spring.starter.wxjava.mp.enums.StorageType; +import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties; import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties; +import com.google.common.collect.Sets; + import lombok.RequiredArgsConstructor; import me.chanjar.weixin.common.redis.JedisWxRedisOps; import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; import me.chanjar.weixin.common.redis.WxRedisOps; +import me.chanjar.weixin.mp.bean.WxMpHostConfig; import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.core.StringRedisTemplate; import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolAbstract; import redis.clients.jedis.JedisPoolConfig; +import redis.clients.jedis.JedisSentinelPool; /** * 微信公众号存储策略自动配置. @@ -28,99 +36,113 @@ @Configuration @RequiredArgsConstructor public class WxMpStorageAutoConfiguration { - private final ApplicationContext applicationContext; - - private final WxMpProperties wxMpProperties; - - @Value("${wx.mp.config-storage.redis.host:") - private String redisHost; - - @Value("${wx.mp.configStorage.redis.host:") - private String redisHost2; - - @Bean - @ConditionalOnMissingBean(WxMpConfigStorage.class) - public WxMpConfigStorage wxMpConfigStorage() { - StorageType type = wxMpProperties.getConfigStorage().getType(); - WxMpConfigStorage config; - switch (type) { - case Jedis: - config = jedisConfigStorage(); - break; - case RedisTemplate: - config = redisTemplateConfigStorage(); - break; - default: - config = defaultConfigStorage(); - break; - } - return config; - } - - private WxMpConfigStorage defaultConfigStorage() { - WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); - setWxMpInfo(config); - return config; - } - - private WxMpConfigStorage jedisConfigStorage() { - JedisPool jedisPool; - if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) { - jedisPool = getJedisPool(); - } else { - jedisPool = applicationContext.getBean(JedisPool.class); - } - WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); - WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix()); - setWxMpInfo(wxMpRedisConfig); - return wxMpRedisConfig; - } - - private WxMpConfigStorage redisTemplateConfigStorage() { - StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); - WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); - WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, wxMpProperties.getConfigStorage().getKeyPrefix()); - setWxMpInfo(wxMpRedisConfig); - return wxMpRedisConfig; - } - - private void setWxMpInfo(WxMpDefaultConfigImpl config) { - WxMpProperties properties = wxMpProperties; - WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage(); - config.setAppId(properties.getAppId()); - config.setSecret(properties.getSecret()); - config.setToken(properties.getToken()); - config.setAesKey(properties.getAesKey()); - - config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); - config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); - config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); - if (configStorageProperties.getHttpProxyPort() != null) { - config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); - } - } - - private JedisPool getJedisPool() { - WxMpProperties.ConfigStorage storage = wxMpProperties.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()); - } + private final ApplicationContext applicationContext; + + private final WxMpProperties wxMpProperties; + + @Value("${wx.mp.config-storage.redis.host:") + private String redisHost; + + @Value("${wx.mp.configStorage.redis.host:") + private String redisHost2; + + @Bean + @ConditionalOnMissingBean(WxMpConfigStorage.class) + public WxMpConfigStorage wxMpConfigStorage() { + StorageType type = wxMpProperties.getConfigStorage().getType(); + WxMpConfigStorage config; + switch (type) { + case Jedis: + config = jedisConfigStorage(); + break; + case RedisTemplate: + config = redisTemplateConfigStorage(); + break; + default: + config = defaultConfigStorage(); + break; + } + // wx host config + if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApihost())) { + WxMpHostConfig hostConfig = new WxMpHostConfig(); + hostConfig.setApiHost(wxMpProperties.getHosts().getApihost()); + hostConfig.setMpHost(wxMpProperties.getHosts().getMphost()); + hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenhost()); + config.setHostConfig(hostConfig); + } + return config; + } + + private WxMpConfigStorage defaultConfigStorage() { + WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl(); + setWxMpInfo(config); + return config; + } + + private WxMpConfigStorage jedisConfigStorage() { + JedisPoolAbstract jedisPool; + if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) { + jedisPool = getJedisPool(); + } else { + jedisPool = applicationContext.getBean(JedisPool.class); + } + WxRedisOps redisOps = new JedisWxRedisOps(jedisPool); + WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, + wxMpProperties.getConfigStorage().getKeyPrefix()); + setWxMpInfo(wxMpRedisConfig); + return wxMpRedisConfig; + } + + private WxMpConfigStorage redisTemplateConfigStorage() { + StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class); + WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate); + WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, + wxMpProperties.getConfigStorage().getKeyPrefix()); + setWxMpInfo(wxMpRedisConfig); + return wxMpRedisConfig; + } + + private void setWxMpInfo(WxMpDefaultConfigImpl config) { + WxMpProperties properties = wxMpProperties; + WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage(); + config.setAppId(properties.getAppId()); + config.setSecret(properties.getSecret()); + config.setToken(properties.getToken()); + config.setAesKey(properties.getAesKey()); + + config.setHttpProxyHost(configStorageProperties.getHttpProxyHost()); + config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername()); + config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword()); + if (configStorageProperties.getHttpProxyPort() != null) { + config.setHttpProxyPort(configStorageProperties.getHttpProxyPort()); + } + } + + private JedisPoolAbstract getJedisPool() { + WxMpProperties.ConfigStorage storage = wxMpProperties.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); + if (StringUtils.isNotEmpty(redis.getSentinelips())) { + Set sentinels = Sets.newHashSet(redis.getSentinelips().split(",")); + return new JedisSentinelPool(redis.getSentinelname(), sentinels); + } + + return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), + redis.getDatabase()); + } } diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java new file mode 100644 index 0000000000..56b5c60884 --- /dev/null +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java @@ -0,0 +1,18 @@ +package com.binarywang.spring.starter.wxjava.mp.properties; + +import java.io.Serializable; + +import lombok.Data; + +@Data +public class HostConfig implements Serializable { + + private static final long serialVersionUID = -4172767630740346001L; + + private String apihost; + + private String openhost; + + private String mphost; + +} diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java index c8c3307fea..efd4831e99 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java @@ -38,6 +38,16 @@ public class RedisProperties implements Serializable { * 数据库. */ private int database = 0; + + /** + * sentinel ips + */ + private String sentinelips; + + /** + * sentinel name + */ + private String sentinelname; private Integer maxActive; private Integer maxIdle; diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/WxMpProperties.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/WxMpProperties.java index 14a61b5748..818d55f3ee 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/WxMpProperties.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/WxMpProperties.java @@ -40,6 +40,11 @@ public class WxMpProperties { * 设置微信公众号的EncodingAESKey. */ private String aesKey; + + /** + * 自定义host配置 + */ + private HostConfig hosts; /** * 存储策略 From 6788d6effc858a0ff67c155805f7c46ea3456b19 Mon Sep 17 00:00:00 2001 From: Dream2Land <346570926@qq.com> Date: Sun, 27 Sep 2020 14:57:00 +0800 Subject: [PATCH 2/3] add redis sentinel& mp hosts config template add redis sentinel& mp hosts config template --- .../wx-java-mp-spring-boot-starter/README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md b/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md index 5059a59f5d..cef1e89054 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md @@ -20,12 +20,19 @@ wx.mp.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认) wx.mp.config-storage.redis.host = 127.0.0.1 wx.mp.config-storage.redis.port = 6379 + #单机和sentinel同时存在时,优先使用sentinel配置 + #wx.mp.config-storage.redis.sentinelips=127.0.0.1:16379,127.0.0.1:26379 + #wx.mp.config-storage.redis.sentinelname=mymaster # http客户端配置 wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp wx.mp.config-storage.http-proxy-host= wx.mp.config-storage.http-proxy-port= wx.mp.config-storage.http-proxy-username= wx.mp.config-storage.http-proxy-password= + # 公众号地址host配置 + #wx.mp.hosts.apihost=http://proxy.com/ + #wx.mp.hosts.openhost=http://proxy.com/ + #wx.mp.hosts.mphost=http://proxy.com/ ``` 3. 自动注入的类型 - `WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~ From 8a2e7ce605465ae8c7d436ccf594f15816b9a77b Mon Sep 17 00:00:00 2001 From: Dream2Land <346570926@qq.com> Date: Sun, 27 Sep 2020 15:05:52 +0800 Subject: [PATCH 3/3] format spelling format spelling --- .../wx-java-mp-spring-boot-starter/README.md | 10 +++++----- .../mp/config/WxMpStorageAutoConfiguration.java | 14 +++++++------- .../starter/wxjava/mp/properties/HostConfig.java | 6 +++--- .../wxjava/mp/properties/RedisProperties.java | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md b/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md index cef1e89054..ae6ed94592 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md @@ -21,8 +21,8 @@ wx.mp.config-storage.redis.host = 127.0.0.1 wx.mp.config-storage.redis.port = 6379 #单机和sentinel同时存在时,优先使用sentinel配置 - #wx.mp.config-storage.redis.sentinelips=127.0.0.1:16379,127.0.0.1:26379 - #wx.mp.config-storage.redis.sentinelname=mymaster + #wx.mp.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379 + #wx.mp.config-storage.redis.sentinel-name=mymaster # http客户端配置 wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp wx.mp.config-storage.http-proxy-host= @@ -30,9 +30,9 @@ wx.mp.config-storage.http-proxy-username= wx.mp.config-storage.http-proxy-password= # 公众号地址host配置 - #wx.mp.hosts.apihost=http://proxy.com/ - #wx.mp.hosts.openhost=http://proxy.com/ - #wx.mp.hosts.mphost=http://proxy.com/ + #wx.mp.hosts.api-host=http://proxy.com/ + #wx.mp.hosts.open-host=http://proxy.com/ + #wx.mp.hosts.mp-host=http://proxy.com/ ``` 3. 自动注入的类型 - `WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~ diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java index 77ad17e4c6..ef8e5348e2 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/config/WxMpStorageAutoConfiguration.java @@ -63,11 +63,11 @@ public WxMpConfigStorage wxMpConfigStorage() { break; } // wx host config - if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApihost())) { + if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) { WxMpHostConfig hostConfig = new WxMpHostConfig(); - hostConfig.setApiHost(wxMpProperties.getHosts().getApihost()); - hostConfig.setMpHost(wxMpProperties.getHosts().getMphost()); - hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenhost()); + hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost()); + hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost()); + hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost()); config.setHostConfig(hostConfig); } return config; @@ -137,9 +137,9 @@ private JedisPoolAbstract getJedisPool() { } config.setTestOnBorrow(true); config.setTestWhileIdle(true); - if (StringUtils.isNotEmpty(redis.getSentinelips())) { - Set sentinels = Sets.newHashSet(redis.getSentinelips().split(",")); - return new JedisSentinelPool(redis.getSentinelname(), sentinels); + if (StringUtils.isNotEmpty(redis.getSentinelIps())) { + Set sentinels = Sets.newHashSet(redis.getSentinelIps().split(",")); + return new JedisSentinelPool(redis.getSentinelName(), sentinels); } return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(), diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java index 56b5c60884..3ade23b9a6 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/HostConfig.java @@ -9,10 +9,10 @@ public class HostConfig implements Serializable { private static final long serialVersionUID = -4172767630740346001L; - private String apihost; + private String apiHost; - private String openhost; + private String openHost; - private String mphost; + private String mpHost; } diff --git a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java index efd4831e99..07e4280b2f 100644 --- a/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java +++ b/spring-boot-starters/wx-java-mp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/properties/RedisProperties.java @@ -42,12 +42,12 @@ public class RedisProperties implements Serializable { /** * sentinel ips */ - private String sentinelips; + private String sentinelIps; /** * sentinel name */ - private String sentinelname; + private String sentinelName; private Integer maxActive; private Integer maxIdle;