Redis client Lettuce can't connect to embedded RedisServer but Jedis can connect

36 Views Asked by At

1、Jedis connect success

    @Test
    public void test() throws Exception {
        RedisServer redisServer = RedisServer.builder()
            .setting("maxmemory 128M")
            .port(6379)
            .bind("127.0.0.1")
            .build();
        redisServer.start();

        JedisPool pool = null;
        Jedis jedis = null;
        try {
            pool = new JedisPool("localhost", 6379);
            jedis = pool.getResource();
            jedis.set("hello", "world");
            System.out.println(jedis.get("hello"));
        } finally {
            if (jedis != null)
                pool.returnResource(jedis);
            redisServer.stop();
        }
    }

code print world

2、Lettuce can't connect to redis server

    @Test
    public void test() throws Exception {
        RedisServer redisServer = RedisServer.builder()
            .setting("maxmemory 128M")
            .port(6379)
            .bind("127.0.0.1")
            .build();
        redisServer.start();

        RedisURI redisURI = RedisURI.builder()
            .withHost("127.0.0.1")
            .withPort(6379)
            .withDatabase(0)
            .build();

        RedisClient client = RedisClient.create(redisURI);
        StatefulRedisConnection<String, String> connection = client.connect();
        connection.sync().set("hello", "world");
        System.out.println(connection.sync().get("hello"));

        redisServer.stop();
    }

Code throws exception:

io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.1/<unresolved>:6379

    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:242)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:206)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:191)
    at com.huaweicloud.iot.msgsms.sdk.redis.FailoverRedisClientTest.test(FailoverRedisClientTest.java:39)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: io.netty.channel.StacklessClosedChannelException
    at io.netty.channel.AbstractChannel$AbstractUnsafe.ensureOpen(ChannelPromise)(Unknown Source)
0

There are 0 best solutions below