Skip to content

Commit 585bd94

Browse files
authored
🎨 #1522 开放平台优化redis存储配置类,同时对应的 spring boot starter 模块增加新特性
1 parent 7ace417 commit 585bd94

File tree

10 files changed

+325
-271
lines changed

10 files changed

+325
-271
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
# 使用说明
2-
1. 在自己的Spring Boot项目里,引入maven依赖
3-
```xml
1+
# wx-java-miniapp-spring-boot-starter
2+
## 快速开始
3+
1. 引入依赖
4+
```xml
45
<dependency>
56
<groupId>com.github.binarywang</groupId>
67
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
78
<version>${version}</version>
89
</dependency>
9-
```
10-
2. 添加配置(application.yml)
11-
```yml
12-
wx:
13-
miniapp:
14-
appid: 111
15-
secret: 111
16-
token: 111
17-
aesKey: 111
18-
msgDataFormat: JSON
19-
```
20-
21-
22-
23-
24-
25-
10+
```
11+
2. 添加配置(application.properties)
12+
```properties
13+
# 公众号配置(必填)
14+
wx.miniapp.appid = appId
15+
wx.miniapp.secret = @secret
16+
wx.miniapp.token = @token
17+
wx.miniapp.aesKey = @aesKey
18+
wx.miniapp.msgDataFormat = @msgDataFormat # 消息格式,XML或者JSON.
19+
# 存储配置redis(可选)
20+
# 注意: 指定redis.host值后不会使用容器注入的redis连接(JedisPool)
21+
wx.miniapp.config-storage.type = jedis # 配置类型: memory(默认), jedis, redistemplate
22+
wx.miniapp.config-storage.key-prefix = wa # 相关redis前缀配置: wa(默认)
23+
wx.miniapp.config-storage.redis.host = 127.0.0.1
24+
wx.miniapp.config-storage.redis.port = 6379
25+
# http客户端配置
26+
wx.miniapp.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认)
27+
wx.miniapp.config-storage.http-proxy-host=
28+
wx.miniapp.config-storage.http-proxy-port=
29+
wx.miniapp.config-storage.http-proxy-username=
30+
wx.miniapp.config-storage.http-proxy-password=
31+
```
32+
3. 自动注入的类型
33+
- `WxMaService`
34+
- `WxMaConfig`
2635

