引入jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zno</groupId> <artifactId>memcached-test</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>com.danga</groupId> <artifactId>java-memcached</artifactId> <version>2.6.3</version> </dependency> </dependencies> </project>
配置Beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown"> <constructor-arg> <value>my-memcachedPool</value> </constructor-arg> <property name="servers"> <list> <value>172.16.50.238:11211</value> <value>172.16.162.248:11211</value> </list> </property> <!-- 初始化连接为20 --> <property name="initConn"> <value>20</value> </property> <!-- 最小连接为5 --> <property name="minConn"> <value>7</value> </property> <!-- 最大连接为100 --> <property name="maxConn"> <value>10000</value> </property> <!-- 平衡线程休眠时间为30ms --> <property name="maintSleep"> <value>30</value> </property> <!-- Nagle标志为false --> <property name="nagle"> <value>false</value> </property> <!-- 响应超时时间为3000ms --> <property name="socketTO"> <value>3000</value> </property> </bean> <bean class="com.danga.MemCached.MemCachedClient"> <constructor-arg> <value>my-memcachedPool</value> </constructor-arg> </bean> </beans>
测试类
import java.util.Date; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.danga.MemCached.MemCachedClient; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml"); MemCachedClient client = ctx.getBean(MemCachedClient.class); System.out.println(client.add("xiaoming", "0", new Date(60 * 1000))); System.out.println(client.add("xiaoming1", "0", new Date(60 * 1000))); System.out.println(client.add("xiaoming", "0", new Date(60 * 1000))); // System.out.println(client.get("xiaoming")); System.out.println(client.add("xiaoming", "0", new Date(60 * 1000))); } }
验证结论:
/* ======== 单节点 add 测试 ============ 测试结果: 1. telnet 172.16.50.238 11211 不通时,返回 false 2. telnet 172.16.50.238 11211 通时 ,数据不存在时,返回 true 3. telnet 172.16.50.238 11211 通时 ,数据已存在时,返回 false ==========多节点 [A:172.16.50.238 11211] [B:172.16.162.248 11211] add 测试============ 测试用例123: 1. [AB通] 时数据不存在时true,数据存在时false 2. 运行过程中有节点不可用 System.out.println(client.add("xiaoming", "0", new Date(60*1000))); // 哈希应该存至A ,A通,返回true System.out.println(client.add("xiaoming1", "0", new Date(60*1000)));// 哈希应该存至B ,B通,返回true System.out.println(client.add("xiaoming", "0", new Date(60*1000))); // 哈希应该存至A ,A不通 ,返回false ,A节点移除,哈希算法改变。 !上一行的注释! 也可用get方法代替,交互一次就可以判断节点是否可用。 System.out.println(client.add("xiaoming", "0", new Date(60*1000))); // 哈希算法存至B ,B通,返回true 3. 启动前A节点不可用 哈希算法都存至B */
补充:
如果有AB两节点,B节点停止服务,则之前哈希至B的都哈希至A(但是每次都有稍微卡顿,具体可能是判断该服务可用不可用);当B节点再次提供服务时,则哈希至B(无卡顿)。