mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
🎨 #1522 开放平台优化redis存储配置类,同时对应的 spring boot starter 模块增加新特性
This commit is contained in:
@@ -3,20 +3,23 @@ package com.binarywang.spring.starter.wxjava.open.config;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
|
||||
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;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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.JedisPoolConfig;
|
||||
|
||||
@@ -29,15 +32,7 @@ import redis.clients.jedis.JedisPoolConfig;
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenStorageAutoConfiguration {
|
||||
private final WxOpenProperties properties;
|
||||
|
||||
@Autowired(required = false)
|
||||
private JedisPool jedisPool;
|
||||
|
||||
@Autowired(required = false)
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Value("${wx.open.config-storage.redis.host:}")
|
||||
private String redisHost;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
|
||||
@@ -45,43 +40,63 @@ public class WxOpenStorageAutoConfiguration {
|
||||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
WxOpenProperties.StorageType type = storage.getType();
|
||||
|
||||
if (type == WxOpenProperties.StorageType.redis) {
|
||||
return getWxOpenInRedisConfigStorage();
|
||||
WxOpenInMemoryConfigStorage config;
|
||||
if (type == WxOpenProperties.StorageType.redis || type == WxOpenProperties.StorageType.jedis) {
|
||||
config = getWxOpenInRedisConfigStorage();
|
||||
} else if (type == WxOpenProperties.StorageType.redisson) {
|
||||
config = getWxOpenInRedissonConfigStorage();
|
||||
} else if (type == WxOpenProperties.StorageType.redistemplate) {
|
||||
config = getWxOpenInRedisTemplateConfigStorage();
|
||||
} else {
|
||||
config = getWxOpenInMemoryConfigStorage();
|
||||
}
|
||||
|
||||
if (type == WxOpenProperties.StorageType.jedis) {
|
||||
return getWxOpenInRedisConfigStorage();
|
||||
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
|
||||
if (type == WxOpenProperties.StorageType.redisson) {
|
||||
return getWxOpenInRedissonConfigStorage();
|
||||
}
|
||||
return getWxOpenInMemoryConfigStorage();
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
|
||||
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
|
||||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
|
||||
JedisPool poolToUse = jedisPool;
|
||||
if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) {
|
||||
poolToUse = getJedisPool();
|
||||
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse, properties.getConfigStorage().getKeyPrefix());
|
||||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() {
|
||||
RedissonClient redissonClientToUse = this.redissonClient;
|
||||
if (redissonClient == null) {
|
||||
redissonClientToUse = getRedissonClient();
|
||||
private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() {
|
||||
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient();
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse, properties.getConfigStorage().getKeyPrefix());
|
||||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
|
||||
WxRedisOps redisOps = new RedissonWxRedisOps(redissonClient);
|
||||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() {
|
||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class RedisProperties implements Serializable {
|
||||
/**
|
||||
* 主机地址.
|
||||
*/
|
||||
private String host = "127.0.0.1";
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口号.
|
||||
|
||||
@@ -40,7 +40,7 @@ public class WxOpenProperties {
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 存储策略, memory, redis.
|
||||
* 存储策略.
|
||||
*/
|
||||
private ConfigStorage configStorage = new ConfigStorage();
|
||||
|
||||
@@ -49,11 +49,45 @@ public class WxOpenProperties {
|
||||
public static class ConfigStorage implements Serializable {
|
||||
private static final long serialVersionUID = 4815731027000065434L;
|
||||
|
||||
/**
|
||||
* 存储类型.
|
||||
*/
|
||||
private StorageType type = memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀.
|
||||
*/
|
||||
private String keyPrefix = "wx";
|
||||
|
||||
/**
|
||||
* redis连接配置.
|
||||
*/
|
||||
private RedisProperties redis = new RedisProperties();
|
||||
|
||||
private String keyPrefix;
|
||||
/**
|
||||
* http客户端类型.
|
||||
*/
|
||||
private HttpClientType httpClientType = HttpClientType.httpclient;
|
||||
|
||||
/**
|
||||
* http代理主机.
|
||||
*/
|
||||
private String httpProxyHost;
|
||||
|
||||
/**
|
||||
* http代理端口.
|
||||
*/
|
||||
private Integer httpProxyPort;
|
||||
|
||||
/**
|
||||
* http代理用户名.
|
||||
*/
|
||||
private String httpProxyUsername;
|
||||
|
||||
/**
|
||||
* http代理密码.
|
||||
*/
|
||||
private String httpProxyPassword;
|
||||
|
||||
}
|
||||
|
||||
@@ -73,6 +107,17 @@ public class WxOpenProperties {
|
||||
/**
|
||||
* redisson.
|
||||
*/
|
||||
redisson
|
||||
redisson,
|
||||
/**
|
||||
* redistemplate
|
||||
*/
|
||||
redistemplate
|
||||
}
|
||||
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient.
|
||||
*/
|
||||
httpclient
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user