Skip to content

Commit a0c370b

Browse files
authored
🎨 #1627 优化小程序starter,避免依赖jedis
1 parent c2034cb commit a0c370b

File tree

4 files changed

+101
-148
lines changed

4 files changed

+101
-148
lines changed

Diff for: spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/config/WxMaAutoConfiguration.java

+21-30
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
1010
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
1111
import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
12-
import com.binarywang.spring.starter.wxjava.miniapp.properties.ConfigStorage;
13-
import com.binarywang.spring.starter.wxjava.miniapp.properties.RedisProperties;
1412
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
1513
import lombok.AllArgsConstructor;
1614
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
@@ -89,7 +87,7 @@ public WxMaConfig wxMaConfig() {
8987
config.setAesKey(StringUtils.trimToNull(this.wxMaProperties.getAesKey()));
9088
config.setMsgDataFormat(StringUtils.trimToNull(this.wxMaProperties.getMsgDataFormat()));
9189

92-
ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
90+
WxMaProperties.ConfigStorage configStorageProperties = wxMaProperties.getConfigStorage();
9391
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
9492
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
9593
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
@@ -104,10 +102,27 @@ private WxMaDefaultConfigImpl wxMaDefaultConfigStorage() {
104102
}
105103

106104
private WxMaDefaultConfigImpl wxMaJedisConfigStorage() {
107-
RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
105+
WxMaProperties.RedisProperties redisProperties = wxMaProperties.getConfigStorage().getRedis();
108106
JedisPool jedisPool;
109-
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
110-
jedisPool = getJedisPool();
107+
if (StringUtils.isNotEmpty(redisProperties.getHost())) {
108+
JedisPoolConfig config = new JedisPoolConfig();
109+
if (redisProperties.getMaxActive() != null) {
110+
config.setMaxTotal(redisProperties.getMaxActive());
111+
}
112+
if (redisProperties.getMaxIdle() != null) {
113+
config.setMaxIdle(redisProperties.getMaxIdle());
114+
}
115+
if (redisProperties.getMaxWaitMillis() != null) {
116+
config.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
117+
}
118+
if (redisProperties.getMinIdle() != null) {
119+
config.setMinIdle(redisProperties.getMinIdle());
120+
}
121+
config.setTestOnBorrow(true);
122+
config.setTestWhileIdle(true);
123+
124+
jedisPool = new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(),
125+
redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase());
111126
} else {
112127
jedisPool = applicationContext.getBean(JedisPool.class);
113128
}
@@ -120,28 +135,4 @@ private WxMaDefaultConfigImpl wxMaRedisTemplateConfigStorage() {
120135
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
121136
return new WxMaRedisBetterConfigImpl(redisOps, wxMaProperties.getConfigStorage().getKeyPrefix());
122137
}
123-
124-
private JedisPool getJedisPool() {
125-
ConfigStorage storage = wxMaProperties.getConfigStorage();
126-
RedisProperties redis = storage.getRedis();
127-
128-
JedisPoolConfig config = new JedisPoolConfig();
129-
if (redis.getMaxActive() != null) {
130-
config.setMaxTotal(redis.getMaxActive());
131-
}
132-
if (redis.getMaxIdle() != null) {
133-
config.setMaxIdle(redis.getMaxIdle());
134-
}
135-
if (redis.getMaxWaitMillis() != null) {
136-
config.setMaxWaitMillis(redis.getMaxWaitMillis());
137-
}
138-
if (redis.getMinIdle() != null) {
139-
config.setMinIdle(redis.getMinIdle());
140-
}
141-
config.setTestOnBorrow(true);
142-
config.setTestWhileIdle(true);
143-
144-
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
145-
redis.getDatabase());
146-
}
147138
}

Diff for: spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/ConfigStorage.java

-65
This file was deleted.

Diff for: spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/RedisProperties.java

-52
This file was deleted.

Diff for: spring-boot-starters/wx-java-miniapp-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/miniapp/properties/WxMaProperties.java

+80-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.binarywang.spring.starter.wxjava.miniapp.properties;
22

3+
import com.binarywang.spring.starter.wxjava.miniapp.enums.HttpClientType;
4+
import com.binarywang.spring.starter.wxjava.miniapp.enums.StorageType;
35
import lombok.Data;
46
import org.springframework.boot.context.properties.ConfigurationProperties;
57

@@ -40,6 +42,83 @@ public class WxMaProperties {
4042
/**
4143
* 存储策略
4244
*/
43-
private ConfigStorage configStorage = new ConfigStorage();
45+
private final ConfigStorage configStorage = new ConfigStorage();
4446

47+
@Data
48+
public static class ConfigStorage {
49+
50+
/**
51+
* 存储类型.
52+
*/
53+
private StorageType type = StorageType.Memory;
54+
55+
/**
56+
* 指定key前缀.
57+
*/
58+
private String keyPrefix = "wa";
59+
60+
/**
61+
* redis连接配置.
62+
*/
63+
private final RedisProperties redis = new RedisProperties();
64+
65+
/**
66+
* http客户端类型.
67+
*/
68+
private HttpClientType httpClientType = HttpClientType.HttpClient;
69+
70+
/**
71+
* http代理主机.
72+
*/
73+
private String httpProxyHost;
74+
75+
/**
76+
* http代理端口.
77+
*/
78+
private Integer httpProxyPort;
79+
80+
/**
81+
* http代理用户名.
82+
*/
83+
private String httpProxyUsername;
84+
85+
/**
86+
* http代理密码.
87+
*/
88+
private String httpProxyPassword;
89+
}
90+
91+
@Data
92+
public static class RedisProperties {
93+
94+
/**
95+
* 主机地址.不填则从spring容器内获取JedisPool
96+
*/
97+
private String host;
98+
99+
/**
100+
* 端口号.
101+
*/
102+
private int port = 6379;
103+
104+
/**
105+
* 密码.
106+
*/
107+
private String password;
108+
109+
/**
110+
* 超时.
111+
*/
112+
private int timeout = 2000;
113+
114+
/**
115+
* 数据库.
116+
*/
117+
private int database = 0;
118+
119+
private Integer maxActive;
120+
private Integer maxIdle;
121+
private Integer maxWaitMillis;
122+
private Integer minIdle;
123+
}
45124
}

0 commit comments

Comments
 (0)