我正在使用Redis的Sentinel模式。我正在使用Redisson Client 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:我也尝试了主从配置,其中一个哨兵连接到主机,另一个哨兵连接到从属,但是错误仍然相同。我尝试过在具有不同私有IP的不同本地计算机上运行的哨兵,但错误仍然相同。 尝试使用3个哨兵服务器,没有任何好处。
答案 0 :(得分:0)
确保同时运行 redis-server
和 redis-sentinal <config>
。
完成后,检查哨兵正在运行的端口。它也可能是 5000,就我而言。然后将 "redis://localhost:5000"
传递给 addSentinelAddress
。
答案 1 :(得分:-1)
Redisson似乎不适用于本地主机地址。当我用AWS托管的地址替换前哨节点地址时,它工作正常。
在我看来,Redisson是一位非常不成熟的客户。考虑使用生菜或吉迪斯。
查看在https://github.com/redisson/redisson/issues/1736处提交的错误 不允许它关闭。
我遇到的大多数问题是使用哨兵模式时。我没有尝试过集群模式。在单模式下可以正常工作。
此外,他们几个月都无法满足在github上提交的错误或问题,这很明显,因为它们具有专业版和付费版本,因此您是一个人。
到目前为止,我不建议使用Redisson。
答案 2 :(得分:-1)
Redisson与本地主机地址完美配合。在您的情况下,SENTINEL SENTINELS
命令返回空结果。这意味着Redis安装程序配置不正确。