Dubbo Failed to save registry store file, cause: Can not lock the registry cache file
启动的Dubbo 服务的时候报错,异常信息如下:
2016-08-22 16:44:40.588 | DubboSaveRegistryCache-thread-1 | WARN | com.alibaba.dubbo.common.logger.log4j.Log4jLogger:Log4jLogger.java(78) | [DUBBO] Failed to save registry store file, cause: Can not lock the registry cache file /root/.dubbo/dubbo-registry-10.141.4.168.cache, ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties, dubbo version: 2.8.3, current host: 127.0.0.1
java.io.IOException: Can not lock the registry cache file /root/.dubbo/dubbo-registry-10.141.4.168.cache, ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties
at com.alibaba.dubbo.registry.support.AbstractRegistry.doSaveProperties(AbstractRegistry.java:193) ~[dubbo-2.8.3.jar:2.8.3]
at com.alibaba.dubbo.registry.support.AbstractRegistry$SaveProperties.run(AbstractRegistry.java:150) [dubbo-2.8.3.jar:2.8.3]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_60]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_60]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_60]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
报错的大概意思是 Dubbo在保存服务列表时失败,Can not lock the registry cache file /root/.dubbo/dubbo-registry-10.141.4.168.cache,拿不到文件锁,无法保存服务列表。
错误原因
出现这个的原因是服务向ZK注册的同时,会缓存Consumer的列表,写入user.home/.dubbo/dubbo-registry-” + url.getHost() + “.cache 这个文件,当在同一个机器上启动多个Provider的时候,就会出现文件锁争用的问题,报上面这个错误。
解决办法
既然是由于竞争文件锁导致的,那么让服务模块各自缓存自己的cache文件就可以避免这样的问题了。
具体做法是:在provider的xml配置文件中加入 file=”${catalina.home}/dubbo-registry/dubbo-registry.properties” ,如下:
<dubbo:registry id="zkcenter" protocol="zookeeper" address="${dubbo.zk_address}" file="${catalina.home}/dubbo-registry/dubbo-registry.properties"/>
- 1
这样就会在catalina.home目录下生成dubbo-registry这个目录,cache文件就缓存在这个里边了。
参考资料
AbstractRegistry lock problem:https://github.com/alibaba/dubbo/issues/81
另一个方法:
我们使用的Dubbo最近老是遇到WARN [DubboSaveRegistryCache-thread-1] (AbstractRegistry.java:221) method:doSaveProperties - [DUBBO] Failed to save registry store file, cause: Can not lock the registry cache file /home/newad/.dubbo/dubbo-registry-*.*.*.*.cache, ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties, dubbo version: 2.5.3, current host: *.*.*.*
java.io.IOException: Can not lock the registry cache file
/home/newad/.dubbo/dubbo-registry-*.*.*.*.cache, ignore and retry later,
maybe multi java process use the file, please config:
dubbo.registry.file=xxx.properties
从异常中很清楚的看到,Dubbo在保存服务列表时失败,失败的原因也很简单,异常里面都说得很清楚了,Can not lock the
registry cache file
/home/newad/.dubbo/dubbo-registry-*.*.*.*.cache,拿不到文件锁,无法保存服务列表。
Dubbo通过注册中心发现服务,发现的服务Dubbo同时也会保存到本地缓存一份,缓存的好处有很多,比如不需要每次使用的时候都通过注册中心获取,注册中心不可用了,不影响消费端的调用,因为本地缓存了一份服务提供者列表。Dubbo本地缓存默认采用的文件,会根据注册中心自动在当前用户目录下生成一个缓存文件,类似/home/newad/.dubbo/dubbo-registry-*.*.*.*.cache,星号表示注册中心的IP地址,当同一台机器上同时启动多个进程,就会出现多个进程争夺此文件的写入权限,觖此问题的方法也很简单,日志里面都说了重新配置一下这个缓存文件就。
主要在启动脚本里面添加配置: -Ddubbo.registry.file=/home/newad/.dubbo/dubbo-registry-Order-0.cache
- #!/bin/sh
- CU=/home/www/WEB-INF/release/
- #LANG="zh_CN"
- #export LANG
- CP=$CU":./"
- LIB=$CU"lib/*.jar"
- for i in $LIB
- do
- CP="$i:$CP"
- done
- export CP
- JAVA=/home/www/jvm/jdk1.7.0_02/bin/java
- export JAVA
- cd "$CUorder"
- lock=./lock
- if [ ! -f "$lock" ]
- then
- touch "$lock"
- echo "classpath:" $CP
- $JAVA -server -Xms1024m -Xmx1024m -XX:PermSize=256m -DOrder=Order-0 -Dlog4j.configuration=file:/home/www/WEB-INF/release/order/log4j.properties -Ddubbo.registry.file=/home/newad/.dubbo/dubbo-registry-Order-0.cache -cp $CP com.product.PServer
- rm "$lock"
- else
- echo " already startup!"
- fi