spring-boot-starters/wx-java-mp-spring-boot-starter/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# wx-java-mp-starter
1+
# wx-java-mp-spring-boot-starter
22
## 快速开始
33
1. 引入依赖
44
```xml
@@ -27,9 +27,9 @@
2727
wx.mp.config-storage.http-proxy-username=
2828
wx.mp.config-storage.http-proxy-password=
2929
```
30-
3. 支持自动注入的类型
31-
32-
`WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~
30+
3. 自动注入的类型
31+
- `WxMpService`以及~~相关的服务类, 比如: `wxMpService.getXxxService`。~~
32+
- `WxMpConfigStorage`
3333

3434

3535

spring-boot-starters/wx-java-open-spring-boot-starter/README.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,27 @@
99
</dependency>
1010
```
1111
2. 添加配置(application.properties)
12-
```
13-
# 开放平台配置(必填)
14-
wx.open.appId = @appId
15-
wx.open.secret = @secret
16-
wx.open.token = @token
17-
wx.open.aesKey = @aesKey
18-
# 存储配置redis(可选), 优先使用(wx.open.config-storage.redis)配置的redis, 支持自定注入的JedisPool
19-
wx.open.config-storage.type = redis # 可选值, memory(默认), redis
20-
wx.open.config-storage.redis.host = 127.0.0.1
21-
wx.open.config-storage.redis.port = 6379
12+
```properties
13+
# 公众号配置(必填)
14+
wx.open.appId = appId
15+
wx.open.secret = @secret
16+
wx.open.token = @token
17+
wx.open.aesKey = @aesKey
18+
# 存储配置redis(可选)
19+
# 优先注入容器的(JedisPool, RedissonClient), 当配置了wx.open.config-storage.redis.host, 不会使用容器注入redis连接配置
20+
wx.open.config-storage.type = redis # 配置类型: memory(默认), redis(jedis), jedis, redisson, redistemplate
21+
wx.open.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)
22+
wx.open.config-storage.redis.host = 127.0.0.1
23+
wx.open.config-storage.redis.port = 6379
24+
# http客户端配置
25+
wx.open.config-storage.http-client-type=httpclient # http客户端类型: httpclient(默认)
26+
wx.open.config-storage.http-proxy-host=
27+
wx.open.config-storage.http-proxy-port=
28+
wx.open.config-storage.http-proxy-username=
29+
wx.open.config-storage.http-proxy-password=
2230
```
2331
3. 支持自动注入的类型: `WxOpenService, WxOpenMessageRouter, WxOpenComponentService`
2432

2533
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的
2634
- WxOpenConfigStorage
2735
- WxOpenService
28-
29-
30-
31-
32-
33-
34-

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
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.redisson</groupId>
2929
<artifactId>redisson</artifactId>
30-
<scope>compile</scope>
30+
<scope>provided</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.data</groupId>
34+
<artifactId>spring-data-redis</artifactId>
35+
<version>${spring.boot.version}</version>
36+
<scope>provided</scope>
3137
</dependency>
3238
</dependencies>
3339

spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/config/WxOpenStorageAutoConfiguration.java

+48-33
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
import com.binarywang.spring.starter.wxjava.open.properties.RedisProperties;
44
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenProperties;
55
import lombok.RequiredArgsConstructor;
6+
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
7+
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
8+
import me.chanjar.weixin.common.redis.RedissonWxRedisOps;
9+
import me.chanjar.weixin.common.redis.WxRedisOps;
610
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
711
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
812
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
9-
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
1013
import org.apache.commons.lang3.StringUtils;
1114
import org.redisson.Redisson;
1215
import org.redisson.api.RedissonClient;
1316
import org.redisson.config.Config;
1417
import org.redisson.config.TransportMode;
15-
import org.springframework.beans.factory.annotation.Autowired;
16-
import org.springframework.beans.factory.annotation.Value;
1718
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
19+
import org.springframework.context.ApplicationContext;
1820
import org.springframework.context.annotation.Bean;
1921
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.data.redis.core.StringRedisTemplate;
2023
import redis.clients.jedis.JedisPool;
2124
import redis.clients.jedis.JedisPoolConfig;
2225

@@ -29,59 +32,71 @@
2932
@RequiredArgsConstructor
3033
public class WxOpenStorageAutoConfiguration {
3134
private final WxOpenProperties properties;
32-
33-
@Autowired(required = false)
34-
private JedisPool jedisPool;
35-
36-
@Autowired(required = false)
37-
private RedissonClient redissonClient;
38-
39-
@Value("${wx.open.config-storage.redis.host:}")
40-
private String redisHost;
35+
private final ApplicationContext applicationContext;
4136

4237
@Bean
4338
@ConditionalOnMissingBean(WxOpenConfigStorage.class)
4439
public WxOpenConfigStorage wxOpenConfigStorage() {
4540
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
4641
WxOpenProperties.StorageType type = storage.getType();
4742

48-
if (type == WxOpenProperties.StorageType.redis) {
49-
return getWxOpenInRedisConfigStorage();
43+
WxOpenInMemoryConfigStorage config;
44+
if (type == WxOpenProperties.StorageType.redis || type == WxOpenProperties.StorageType.jedis) {
45+
config = getWxOpenInRedisConfigStorage();
46+
} else if (type == WxOpenProperties.StorageType.redisson) {
47+
config = getWxOpenInRedissonConfigStorage();
48+
} else if (type == WxOpenProperties.StorageType.redistemplate) {
49+
config = getWxOpenInRedisTemplateConfigStorage();
50+
} else {
51+
config = getWxOpenInMemoryConfigStorage();
5052
}
5153

52-
if (type == WxOpenProperties.StorageType.jedis) {
53-
return getWxOpenInRedisConfigStorage();
54-
}
55-
56-
if (type == WxOpenProperties.StorageType.redisson) {
57-
return getWxOpenInRedissonConfigStorage();
54+
WxOpenProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
55+
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
56+
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
57+
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
58+
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
59+
if (configStorageProperties.getHttpProxyPort() != null) {
60+
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
5861
}
59-
return getWxOpenInMemoryConfigStorage();
62+
return config;
6063
}
6164

6265
private WxOpenInMemoryConfigStorage getWxOpenInMemoryConfigStorage() {
6366
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
64-
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
6567
return config;
6668
}
6769

6870
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
69-
JedisPool poolToUse = jedisPool;
70-
if (jedisPool == null || StringUtils.isNotEmpty(redisHost)) {
71-
poolToUse = getJedisPool();
71+
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
72+
JedisPool jedisPool;
73+
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
74+
jedisPool = getJedisPool();
75+
} else {
76+
jedisPool = applicationContext.getBean(JedisPool.class);
7277
}
73-
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(poolToUse, properties.getConfigStorage().getKeyPrefix());
74-
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
78+
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
79+
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
7580
return config;
7681
}
7782

78-
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() {
79-
RedissonClient redissonClientToUse = this.redissonClient;
80-
if (redissonClient == null) {
81-
redissonClientToUse = getRedissonClient();
83+
private WxOpenInRedisConfigStorage getWxOpenInRedissonConfigStorage() {
84+
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
85+
RedissonClient redissonClient;
86+
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
87+
redissonClient = getRedissonClient();
88+
} else {
89+
redissonClient = applicationContext.getBean(RedissonClient.class);
8290
}
83-
WxOpenInRedissonConfigStorage config = new WxOpenInRedissonConfigStorage(redissonClientToUse, properties.getConfigStorage().getKeyPrefix());
84-
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
91+
WxRedisOps redisOps = new RedissonWxRedisOps(redissonClient);
92+
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
93+
return config;
94+
}
95+
96+
private WxOpenInRedisConfigStorage getWxOpenInRedisTemplateConfigStorage() {
97+
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
98+
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
99+
WxOpenInRedisConfigStorage config = new WxOpenInRedisConfigStorage(redisOps, properties.getConfigStorage().getKeyPrefix());
85100
return config;
86101
}
87102

spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/RedisProperties.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class RedisProperties implements Serializable {
1616
/**
1717
* 主机地址.
1818
*/
19-
private String host = "127.0.0.1";
19+
private String host;
2020

2121
/**
2222
* 端口号.

spring-boot-starters/wx-java-open-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenProperties.java

+48-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class WxOpenProperties {
4040
private String aesKey;
4141

4242
/**
43-
* 存储策略, memory, redis.
43+
* 存储策略.
4444
*/
4545
private ConfigStorage configStorage = new ConfigStorage();
4646

@@ -49,11 +49,45 @@ public class WxOpenProperties {
4949
public static class ConfigStorage implements Serializable {
5050
private static final long serialVersionUID = 4815731027000065434L;
5151

52+
/**
53+
* 存储类型.
54+
*/
5255
private StorageType type = memory;
5356

57+
/**
58+
* 指定key前缀.
59+
*/
60+
private String keyPrefix = "wx";
61+
62+
/**
63+
* redis连接配置.
64+
*/
5465
private RedisProperties redis = new RedisProperties();
5566

56-
private String keyPrefix;
67+
/**
68+
* http客户端类型.
69+
*/
70+
private HttpClientType httpClientType = HttpClientType.httpclient;
71+
72+
/**
73+
* http代理主机.
74+
*/
75+
private String httpProxyHost;
76+
77+
/**
78+
* http代理端口.
79+
*/
80+
private Integer httpProxyPort;
81+
82+
/**
83+
* http代理用户名.
84+
*/
85+
private String httpProxyUsername;
86+
87+
/**
88+
* http代理密码.
89+
*/
90+
private String httpProxyPassword;
5791

5892
}
5993

@@ -73,6 +107,17 @@ public enum StorageType {
73107
/**
74108
* redisson.
75109
*/
76-
redisson
110+
redisson,
111+
/**
112+
* redistemplate
113+
*/
114+
redistemplate
115+
}
116+
117+
public enum HttpClientType {
118+
/**
119+
* HttpClient.
120+
*/
121+
httpclient
77122
}
78123
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/redis/JedisWxRedisOps.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import lombok.RequiredArgsConstructor;
44
import me.chanjar.weixin.common.util.locks.JedisDistributedLock;
55
import redis.clients.jedis.Jedis;
6-
import redis.clients.jedis.JedisPool;
6+
import redis.clients.util.Pool;
77

88
import java.util.concurrent.TimeUnit;
99
import java.util.concurrent.locks.Lock;
1010

1111
@RequiredArgsConstructor
1212
public class JedisWxRedisOps implements WxRedisOps {
1313

14-
private final JedisPool jedisPool;
14+
private final Pool<Jedis> jedisPool;
1515

1616
@Override
1717
public String getValue(String key) {

0 commit comments

Comments
 (0)