对微信公众号相关Api进行封装,方便以后复用
整个项目的结果采用Maven聚合工程,方便以后小程序、微信支付、开放平台的Api的封装扩展。
注意:分布式环境下,accessToken获取和更新尽量放在一台中控服务器上,一定要避免各个业务逻辑点各自刷新accessToken, 这样会反复导致accessToken重复刷新,过期问题。比如有这样一个业务场景:我们部署了10台服务器,每台服务器都可以刷新accessToken。 当每个点accessToken过期时,并发量过来,有5台服务器同时发现redis过期,去刷新accessToken,这样会导致token反复过期。
如果不采用中控服务器,可以在发现accessToken过期时,采用分布式锁,保证只有一个业务点去刷新accessToken。
项目地址
使用, 比如获取accessToken
@Test
public void testAccessTokenInMemory() {
//基于内存
WechatPSInMemoryConfigImpl config = new WechatPSInMemoryConfigImpl();
config.setAppId("你的appid");
config.setSecret("你的secret");
WechatPSServiceImpl psService = new WechatPSServiceImpl();
psService.setWechatPSConfigStorage(config);
String accessToken = psService.getAccessToken();
Assert.assertNotNull(accessToken);
}
@Test
public void testAccessTokenInRedis() {
//基于redis,实现分布式锁
...
WechatPSInRedisConfigImpl config = new WechatPSInRedisConfigImpl(jedisPool);
//WechatPSAdServiceImpl config = new WechatPSAdServiceImpl(jedisPool);
config.setAppId("你的appid");
config.setSecret("你的secret");
WechatPSServiceImpl psService = new WechatPSServiceImpl();
psService.setWechatPSConfigStorage(config);
String accessToken = psService.getAccessToken();
Assert.assertNotNull(accessToken);
}
*需要重点关注获取accessToken,要考虑多线程线程安全和分布式
*基于Redis的微信配置provider,需要根据自己实现情况重写
/**
* Created by wujie on 2019/1/13.
* 基于Redis的微信配置provider. 调用者可以根据自己的实际情况进行重写
*/
@Slf4j
public class WechatPSInRedisConfigImpl extends WechatPSInMemoryConfigImpl {
protected static final String ACCESS_TOKEN_KEY = "wechat:accesstoken:";
protected static final String LOCK_ACCESS_TOKEN_KEY = "wechat:accesstokenlock:";
protected static final int LOCKTIMEOUT = 1; //分布式锁的超时时间暂停为1秒,具体视业务进行调整
//使用连接池保证线程安全.
//单节点
protected final JedisPool jedisPool;
private String accessTokenKey;
private String accessTokenLockKey;
public WechatPSInRedisConfigImpl(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
//该项目支持多个公众号,所以每个公众号需要生成独有的存储key来区分
@Override
public void setAppId(String appId) {
super.setAppId(appId);
this.accessTokenKey = ACCESS_TOKEN_KEY.concat(appId);
this.accessTokenLockKey = LOCK_ACCESS_TOKEN_KEY.concat(appId);
}
@Override
public String getAccessToken() {
String accessToken = null;
Jedis jedis = null;
//注意:这里不要乱用jdk7的try-with-resources,try-with-resources会在结束后自动调用close方法
//但前提是:括号里的资源实现类必须实现AutoCloseable或Closeable接口
try {
jedis = this.jedisPool.getResource();
accessToken = jedis.get(this.accessTokenKey);
} catch (Exception e) {
log.error("[redis异常]读取失败", e);
} finally {
jedis.close();
}
return accessToken;
}
@Override
public boolean isAccessTokenExpired() {
Long result = null;
Jedis jedis = null;
try {
jedis = this.jedisPool.getResource();
result = jedis.ttl(accessTokenKey);
} catch (Exception e) {
log.error("[redis异常]读取失败", e);
} finally {
jedis.close();
}
return result != null && result < 0;
}
@Override
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
//分布式锁
RedisLockUtil lock = RedisLockUtil.getInstance(this.jedisPool);
//抢到锁才能更改
if(lock.lock(this.accessTokenLockKey, LOCKTIMEOUT)) {
Jedis jedis = null;
try {
jedis = this.jedisPool.getResource();
jedis.setex(this.