Skip to content

Commit e572ddf

Browse files
authored
🎨 #3833 【公众号】重构Starter模块配置存储自动配置功能,按存储类型拆分为独立配置类,并新增可选的 Redisson 存储实现
1 parent 51d2ed7 commit e572ddf

File tree

14 files changed

+514
-293
lines changed

14 files changed

+514
-293
lines changed

solon-plugins/wx-java-mp-solon-plugin/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
<dependency>
2323
<groupId>redis.clients</groupId>
2424
<artifactId>jedis</artifactId>
25-
<scope>compile</scope>
25+
<scope>provided</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.redisson</groupId>
29+
<artifactId>redisson</artifactId>
30+
<scope>provided</scope>
2631
</dependency>
2732
<dependency>
2833
<groupId>org.jodd</groupId>

solon-plugins/wx-java-mp-solon-plugin/src/main/java/com/binarywang/solon/wxjava/mp/config/WxMpStorageAutoConfiguration.java

Lines changed: 0 additions & 128 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.binarywang.solon.wxjava.mp.config.storage;
2+
3+
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
4+
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
5+
6+
/**
7+
* @author <a href="https://github.com/buaazyl">zhangyl</a>
8+
*/
9+
public abstract class AbstractWxMpConfigStorageConfiguration {
10+
11+
protected WxMpDefaultConfigImpl config(WxMpDefaultConfigImpl config, WxMpProperties properties) {
12+
config.setAppId(properties.getAppId());
13+
config.setSecret(properties.getSecret());
14+
config.setToken(properties.getToken());
15+
config.setAesKey(properties.getAesKey());
16+
config.setUseStableAccessToken(properties.isUseStableAccessToken());
17+
18+
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
19+
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
20+
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
21+
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
22+
if (configStorageProperties.getHttpProxyPort() != null) {
23+
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
24+
}
25+
return config;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.binarywang.solon.wxjava.mp.config.storage;
2+
3+
import com.binarywang.solon.wxjava.mp.properties.RedisProperties;
4+
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
5+
import lombok.RequiredArgsConstructor;
6+
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
7+
import me.chanjar.weixin.common.redis.WxRedisOps;
8+
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
9+
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
10+
import org.apache.commons.lang3.StringUtils;
11+
import org.noear.solon.annotation.Bean;
12+
import org.noear.solon.annotation.Condition;
13+
import org.noear.solon.annotation.Configuration;
14+
import org.noear.solon.core.AppContext;
15+
import redis.clients.jedis.Jedis;
16+
import redis.clients.jedis.JedisPool;
17+
import redis.clients.jedis.JedisPoolConfig;
18+
19+
/**
20+
* @author <a href="https://github.com/buaazyl">zhangyl</a>
21+
*/
22+
@Configuration
23+
@Condition(
24+
onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.type} = jedis",
25+
onClass = Jedis.class
26+
)
27+
@RequiredArgsConstructor
28+
public class WxMpInJedisConfigStorageConfiguration extends AbstractWxMpConfigStorageConfiguration {
29+
private final WxMpProperties properties;
30+
private final AppContext applicationContext;
31+
32+
@Bean
33+
@Condition(onMissingBean = WxMpConfigStorage.class)
34+
public WxMpConfigStorage wxMpConfigStorage() {
35+
WxMpRedisConfigImpl config = getWxMpRedisConfigImpl();
36+
return this.config(config, properties);
37+
}
38+
39+
private WxMpRedisConfigImpl getWxMpRedisConfigImpl() {
40+
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
41+
JedisPool jedisPool;
42+
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
43+
jedisPool = applicationContext.getBean("wxMpJedisPool");
44+
} else {
45+
jedisPool = applicationContext.getBean(JedisPool.class);
46+
}
47+
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
48+
return new WxMpRedisConfigImpl(redisOps, properties.getConfigStorage().getKeyPrefix());
49+
}
50+
51+
@Bean
52+
@Condition(onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.redis.host}")
53+
public JedisPool wxMpJedisPool() {
54+
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
55+
RedisProperties redis = storage.getRedis();
56+
57+
JedisPoolConfig config = new JedisPoolConfig();
58+
if (redis.getMaxActive() != null) {
59+
config.setMaxTotal(redis.getMaxActive());
60+
}
61+
if (redis.getMaxIdle() != null) {
62+
config.setMaxIdle(redis.getMaxIdle());
63+
}
64+
if (redis.getMaxWaitMillis() != null) {
65+
config.setMaxWaitMillis(redis.getMaxWaitMillis());
66+
}
67+
if (redis.getMinIdle() != null) {
68+
config.setMinIdle(redis.getMinIdle());
69+
}
70+
config.setTestOnBorrow(true);
71+
config.setTestWhileIdle(true);
72+
73+
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
74+
redis.getDatabase());
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.binarywang.solon.wxjava.mp.config.storage;
2+
3+
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
4+
import lombok.RequiredArgsConstructor;
5+
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
6+
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
7+
import org.noear.solon.annotation.Bean;
8+
import org.noear.solon.annotation.Condition;
9+
import org.noear.solon.annotation.Configuration;
10+
11+
/**
12+
* @author <a href="https://github.com/buaazyl">zhangyl</a>
13+
*/
14+
@Configuration
15+
@Condition(
16+
onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.type:memory} = memory"
17+
)
18+
@RequiredArgsConstructor
19+
public class WxMpInMemoryConfigStorageConfiguration extends AbstractWxMpConfigStorageConfiguration {
20+
private final WxMpProperties properties;
21+
22+
@Bean
23+
@Condition(onMissingBean = WxMpConfigStorage.class)
24+
public WxMpConfigStorage wxMpConfigStorage() {
25+
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
26+
config(config, properties);
27+
return config;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.binarywang.solon.wxjava.mp.config.storage;
2+
3+
import com.binarywang.solon.wxjava.mp.properties.RedisProperties;
4+
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
5+
import lombok.RequiredArgsConstructor;
6+
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
7+
import me.chanjar.weixin.mp.config.impl.WxMpRedissonConfigImpl;
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.noear.solon.annotation.Bean;
10+
import org.noear.solon.annotation.Condition;
11+
import org.noear.solon.annotation.Configuration;
12+
import org.noear.solon.core.AppContext;
13+
import org.redisson.Redisson;
14+
import org.redisson.api.RedissonClient;
15+
import org.redisson.config.Config;
16+
import org.redisson.config.TransportMode;
17+
18+
/**
19+
* @author <a href="https://github.com/buaazyl">zhangyl</a>
20+
*/
21+
@Configuration
22+
@Condition(
23+
onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.type} = redisson",
24+
onClass = Redisson.class
25+
)
26+
@RequiredArgsConstructor
27+
public class WxMpInRedissonConfigStorageConfiguration extends AbstractWxMpConfigStorageConfiguration {
28+
private final WxMpProperties properties;
29+
private final AppContext applicationContext;
30+
31+
@Bean
32+
@Condition(onMissingBean = WxMpConfigStorage.class)
33+
public WxMpConfigStorage wxMaConfig() {
34+
WxMpRedissonConfigImpl config = getWxMpInRedissonConfigStorage();
35+
return this.config(config, properties);
36+
}
37+
38+
private WxMpRedissonConfigImpl getWxMpInRedissonConfigStorage() {
39+
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
40+
RedissonClient redissonClient;
41+
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
42+
redissonClient = applicationContext.getBean("wxMpRedissonClient");
43+
} else {
44+
redissonClient = applicationContext.getBean(RedissonClient.class);
45+
}
46+
return new WxMpRedissonConfigImpl(redissonClient, properties.getConfigStorage().getKeyPrefix());
47+
}
48+
49+
@Bean
50+
@Condition(onProperty = "${" + WxMpProperties.PREFIX + ".config-storage.redis.host}")
51+
public RedissonClient wxMpRedissonClient() {
52+
WxMpProperties.ConfigStorage storage = properties.getConfigStorage();
53+
RedisProperties redis = storage.getRedis();
54+
55+
Config config = new Config();
56+
config.useSingleServer()
57+
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
58+
.setDatabase(redis.getDatabase());
59+
if (StringUtils.isNotBlank(redis.getPassword())) {
60+
config.useSingleServer().setPassword(redis.getPassword());
61+
}
62+
config.setTransportMode(TransportMode.NIO);
63+
return Redisson.create(config);
64+
}
65+
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.binarywang.solon.wxjava.mp.integration;
22

33
import com.binarywang.solon.wxjava.mp.config.WxMpServiceAutoConfiguration;
4-
import com.binarywang.solon.wxjava.mp.config.WxMpStorageAutoConfiguration;
4+
import com.binarywang.solon.wxjava.mp.config.storage.WxMpInJedisConfigStorageConfiguration;
5+
import com.binarywang.solon.wxjava.mp.config.storage.WxMpInMemoryConfigStorageConfiguration;
6+
import com.binarywang.solon.wxjava.mp.config.storage.WxMpInRedissonConfigStorageConfiguration;
57
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
68
import org.noear.solon.core.AppContext;
79
import org.noear.solon.core.Plugin;
10+
import org.noear.solon.core.util.ClassUtil;
811

912
/**
1013
* @author noear 2024/9/2 created
@@ -13,8 +16,14 @@ public class WxMpPluginImpl implements Plugin {
1316
@Override
1417
public void start(AppContext context) throws Throwable {
1518
context.beanMake(WxMpProperties.class);
16-
17-
context.beanMake(WxMpStorageAutoConfiguration.class);
1819
context.beanMake(WxMpServiceAutoConfiguration.class);
20+
21+
context.beanMake(WxMpInMemoryConfigStorageConfiguration.class);
22+
if (ClassUtil.loadClass("redis.clients.jedis.Jedis") != null) {
23+
context.beanMake(WxMpInJedisConfigStorageConfiguration.class);
24+
}
25+
if (ClassUtil.loadClass("org.redisson.api.RedissonClient") != null) {
26+
context.beanMake(WxMpInRedissonConfigStorageConfiguration.class);
27+
}
1928
}
2029
}

spring-boot-starters/wx-java-mp-spring-boot-starter/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<dependency>
2323
<groupId>redis.clients</groupId>
2424
<artifactId>jedis</artifactId>
25-
<scope>compile</scope>
25+
<scope>provided</scope>
2626
</dependency>
2727
<dependency>
2828
<groupId>org.springframework.data</groupId>
@@ -44,6 +44,11 @@
4444
<artifactId>httpclient5</artifactId>
4545
<scope>provided</scope>
4646
</dependency>
47+
<dependency>
48+
<groupId>org.redisson</groupId>
49+
<artifactId>redisson</artifactId>
50+
<scope>provided</scope>
51+
</dependency>
4752
</dependencies>
4853

4954
<build>

0 commit comments

Comments
 (0)