🎨 #3833 【公众号】重构Starter模块配置存储自动配置功能,按存储类型拆分为独立配置类,并新增可选的 Redisson 存储实现
This commit is contained in:
@@ -22,7 +22,12 @@
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<scope>compile</scope>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jodd</groupId>
|
||||
|
||||
@@ -1,128 +0,0 @@
|
||||
package com.binarywang.solon.wxjava.mp.config;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.enums.StorageType;
|
||||
import com.binarywang.solon.wxjava.mp.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.config.WxMpHostConfig;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisSentinelPool;
|
||||
import redis.clients.jedis.util.Pool;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 微信公众号存储策略自动配置.
|
||||
*
|
||||
* @author Luo
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpStorageAutoConfiguration {
|
||||
private final AppContext applicationContext;
|
||||
|
||||
private final WxMpProperties wxMpProperties;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxMpConfigStorage.class)
|
||||
public WxMpConfigStorage wxMpConfigStorage() {
|
||||
StorageType type = wxMpProperties.getConfigStorage().getType();
|
||||
WxMpConfigStorage config;
|
||||
switch (type) {
|
||||
case Jedis:
|
||||
config = jedisConfigStorage();
|
||||
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() {
|
||||
Pool<Jedis> jedisPool;
|
||||
if (wxMpProperties.getConfigStorage() != null && wxMpProperties.getConfigStorage().getRedis() != null
|
||||
&& StringUtils.isNotEmpty(wxMpProperties.getConfigStorage().getRedis().getHost())) {
|
||||
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 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.setUseStableAccessToken(wxMpProperties.isUseStableAccessToken());
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
}
|
||||
|
||||
private Pool<Jedis> getJedisPool() {
|
||||
RedisProperties redis = wxMpProperties.getConfigStorage().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<String> 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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.binarywang.solon.wxjava.mp.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/buaazyl">zhangyl</a>
|
||||
*/
|
||||
public abstract class AbstractWxMpConfigStorageConfiguration {
|
||||
|
||||
protected WxMpDefaultConfigImpl config(WxMpDefaultConfigImpl config, WxMpProperties properties) {
|
||||
config.setAppId(properties.getAppId());
|
||||
config.setSecret(properties.getSecret());
|
||||
config.setToken(properties.getToken());
|
||||
config.setAesKey(properties.getAesKey());
|
||||
config.setUseStableAccessToken(properties.isUseStableAccessToken());
|
||||
|
||||
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.binarywang.solon.wxjava.mp.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/buaazyl">zhangyl</a>
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.type} = jedis",
|
||||
onClass = Jedis.class
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpInJedisConfigStorageConfiguration extends AbstractWxMpConfigStorageConfiguration {
|
||||
private final WxMpProperties properties;
|
||||
private final AppContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean = WxMpConfigStorage.class)
|
||||
public WxMpConfigStorage wxMpConfigStorage() {
|
||||
WxMpRedisConfigImpl config = getWxMpRedisConfigImpl();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
|
||||
private WxMpRedisConfigImpl getWxMpRedisConfigImpl() {
|
||||
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
jedisPool = applicationContext.getBean("wxMpJedisPool");
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
return new WxMpRedisConfigImpl(redisOps, properties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Condition(onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.redis.host}")
|
||||
public JedisPool wxMpJedisPool() {
|
||||
WxMpProperties.ConfigStorage storage = properties.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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.binarywang.solon.wxjava.mp.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/buaazyl">zhangyl</a>
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.type:memory} = memory"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpInMemoryConfigStorageConfiguration extends AbstractWxMpConfigStorageConfiguration {
|
||||
private final WxMpProperties properties;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean = WxMpConfigStorage.class)
|
||||
public WxMpConfigStorage wxMpConfigStorage() {
|
||||
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
||||
config(config, properties);
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.binarywang.solon.wxjava.mp.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpRedissonConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/buaazyl">zhangyl</a>
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.type} = redisson",
|
||||
onClass = Redisson.class
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpInRedissonConfigStorageConfiguration extends AbstractWxMpConfigStorageConfiguration {
|
||||
private final WxMpProperties properties;
|
||||
private final AppContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean = WxMpConfigStorage.class)
|
||||
public WxMpConfigStorage wxMaConfig() {
|
||||
WxMpRedissonConfigImpl config = getWxMpInRedissonConfigStorage();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
|
||||
private WxMpRedissonConfigImpl getWxMpInRedissonConfigStorage() {
|
||||
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = applicationContext.getBean("wxMpRedissonClient");
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxMpRedissonConfigImpl(redissonClient, properties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Condition(onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.redis.host}")
|
||||
public RedissonClient wxMpRedissonClient() {
|
||||
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
RedisProperties redis = storage.getRedis();
|
||||
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
|
||||
.setDatabase(redis.getDatabase());
|
||||
if (StringUtils.isNotBlank(redis.getPassword())) {
|
||||
config.useSingleServer().setPassword(redis.getPassword());
|
||||
}
|
||||
config.setTransportMode(TransportMode.NIO);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.binarywang.solon.wxjava.mp.integration;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.config.WxMpServiceAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.config.WxMpStorageAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.config.storage.WxMpInJedisConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.config.storage.WxMpInMemoryConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.config.storage.WxMpInRedissonConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import org.noear.solon.core.Plugin;
|
||||
import org.noear.solon.core.util.ClassUtil;
|
||||
|
||||
/**
|
||||
* @author noear 2024/9/2 created
|
||||
@@ -13,8 +16,14 @@ public class WxMpPluginImpl implements Plugin {
|
||||
@Override
|
||||
public void start(AppContext context) throws Throwable {
|
||||
context.beanMake(WxMpProperties.class);
|
||||
|
||||
context.beanMake(WxMpStorageAutoConfiguration.class);
|
||||
context.beanMake(WxMpServiceAutoConfiguration.class);
|
||||
|
||||
context.beanMake(WxMpInMemoryConfigStorageConfiguration.class);
|
||||
if (ClassUtil.loadClass("redis.clients.jedis.Jedis") != null) {
|
||||
context.beanMake(WxMpInJedisConfigStorageConfiguration.class);
|
||||
}
|
||||
if (ClassUtil.loadClass("org.redisson.api.RedissonClient") != null) {
|
||||
context.beanMake(WxMpInRedissonConfigStorageConfiguration.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user