redis - 如何使用 Spymemcached 获得 Redis Cloud Memcached 的身份验证?

标签 redis memcached sasl spymemcached

我正在尝试连接到 Redis Cloud Memcached 但出现错误(如下)。我检查了apps.redislabs.com界面中的用户名、密码、主机和端口是否正确。如果我禁用 SASL 并在未经身份验证的情况下连接,我就能够连接。

我该如何诊断?

(使用 spymemcached 2.11.6。)

import net.spy.memcached.auth.*;
import net.spy.memcached.*;
...
List<InetSocketAddress> addresses = Collections.singletonList(addr);
AuthDescriptor ad = new AuthDescriptor(new String[] { "CRAM-MD5", "PLAIN" },
        new PlainCallbackHandler(user, password));
MemcachedClient mc = new MemcachedClient(new ConnectionFactoryBuilder()
       .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
       .setAuthDescriptor(ad).build(),
AddrUtil.getAddresses(host + ":" + port));

堆栈跟踪:

net.spy.memcached.MemcachedConnection:  Added {QA sa=pub-memcache-14154.us-central1-1-1.gce.garantiadata.com/104.197.191.74:14514, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
net.spy.memcached.protocol.binary.BinaryMemcachedNodeImpl:  Discarding partially completed op: SASL auth operation
net.spy.memcached.MemcachedConnection:  Reconnecting due to exception on {QA sa=pub-memcache-14154.us-central1-1-1.gce.garantiadata.com/104.197.191.74:14514, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=1}
java.io.IOException: An existing connection was forcibly closed by the remote host
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at net.spy.memcached.MemcachedConnection.handleReads(MemcachedConnection.java:820)
    at net.spy.memcached.MemcachedConnection.handleReadsAndWrites(MemcachedConnection.java:720)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:683)
    at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:436)
    at net.spy.memcached.MemcachedConnection.run(MemcachedConnection.java:1446)
 net.spy.memcached.MemcachedConnection:  Closing, and reopening {QA sa=pub-memcache-14154.us-central1-1-1.gce.garantiadata.com/104.197.191.74:14514, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=1}, attempt 0.
net.spy.memcached.MemcachedConnection:  Could not redistribute to another node, retrying primary node for foo.

最佳答案

在您的 AuthDescriptior 声明中丢失 "CRAM-MD5"

以下在我的测试中有效:(删除了用户、密码和 url)

AuthDescriptor ad = new AuthDescriptor(new String[] {"PLAIN"}, new PlainCallbackHandler(user, pass));
MemcachedClient mc = null;
    try {
        mc = new MemcachedClient(
        new ConnectionFactoryBuilder()
            .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
            .setAuthDescriptor(ad).build(),
        AddrUtil.getAddresses(host + ":" + port ));
    } catch (IOException e) {
        // handle exception
    }
mc.set("foo", 0, "bar");
String value = (String) mc.get("foo");
System.out.println(value);

关于redis - 如何使用 Spymemcached 获得 Redis Cloud Memcached 的身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42110296/

相关文章:

caching - 从 block 中的 redis 缓存中清除数据

lua - Redis - Lua 表作为返回值 - 为什么这不起作用

apache-kafka - Kafka Zookeeper - 不尝试使用 SASL 进行身份验证(未知错误)

c - 从终端执行时 Netbeans EXE 不工作 - CentOs

redis - 从终端(redis 代理)获取 celery 队列中的任务数

c# - Redis 不会序列化我的复杂对象?

java - 缓存/优化从数据库加载数据的动态 View

java - spy memcached 转换异常,可能的错误?

php - 什么时候应该使用 Memcache 而不是 Memcached?

android - 无法使用 Asmack 连接到我的 ejabberd 服务器