Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多节点ApiConfig同步问题 #29

Open
chenxinglin94 opened this issue May 25, 2017 · 4 comments
Open

多节点ApiConfig同步问题 #29

chenxinglin94 opened this issue May 25, 2017 · 4 comments

Comments

@chenxinglin94
Copy link

我的服务需要部署多个节点,但是多个节点就会产生ApiConfig不一致的情况,会使之前获取的token失效,这种情况应该怎么处理

@qixiaobo
Copy link

使用多层cache,目前我们是放在redis中 springcache

@chenxinglin94
Copy link
Author

我将ApiConfig放在redis中,读取时候报错,extends Observable ,但是Observable没有序列化

@qixiaobo
Copy link

qixiaobo commented Jun 5, 2017

其实目前没有好的办法,我们之前使用memcache作为监听也出现了不能序列化的问题 因此需要手动实现Serializable接口,但是对于具体的client其实此时已经可能在反序列化已经失败。
private transient final MemcachedClient client;

public class ApiConfigChangeHandler extends AbstractApiConfigChangeHandle implements Serializable {
    private static final long serialVersionUID = 4683537583713221627L;
    private transient final MemcachedClient client;

    private final static int WECHAT_ACCESS_TOKEN_EXPIRE = 2 * 60 * 60;

    private Logger logger = org.slf4j.LoggerFactory.getLogger(ApiConfigChangeHandler.class);

    @Autowired(required = false)
    public ApiConfigChangeHandler(MemcachedClient client) {
        this.client = client;
    }

    @Override
    public void configChange(ConfigChangeNotice notice) {
        if (client != null && notice != null && notice.getType() == ChangeType.ACCESS_TOKEN) {
            try {
                logger.info("appId:{} accessToken:{}",notice.getAppid(),notice.getValue());
                client.set(notice.getAppid(), WECHAT_ACCESS_TOKEN_EXPIRE, notice.getValue());
            } catch (TimeoutException | InterruptedException | MemcachedException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

@winter4666
Copy link

我是改了下源码,增加了一个继承自ApiConfigSharedApiConfig类,程序里用我的SharedApiConfig替代原来的ApiConfig就可以了。
具体可以看我fork的https://github.com/winter4666/fastweixin,里面readme最后一段写了具体的使用方法。其实就是把accessToken和jsApiTicket存到了redis里,你也可以把这两个东西存到别的地方。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants