NutzCN Logo
问答 Nutz集成Redis出错
发布于 2594天前 作者 baohang 1839 次浏览 复制 上一个帖子 下一个帖子
标签:

我在ioc资源目录下加入了redis.js

var ioc = {
		// 参考 https://github.com/xetorthio/jedis/wiki/Getting-started
		jedisPoolConfig : {
			type : "redis.clients.jedis.JedisPoolConfig",
			fields : {
				testWhileIdle : true, // 空闲时测试,免得redis连接空闲时间长了断线
				maxTotal : {java : "$conf.getInt('redis.maxTotal', 100)"} // 一般都够了吧
			}
		},
		jedisPool : {
			type : "redis.clients.jedis.JedisPool",
			args : [
			        {refer : "jedisPoolConfig"},
			        // 从配置文件中读取redis服务器信息
			        {java : "$conf.get('redis.host', 'test-38.dev.com')"},
			        {java : "$conf.getInt('redis.port', 6379)"}, 
			        {java : "$conf.getInt('redis.timeout', 2000)"}, 
			        {java : "$conf.get('redis.password','hellorokid')"},
			        {java : "$conf.getInt('redis.database', 0)"}
			        ],
			fields : {},
			events : {
				depose : "destroy" // 关闭应用时必须关掉呢
			}
		},
		jedisClusterNodes : {
			type : "java.util.HashSet",
			args : [
				[{
					type : "redis.clients.jedis.HostAndPort",
					args : [
						{java : "$conf.get('redis.host', 'test-38.dev.com')"},
						{java : "$conf.getInt('redis.port', 6379)"}
					]
				}]
			]
		},
		jedisCluster : {
			type : "redis.clients.jedis.JedisCluster",
			args : [
				{refer:"jedisClusterNodes"},
				{refer:"jedisPoolConfig"}
			],
			events : {
				depose : "close"
			}
		},
		redis : {
			type : "org.nutz.integration.jedis.RedisInterceptor",
			fields : {
				jedisAgent : {refer:"jedisAgent"}
			}
		},
		redisService : {
			type : "org.nutz.integration.jedis.RedisService"
		},
		pubSubService : {
			type : "org.nutz.integration.jedis.pubsub.PubSubService",
			fields : {
				jedisAgent : {refer:"jedisAgent"}
			},
			events : {
				depose : "depose"
			}
		},
		jedisAgent : {
			type : "org.nutz.integration.jedis.JedisAgent",
			fields : {
				ioc : {refer:"$ioc"},
				conf : {refer:"conf"}
			}
		},
		jedisClusterWrapper : {
			type : "org.nutz.integration.jedis.JedisClusterWrapper",
			args : [{refer:"jedisCluster"}]
		}
};

然后在MainModule那边的IocBy里面也加入了"*jedis",但是在MainSetup那边使用

 JedisAgent jedisAgent = ioc.get(JedisAgent.class);
            try (Jedis jedis = jedisAgent.getResource()) { // Java7的语法
                String re = jedis.set("_nutzbook_test_key", "http://nutzbook.wendal.net");
                log.debug("redis say : " + re);
                re = jedis.get("_nutzbook_test_key");
                log.debug("redis say : " + re);
            } finally {
            }

报错了,错误如下

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
	at redis.clients.util.Pool.getResource(Pool.java:53)
	at redis.clients.jedis.JedisPool.getResource(JedisPool.java:99)
	at redis.clients.jedis.JedisPool.getResource(JedisPool.java:12)
	at org.nutz.integration.jedis.JedisAgent.jedis(JedisAgent.java:49)
	at org.nutz.integration.jedis.JedisAgent.getResource(JedisAgent.java:54)
	at com.rokid.weather.api.common.core.Setup.init(Setup.java:42)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
9 回复

并不需要redis.js, 删掉

那我redis的配置信息放在那边?

redis.properties 里面

配置信息放redis.properties, 而redis.properties的路径, 取决于conf这个bean所定义的路径, 看dao.js里面的conf的定义就知道了

另外,测试的时候建议先本地跑通, 而不是连其他服务器上的redis服务.

redis.properties这个文件好像我们book里面没有说明,有样例吗?里面应该放哪些信息?

已经删除了js文件,properties也没有问题,然后在MainModule里面也加入了@IocBy(type = ComboIocProvider.class, args = {"*js", "config/ioc/",
"*anno", "com.rokid.weather",
"*tx", // 事务拦截 aop
"*async","*jedis"}) // 异步执行aop,但是在MainSetup中测试报错了,测试代码


JedisAgent jedisAgent = ioc.get(JedisAgent.class); try (Jedis jedis = jedisAgent.getResource()) { // Java7的语法 String re = jedis.set("_nutzbook_test_key", "http://nutzbook.wendal.net"); log.debug("redis say : " + re); re = jedis.get("_nutzbook_test_key"); log.debug("redis say : " + re); } finally {}

错误日志:

2017-03-20 14:26:44,345 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'conf'<>
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
2017-03-20 14:26:44,500 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:141) INFO  - Nutz.Mvc[nutz] is up in 5870ms
	at redis.clients.util.Pool.getResource(Pool.java:53)
2017-03-20 14:26:44,501 org.nutz.mvc.NutFilter._init(NutFilter.java:113) INFO  - ex

你本地的redis吗?

添加回复
请先登陆
回到顶部