Watch Star Fork

GVP红薯 / J2CacheJavaApache-2.0

动态构建J2Cache实例的时候,Redis初始化取值错误导致无法启动 #IK07Z

Closed
傲娇字符  Created at

RedisCacheProvider类第69行代码,如果取值为空,会直接报空指针异常;即使在j2cache.properties文件中配置了,也还是报错。原因是取值Key不对导致。

    String hosts = props.getProperty("hosts");
    String mode = props.getProperty("mode");
    String clusterName = props.getProperty("cluster_name");
    String password = props.getProperty("password");
    int database = Integer.parseInt(props.getProperty("database"));

应该改为:

  1. 添加方法(或者共用RedisUtil类中的key方法,不过需要改成public了)
    private static String key(String prefix, String key) {
        return (prefix == null) ? key : prefix + "." + key;
    }
  1. 代码改为:
    String prefix = "redis";
    String hosts = props.getProperty(key(prefix ,"hosts"));
    String mode = props.getProperty(key(prefix ,"mode"));
    String clusterName = props.getProperty(key(prefix ,"cluster_name"));
    String password = props.getProperty(key(prefix ,"password"));
    int database = Integer.parseInt(props.getProperty(key(prefix ,"database")));

最后一句最好还要判断下是否为null,不过这个是建议。。。

输入图片说明

1584_gongstring 36_ld total 2 participants

Comments (3)

1584_gongstring
傲娇字符 2018-05-24 09:57

@红薯 看来我又必要把源码下下来找一下还有没有别的BUG了 :grimacing:

1584_gongstring
傲娇字符 2018-05-24 10:42

把源码下载下来试了一下,改完上面的BUG后,已经成功跑起来了。 :sweat_smile:

输入图片说明

用法如下(避免因为打开方式不对,产生误解 :stuck_out_tongue: ):

try {

                Properties l1cacheProperties = new Properties();
                l1cacheProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("j2cache/caffeine.properties"));

                Properties l2cacheProperties = new Properties();
                l2cacheProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("j2cache/j2cache.properties"));
                //自定义redis配置连接
                l2cacheProperties.put("redis.hosts",server+":"+port);
                l2cacheProperties.put("redis.timeout",timeout);
                if(auth != null){
                    l2cacheProperties.put("redis.password",auth);
                }

                if(database != null){
                    l2cacheProperties.put("redis.database",database);
                }

                J2CacheConfig config = new J2CacheConfig();
                //不使用集群通知
                config.setBroadcast("none");
                config.setL1CacheName("caffeine");
                config.setL1CacheProperties(l1cacheProperties);

                config.setL2CacheName("redis");
                config.setL2CacheProperties(l2cacheProperties);
                J2CacheBuilder builder = J2CacheBuilder.init(config);
                channel = builder.getChannel();

                logger.info("J2Cache启动成功");
            }catch (Exception e){
                e.printStackTrace();
                logger.error("J2Cache启动失败:{}",e);
            }
36_ld
红薯 2018-05-24 14:06 owner

用法问题哦,自己设置 l2cacheproperties 的时候没必要再搞 redis. 的这个前缀了

Sign in and comment

Assignee
Labels
Not set
Milestone
Branch
Scheduled start
Not set
Scheduled end
Not set
Top level
Priority

Help Search