使用spymemcached spring整合的时候遇到的一个配置问题

    xiaoxiao2021-04-12  34

    当使用spymemcached的时候,在windows下安装了memcached,telnet进入,测试之后写了一个java的测试类

    public static void main(String args[])throws Exception{ MemcachedClient cache = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211)); cache.set("test", 30 * 60 * 1000, "testinfo"); Object object = cache.get("test"); System.out.println(object.toString()); } 当然,这需要导入spymemcached的包,测试结果没有问题。

    既然这样的话就直接用spring整合一下吧。

    首先,创建一个spring-memcached.xml文件:

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="memcachedClientFactory" class="net.spy.memcached.spring.MemcachedClientFactoryBean"> <property name="servers" value="127.0.0.1:11211" /> <property name="protocol" value="BINARY" /> <property name="transcoder"> <bean class="net.spy.memcached.transcoders.SerializingTranscoder"> <property name="compressionThreshold" value="1024" /> </bean> </property> <property name="opTimeout" value="1000" /> <property name="timeoutExceptionThreshold" value="1998" /> <!-- error <property name="hashAlg" value="KETAMA_HASH"/> --> <property name="hashAlg"> <value type="net.spy.memcached.DefaultHashAlgorithm">KETAMA_HASH</value> </property> <property name="locatorType" value="CONSISTENT" /> <property name="failureMode" value="Redistribute" /> <property name="useNagleAlgorithm" value="false" /> <!-- <property name="authDescriptor" ref="authDescriptor" /> --> </bean> </beans>这是好多地方看到的都是这样的配置,需要详细了解的可以查一下。

    然后将他导入到applicationContext.xml中:

    <import resource="classpath:spring-memcached*.xml"/>

    接下来只要在要使用MemcachedClient的地方以注解的方式注入就可以直接使用了

    @Autowired

    MemcachedClient mc;

    具体的代码就不贴了,主要看问题。

    当一切准备就绪,在tomcat下启动出现

    2017-04-13 15:30:40.883 INFO net.spy.memcached.MemcachedConnection:  Added {QA sa=/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue

    认为是没有问题了。

    接下来打开网页,当我想要存取到memcached的时候,出现:

    net.spy.memcached.OperationTimeoutException: Timeout waiting for value: waited 1,000 ms. Node status: Connection Status { /127.0.0.1:11211 active: true, authed: true, last read: 158,960 ms ago } 这里的active不是true吗,刚才测试的时候也好好的呀。

    上网查了一下,有的说是tomcat内存溢出导致的,但是我的tomcat并没有报错呀。

    有的说是spymemcached的版本不对导致的。但是我同样的版本在linux上跑了是没有问题的。

    看了许多的博客帖子都没有找到什么合理的解决方法。

    仔细想了下既然linux上没有问题,但是window上有问题,但是没有spring-memcached.xml的时候又能运行正常,难道问题出在配置文件下吗。

    于是我仔细查了一下这些配置代表什么

    <!-- 一个字符串,包括由空格或逗号分隔的主机或IP地址与端口号 --> <property name="servers" value="127.0.0.1:11211" /> <!-- 指定要使用的协议(BINARY,TEXT),默认是TEXT --> <property name="protocol" value="BINARY" /> <!-- 设置默认的转码器(默认以net.spy.memcached.transcoders.SerializingTranscoder) --> <property name="transcoder"> <bean class="net.spy.memcached.transcoders.SerializingTranscoder"> <property name="compressionThreshold" value="1024" /> </bean> </property> <!-- 以毫秒为单位设置默认的操作超时时间 --> <property name="opTimeout" value="1000" /> <property name="timeoutExceptionThreshold" value="1998" /> <!-- 设置哈希算法 --> <property name="hashAlg" ref="KETAMA_HASH" /> <!-- 设置定位器类型(ARRAY_MOD,CONSISTENT),默认是ARRAY_MOD --> <property name="locatorType" value="CONSISTENT" /> <!-- 设置故障模式(取消,重新分配,重试),默认是重新分配 --> <property name="failureMode" value="Redistribute" /> <!-- 想使用Nagle算法,设置为true --> <property name="useNagleAlgorithm" value="false" /> 其中有一条protocol,虽然不懂这是什么,但是默认是text,为什么这里要改成binary?

    于是我将其改成text试了一下,居然成功了。

    哎就这一个小小的问题,要是有一个人能指点我一下该有多好,就不用花这么多的冤枉时间去找错。

    虽然问题是解决了,但是我还是不知道protocol的这两个属性在不同的平台下有什么影响,为什么会这样,希望知道的能告诉我一声,谢谢啦!

    转载请注明原文地址: https://ju.6miu.com/read-667345.html

    最新回复(0)