java - Spring Redisson哨兵错误-至少需要两个哨兵

标签 java spring-boot redis redis-sentinel redisson

我正在为 Redis 使用哨兵模式。我使用的是 Redisson 客户端 3.9.1 版

<dependencies>
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson-spring-data-20</artifactId>
        <version>${redisson-version}</version>
    </dependency>
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson</artifactId>
        <version>${redisson-version}</version>
    </dependency>
</dependencies>

使用Redisson配置Sentinel:

@EnableCaching
public class RedisCache {

    @Autowired
    private ObjectMapper objectMapper;

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setCodec(new JsonJacksonCodec(objectMapper));

        config.useSentinelServers()
                    .setMasterName("redis-master")
                    .addSentinelAddress("redis://localhost:26378")
                    .addSentinelAddress("redis://localhost:26379");

        return Redisson.create(config);
    }

    @Bean
    public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
        return new RedissonConnectionFactory(redisson);
    }
}

我正在运行两台哨兵服务器和一台没有从服务器的 Redis 主服务器。 这只是我本地环境的设置。
一些命令的输出:

redis-cli -p 26379
127.0.0.1:26379> SENTINEL get-master-addr-by-name redis-master
1) "127.0.0.1"
2) "6379"
redis-cli -p 26378
127.0.0.1:26378> SENTINEL get-master-addr-by-name redis-master
1) "127.0.0.1"
2) "6379"

每当我运行我的 Spring Boot 应用程序时,我都会收到以下错误:

       Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redissonClient' defined in class path resource [xxxx/cache/impl/redis/config/RedisCache.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redissonClient' threw exception; nested exception is org.redisson.client.RedisConnectionException: At least two sentinels should be defined in Redis configuration!

......

            at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0 (AbstractBeanFactory.java:317)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:222)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:315)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:204)
            at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType (ServletContextInitializerBeans.java:226)
            at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType (ServletContextInitializerBeans.java:214)
            at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans (ServletContextInitializerBeans.java:91)
            at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init> (ServletContextInitializerBeans.java:80)
            at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans (ServletWebServerApplicationContext.java:250)
            at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize (ServletWebServerApplicationContext.java:237)
            at org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup (TomcatStarter.java:54)
            at org.apache.catalina.core.StandardContext.startInternal (StandardContext.java:5245)
            at org.apache.catalina.util.LifecycleBase.start (LifecycleBase.java:150)
            at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1420)
            at org.apache.catalina.core.ContainerBase$StartChild.call (ContainerBase.java:1410)
            at java.util.concurrent.FutureTask.run (FutureTask.java:266)
            at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
            at java.lang.Thread.run (Thread.java:748)
        [INFO] ------------------------------------------------------------------------
        [INFO] BUILD FAILURE
        [INFO] ------------------------------------------------------------------------
        [INFO] Total time: 22.566 s
        [INFO] Finished at: 2018-12-07T13:44:14+05:30
        [INFO] ------------------------------------------------------------------------
        [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:run (default-cli) on project xxx: An exception occurred while running. null: InvocationTargetException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redissonConnectionFactory' defined in class path resource [xxxxxxx/cache/impl/redis/config/RedisCache.class]: Unsatisfied dependency expressed through method 'redissonConnectionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redissonClient' defined in class path resource [xxxxxx/cache/impl/redis/config/RedisCache.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redissonClient' threw exception; nested exception is org.redisson.client.RedisConnectionException: At least two sentinels should be defined in Redis configuration!

如何解决此错误?

PS:我也试过主从配置,一个哨兵连接到主机,另一个连接到从机,但错误仍然存​​在。我尝试过在具有不同私有(private) IP 的不同本地机器上运行哨兵,但错误仍然存​​在。 尝试使用 3 个哨兵服务器,没有任何好处。

最佳答案

Redisson 似乎无法使用本地主机地址。当我用 AWS 上托管的地址替换前哨节点地址时,它工作正常。

在我看来,Redisson 是一个非常不成熟的客户端。考虑使用生菜或 jedis。

查看在 https://github.com/redisson/redisson/issues/1736 提交的错误 这不允许它关闭。

我遇到的大部分问题都是在使用哨兵模式时出现的。集群模式我没试过。在单一模式下工作正常。

此外,他们几个月来都没有解决在 github 上提交的错误或问题,这很明显,因为他们有专业版和付费版,所以你只能靠自己了。

目前我不推荐使用 Redisson。

关于java - Spring Redisson哨兵错误-至少需要两个哨兵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53665923/

相关文章:

java - 如何使用 Java 8 按某些标准检查集合中的值是否唯一

java - 如何在protobuf3中将 map 添加到消息中

java - Spring Hibernate 事务回滚不起作用

java - 如何使用 Spring Boot 将示例数据添加到数据库?

redis - Redis中如何让多个节点存储一个hash map

java - 如何在 selenium webdriver java 中实现 jquery 脚本

java - 是否可以通过 DB 或 Rest 通过任务列表使 Camunda Webapps(Spring-boot 独立)更新 .bpmn

spring-boot - SpringBoot Keycloak NoSuchMethodError : javax. ws.rs.core.UriBuilder.resolveTemplates

node.js - JS 闭包、Redis、循环、Async::空数组

kubernetes - 在 Kubernetes 中使用分片的生产 Redis 集群