java连接开启了sasl的memcache服务

java连接开启了sasl的memcache服务


public class Xmemcache {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final String server = "192.168.7.253:49156";
		final String authinfo  = "admin:oKSwWKJkbCZ2";
		XMemcachedClientBuilder builder = new XMemcachedClientBuilder(server);  
		// 配置读取连接池大小,缺省为1  
		builder.setConnectionPoolSize(1);    
		Map<InetSocketAddress, AuthInfo> authInfoMap = new HashMap<InetSocketAddress, AuthInfo>();
		authInfoMap.put(new InetSocketAddress("192.168.7.253", 49156), AuthInfo.plain("admin", "oKSwWKJkbCZ2"));
		builder.setAuthInfoMap(authInfoMap);  
		// 添加协议工厂,启用SASL使用binary协议  
		CommandFactory factory = new BinaryCommandFactory(); 
		builder.setCommandFactory(factory);  
		// 分布策略:一致性哈希KetamaMemcachedSessionLocator,ArraySessionLocator  
		MemcachedSessionLocator locator = new ArrayMemcachedSessionLocator(); 
		builder.setSessionLocator(locator);  
		// 序列化转换器  
		Transcoder transcoder = new SerializingTranscoder();
		builder.setTranscoder(transcoder);  
		 BufferAllocator bufferAllocator = new SimpleBufferAllocator();  
		 builder.setBufferAllocator(bufferAllocator);  
		 builder.setFailureMode(true);  
		try {
			MemcachedClient memcachedClient = builder.build();
			System.out.println(memcachedClient.add("key", 1000, "sasl-memcache"));
			System.out.println(memcachedClient.get("key"));
		} catch (IOException | TimeoutException | InterruptedException | MemcachedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值