3.1K Star 16.7K Fork 5.4K

GVPBinary Wang / WxJava

 / 详情

Redis集群,使用JedisCluster 报错

待办的
创建于  
2023-07-28 08:53

强烈建议大家到 github 相关页面提交问题,方便统一查询管理,具体页面地址:https://github.com/Wechat-Group/WxJava/issues

当然如果必须在这里提问,请务必按以下格式填写,谢谢配合~

提问前,请确保阅读过项目首页说明以及wiki开发文档相关内容,尤其是常见问题部分。完成内容后,请务必移除包括本句在内的无用内容,以免影响阅读,谢谢合作~

另外如果确认属于bug,而且已明确如何修复,请参考贡献指南直接提交PR,省的浪费时间在这里描述问题,非常感谢配合

简要描述

Redis集群,使用JedisCluster 报错
Pipeline is currently not supported for JedisClusterConn

模块版本情况

  • WxJava 模块名: weixin-java-mp
  • WxJava 版本号:4.4.0

详细描述

项目重使用的是redis集群,配置如下

Redis的配置

    @Bean(name = "jedisPoolConfigCustom")
    public JedisPoolConfig jedisPoolConfig(@Value("${spring.redis.jedis.pool.max-total:100}") int maxTotal,
                                           @Value("${spring.redis.jedis.pool.max-wait:5000}") int maxWaitMillis,
                                           @Value("${spring.redis.jedis.pool.max-idle:10}") int maxIdle) {
        JedisPoolConfig config = new JedisPoolConfig();
        // 最大连接数
        config.setMaxTotal(maxTotal);
        // 最小空闲连接数
        config.setMaxIdle(maxIdle);
        // 当池内没有可用的连接时,最大等待时间
        config.setMaxWaitMillis(maxWaitMillis);
        return config;
    }


@Bean(name = "redisConnectionFactory")
    public RedisConnectionFactory connectionFactory(@Qualifier("jedisPoolConfigCustom") @Autowired JedisPoolConfig jedisPoolConfig,
                                                    RedisProperties properties) {
        // 获得默认的连接池构造
        //修改我们的连接池配置
        // 这里需要注意的是,edisConnectionFactoryJ对于Standalone模式的没有(RedisStandaloneConfiguration,JedisPoolConfig)的构造函数,对此
        // 我们用JedisClientConfiguration接口的builder方法实例化一个构造器,还得类型转换
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcf = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        jpcf.poolConfig(jedisPoolConfig);
        //通过构造器来构造jedis客户端配置
        JedisClientConfiguration jedisClientConfiguration = jpcf.build();
        //Jedis连接工厂 JedisConnectionFactory是RedisConnectionFactory子类
        JedisConnectionFactory factory;
        //RedisSentinelConfiguration 是 RedisConfiguration 的子类
        RedisSentinelConfiguration sentinelConfig = getSentinelConfiguration(properties);
        RedisClusterConfiguration clusterConfiguration = getClusterConfiguration(properties);
        if (sentinelConfig != null) {
            log.info("Redis Sentinel 集群配置...");
            factory = new JedisConnectionFactory(sentinelConfig, jedisClientConfiguration);
        } else if (clusterConfiguration != null) {
            log.info("Redis Cluster 集群配置...");
            factory = new JedisConnectionFactory(clusterConfiguration, jedisClientConfiguration);
        } else {
            log.info("Redis Standalone单节点配置...");
            RedisStandaloneConfiguration configuration = getRedisStandaloneConfiguration(properties);
            factory = new JedisConnectionFactory(configuration, jedisClientConfiguration);
        }

        return factory;
    }
    @Bean
    public StringRedisTemplate stringRedisTemplate(@Qualifier("redisConnectionFactory") RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

配置类设置 WxMpService ,注入的是上面Redis配置里的 StringRedisTemplate

@Configuration
public class WechatMpConfig {
    @Autowired
    private WechatMpProperties wechatMpProperties;
    @Autowired
    private StringRedisTemplate stringRedisTemplateCustom;

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        RedisTemplateWxRedisOps wxRedisOps = new RedisTemplateWxRedisOps(stringRedisTemplateCustom);
        WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(wxRedisOps, "wxmp");
        wxMpRedisConfig.setAppId(wechatMpProperties.getAppId());
        wxMpRedisConfig.setSecret(wechatMpProperties.getAppSecret());
        wxMpRedisConfig.setToken(wechatMpProperties.getToken());
        wxMpRedisConfig.setAesKey(wechatMpProperties.getAesKey());
        return wxMpRedisConfig;
    }

}

报错的地方
处理订阅消息获取微信用户信息时:

    @Resource
 
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
                                    WxMpService wxMpService, WxSessionManager sessionManager) {
        this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
        // 下面方法执行时报错了  Pipeline is currently not supported for JedisClusterConnection
        WxMpUser wxMpUser = wechatMpCoreService.getUserInfo(wxMessage.getFromUser(), "zh_CN");

日志

java.lang.UnsupportedOperationException: Pipeline is currently not supported for JedisClusterConnection. at org.springframework.data.redis.connection.jedis.JedisClusterConnection.openPipeline(JedisClusterConnection.java:851) at org.springframework.data.redis.connection.DefaultStringRedisConnection.openPipeline(DefaultStringRedisConnection.java:3973) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.data.redis.core.CloseSuppressingInvocationHandler.invoke(CloseSuppressingInvocationHandler.java:61) at com.sun.proxy.$Proxy372.openPipeline(Unknown Source) at org.springframework.data.redis.core.RedisTemplate.lambda$executePipelined$1(RedisTemplate.java:319) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:223)

在网上搜索,应该是 redis集群客户端JedisCluster不支持Pipeline ,请问有办法处理吗?

评论 (2)

三人成虎 创建了任务

你好,我想要使用Redis缓存Access Token,配置和你这个一样,请问一下如果想要缓存Access Token的话,是要单独配置什么吗?因为我直接用下面的代码操作之后再缓存里面没有任何东西

            WxOAuth2AccessToken accessToken = wxService.getOAuth2Service().getAccessToken(code);

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
Java
1
https://gitee.com/binary/weixin-java-tools.git
git@gitee.com:binary/weixin-java-tools.git
binary
weixin-java-tools
WxJava

搜索帮助