mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-21 19:17:39 +08:00
🎨 #1522 开放平台优化redis存储配置类,同时对应的 spring boot starter 模块增加新特性
This commit is contained in:
@@ -9,26 +9,27 @@
|
||||
</dependency>
|
||||
```
|
||||
2. 添加配置(application.properties)
|
||||
```
|
||||
# 开放平台配置(必填)
|
||||
wx.open.appId = @appId
|
||||
wx.open.secret = @secret
|
||||
wx.open.token = @token
|
||||
wx.open.aesKey = @aesKey
|
||||
# 存储配置redis(可选), 优先使用(wx.open.config-storage.redis)配置的redis, 支持自定注入的JedisPool
|
||||
wx.open.config-storage.type = redis # 可选值, memory(默认), redis
|
||||
wx.open.config-storage.redis.host = 127.0.0.1
|
||||
wx.open.config-storage.redis.port = 6379
|
||||
```properties
|
||||
# 公众号配置(必填)
|
||||
wx.open.appId = appId
|
||||
wx.open.secret = @secret
|
||||
wx.open.token = @token
|
||||
wx.open.aesKey = @aesKey
|
||||
# 存储配置redis(可选)
|
||||
# 优先注入容器的(JedisPool, RedissonClient), 当配置了wx.open.config-storage.redis.host, 不会使用容器注入redis连接配置
|
||||
wx.open.config-storage.type = redis # 配置类型: memory(默认), redis(jedis), jedis, redisson, redistemplate
|
||||
wx.open.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)
|
||||
wx.open.config-storage.redis.host = 127.0.0.1
|
||||
wx.open.config-storage.redis.port = 6379
|
||||
# http客户端配置
|
||||
wx.open.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认)
|
||||
wx.open.config-storage.http-proxy-host=
|
||||
wx.open.config-storage.http-proxy-port=
|
||||
wx.open.config-storage.http-proxy-username=
|
||||
wx.open.config-storage.http-proxy-password=
|
||||
```
|
||||
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService`
|
||||
|
||||
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的
|
||||
- WxOpenConfigStorage
|
||||
- WxOpenService
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -22,12 +22,18 @@
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<scope>compile</scope>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<scope>compile</scope>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@@ -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