java连接开启了sasl的memcache服务

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 12:35   11   0

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();
  } 
 }

}



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP