Skip to content

Commit

Permalink
🎨 优化redis配置
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Aug 22, 2020
1 parent e5e14b6 commit 05d1ac1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,20 @@
```
wx:
mp:
useRedis: false
redisConfig:
host: 127.0.0.1
port: 6379
configs:
- appId: 1111 (一个公众号的appid)
secret: 1111公众号的appsecret
token: 111 接口配置里的Token值
aesKey: 111 接口配置里的EncodingAESKey值
- appId: 2222 (另一个公众号的appid,以下同上
- appId: 1111 # 第一个公众号的appid
secret: 1111 # 公众号的appsecret
token: 111 # 接口配置里的Token值
aesKey: 111 # 接口配置里的EncodingAESKey值
- appId: 2222 # 第二个公众号的appid,以下同上
secret: 1111
token: 111
aesKey: 111
```
3. 运行Java程序:`WxMpDemoApplication`
4. 配置微信公众号中的接口地址:http://公网可访问域名/wx/portal/xxxxx (注意,xxxxx为对应公众号的appid值);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ public WxMpService wxMpService() {
WxMpService service = new WxMpServiceImpl();
service.setMultiConfigStorages(configs
.stream().map(a -> {
// WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
WxMpDefaultConfigImpl configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(new JedisPool("redisServer")),
a.getAppId());
WxMpDefaultConfigImpl configStorage;
if (this.properties.isUseRedis()) {
final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
JedisPool jedisPool = new JedisPool(redisConfig.getHost(), redisConfig.getPort());
configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), a.getAppId());
} else {
configStorage = new WxMpDefaultConfigImpl();
}

configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@
@Data
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {
/**
* 是否使用redis存储access token
*/
private boolean useRedis;

/**
* redis 配置
*/
private RedisConfig redisConfig;

@Data
public static class RedisConfig {
/**
* redis服务器 主机地址
*/
private String host;

/**
* redis服务器 端口号
*/
private Integer port;
}

/**
* 多个公众号配置信息
*/
private List<MpConfig> configs;

@Data
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ logging:
me.chanjar.weixin: DEBUG
wx:
mp:
useRedis: false
redisConfig:
host: 127.0.0.1
port: 6379
configs:
- appId: 1111 # 第一个公众号的appid
secret: 1111 # 公众号的appsecret
Expand Down

0 comments on commit 05d1ac1

Please sign in to comment.