Skip to content

Commit 05d1ac1

Browse files
committed
🎨 优化redis配置
1 parent e5e14b6 commit 05d1ac1

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,20 @@
4343
```
4444
wx:
4545
mp:
46+
useRedis: false
47+
redisConfig:
48+
host: 127.0.0.1
49+
port: 6379
4650
configs:
47-
- appId: 1111 (一个公众号的appid)
48-
secret: 1111公众号的appsecret
49-
token: 111 接口配置里的Token值
50-
aesKey: 111 接口配置里的EncodingAESKey值
51-
- appId: 2222 (另一个公众号的appid,以下同上
51+
- appId: 1111 # 第一个公众号的appid
52+
secret: 1111 # 公众号的appsecret
53+
token: 111 # 接口配置里的Token值
54+
aesKey: 111 # 接口配置里的EncodingAESKey值
55+
- appId: 2222 # 第二个公众号的appid,以下同上
5256
secret: 1111
5357
token: 111
5458
aesKey: 111
59+
5560
```
5661
3. 运行Java程序:`WxMpDemoApplication`
5762
4. 配置微信公众号中的接口地址:http://公网可访问域名/wx/portal/xxxxx (注意,xxxxx为对应公众号的appid值);

src/main/java/com/github/binarywang/demo/wx/mp/config/WxMpConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,15 @@ public WxMpService wxMpService() {
5656
WxMpService service = new WxMpServiceImpl();
5757
service.setMultiConfigStorages(configs
5858
.stream().map(a -> {
59-
// WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
60-
WxMpDefaultConfigImpl configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(new JedisPool("redisServer")),
61-
a.getAppId());
59+
WxMpDefaultConfigImpl configStorage;
60+
if (this.properties.isUseRedis()) {
61+
final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
62+
JedisPool jedisPool = new JedisPool(redisConfig.getHost(), redisConfig.getPort());
63+
configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), a.getAppId());
64+
} else {
65+
configStorage = new WxMpDefaultConfigImpl();
66+
}
67+
6268
configStorage.setAppId(a.getAppId());
6369
configStorage.setSecret(a.getSecret());
6470
configStorage.setToken(a.getToken());

src/main/java/com/github/binarywang/demo/wx/mp/config/WxMpProperties.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@
1414
@Data
1515
@ConfigurationProperties(prefix = "wx.mp")
1616
public class WxMpProperties {
17+
/**
18+
* 是否使用redis存储access token
19+
*/
20+
private boolean useRedis;
21+
22+
/**
23+
* redis 配置
24+
*/
25+
private RedisConfig redisConfig;
26+
27+
@Data
28+
public static class RedisConfig {
29+
/**
30+
* redis服务器 主机地址
31+
*/
32+
private String host;
33+
34+
/**
35+
* redis服务器 端口号
36+
*/
37+
private Integer port;
38+
}
39+
40+
/**
41+
* 多个公众号配置信息
42+
*/
1743
private List<MpConfig> configs;
1844

1945
@Data

src/main/resources/application.yml.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ logging:
55
me.chanjar.weixin: DEBUG
66
wx:
77
mp:
8+
useRedis: false
9+
redisConfig:
10+
host: 127.0.0.1
11+
port: 6379
812
configs:
913
- appId: 1111 # 第一个公众号的appid
1014
secret: 1111 # 公众号的appsecret

0 commit comments

Comments
 (0)