wxJava之wx-java-mp踩坑(jedis配置不生效)

坑点

wx.mp.config-storage.type=jedis

选择jedis类型,希望用自己的jedis配置,但无法生效
错误提示:

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

前置配置和pom

        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>wx-java-mp-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>
         <dependency>
            <groupId>com.github.jedis-lock</groupId>
            <artifactId>jedis-lock</artifactId>
            <scope>compile</scope>
        </dependency>
wx:
  mp:
    app-id: wxxxxxxxxxxxx
    secret: afasfsakslkjfslfkajls
    config-storage:
      type: jedis
      key-prefix: wx
      http-client-type: httpclient

原因排查

1、装载配置类:WxMpStorageAutoConfiguration时

	@Bean
    @ConditionalOnMissingBean({WxMpConfigStorage.class})
    public WxMpConfigStorage wxMpConfigStorage() {
        StorageType type = this.wxMpProperties.getConfigStorage().getType();
        WxMpConfigStorage config;
        switch (type) {
            case Jedis:
            	// jedis类型进入该方法
                config = this.jedisConfigStorage();
                break;
            case RedisTemplate:
                config = this.redisTemplateConfigStorage();
                break;
            default:
                config = this.defaultConfigStorage();
        }

        if (null != this.wxMpProperties.getHosts() && StringUtils.isNotEmpty(this.wxMpProperties.getHosts().getApiHost())) {
            WxMpHostConfig hostConfig = new WxMpHostConfig();
            hostConfig.setApiHost(this.wxMpProperties.getHosts().getApiHost());
            hostConfig.setMpHost(this.wxMpProperties.getHosts().getMpHost());
            hostConfig.setOpenHost(this.wxMpProperties.getHosts().getOpenHost());
            config.setHostConfig(hostConfig);
        }

        return config;
    }

2、方法:jedisConfigStorage

    private WxMpConfigStorage jedisConfigStorage() {
        JedisPoolAbstract jedisPool;
        // 注意此处if判断
        if (this.wxMpProperties.getConfigStorage() != null && this.wxMpProperties.getConfigStorage().getRedis() != null && StringUtils.isNotEmpty(this.wxMpProperties.getConfigStorage().getRedis().getHost())) {
            jedisPool = this.getJedisPool();
        } else {
            jedisPool = (JedisPoolAbstract)this.applicationContext.getBean(JedisPool.class);
        }

        WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
        WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps, this.wxMpProperties.getConfigStorage().getKeyPrefix());
        this.setWxMpInfo(wxMpRedisConfig);
        return wxMpRedisConfig;
    }

if判断ConfigStorage和ConfigStorage.redis和ConfigStorage.redis.host,
(1)ConfigStorage:我们配置了,所以不为空;
(2)ConfigStorage.redis:进入类:WxMpProperties中的内部类,也不为空

    public static class ConfigStorage implements Serializable {
        private static final long serialVersionUID = 4815731027000065434L;
        private StorageType type;
        private String keyPrefix;
        @NestedConfigurationProperty
        private final RedisProperties redis;
        private HttpClientType httpClientType;
        private String httpProxyHost;
        private Integer httpProxyPort;
        private String httpProxyUsername;
        private String httpProxyPassword;
    }

(3)ConfigStorage.redis.host:进入类:RedisProperties,host默认给了127.0.0.1,所以也不为空

public class RedisProperties implements Serializable {
    private static final long serialVersionUID = -5924815351660074401L;
    private String host = "127.0.0.1";
    private int port = 6379;
    private String password;
    private int timeout = 2000;
    private int database = 0;
    private String sentinelIps;
    private String sentinelName;
    private Integer maxActive;
    private Integer maxIdle;
    private Integer maxWaitMillis;
    private Integer minIdle;
}

结论:只要我们配置了ConfigStorage,并且Type=jedis,他都不会读我们自己配置的jedis

解决方法

添加配置redis.host=空

wx:
  mp:
    app-id: wxxxxxxxxxxxx
    secret: afasfsakslkjfslfkajls
    config-storage:
      type: jedis
      key-prefix: wx
      http-client-type: httpclient
      // 新增该配置
      redis:
        host:
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