Springboot2.X集成redis集群(Lettuce)连接

前提:搭建好redis集群环境,搭建方式请看:https://www.cnblogs.com/xymBlog/p/9300574.html

1. 新建工程,pom.xml文件中添加redis支持

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.      配置application.properties

1 spring.redis.cluster.nodes=127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0.0.1:6384,127.0.0.1:6385
2
3 spring.redis.cluster.timeout=1000
4
5 spring.redis.cluster.max-redirects=3

3.      新建下面的两个类

@Configuration

public class RedisConfiguration {

    @Resource

    private LettuceConnectionFactory myLettuceConnectionFactory;

    @Bean

    public RedisTemplate<String, Serializable> redisTemplate() {

        RedisTemplate<String, Serializable> template = new RedisTemplate<>();

        template.setKeySerializer(new StringRedisSerializer());

        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.setConnectionFactory(myLettuceConnectionFactory);

        return template;

    }

}

  

@Configuration

public class RedisFactoryConfig {

    @Autowired

    private Environment environment;

    @Bean

    public RedisConnectionFactory myLettuceConnectionFactory() {

        Map<String, Object> source = new HashMap<String, Object>();

        source.put("spring.redis.cluster.nodes", environment.getProperty("spring.redis.cluster.nodes"));

        source.put("spring.redis.cluster.timeout", environment.getProperty("spring.redis.cluster.timeout"));

        source.put("spring.redis.cluster.max-redirects", environment.getProperty("spring.redis.cluster.max-redirects"));

        RedisClusterConfiguration redisClusterConfiguration;

        redisClusterConfiguration = new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));

        return new LettuceConnectionFactory(redisClusterConfiguration);

    }

}

  

4.      执行测试

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisConfigurationTest {

    @Autowired
private RedisTemplate redisTemplate;

@Test
public void redisTemplate() throws Exception {

        redisTemplate.opsForValue().set("author", "Damein_xym");
}

}

5.      验证,使用Redis Desktop Manager 连接redis节点,查看里面的数据是否存在author,有如下显示,证明成功。

原文地址:https://www.cnblogs.com/xymBlog/p/9303032.html

时间: 2024-12-09 19:31:11

Springboot2.X集成redis集群(Lettuce)连接的相关文章

Spring集成redis集群

Spring集成redis集群 有密码 Maven <jedis.version>2.9.0</jedis.version> <spring-data-redis.version>1.7.1.RELEASE</spring-data-redis.version> <spring.version>3.2.17.RELEASE</spring.version> <dependency> <groupId>org.s

SpingBoot之集成Redis集群

一.安装Redis集群 安装步骤参照网上教程,Mac安装步骤参照https://github.com/muyl/mac-docker-redis-cluster 二.创建SpringBoot工程 创建Redis配置类 package com.example.chapterredis.common.config; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.slf4j.Logger; import

Java集成redis集群

spring-redis.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www

Liunx下Redis集群的安装与测试,以及项目中的应用(redis中对象和集合的储存)。

Liunx下Redis集群的安装与测试,以及项目中的应用. 首先准备ruby和redis接口: redis-3.0.0.gem和 去https://redis.io/下载 1.使用ruby脚本搭建集群.需要ruby的运行环境. 安装ruby yum install ruby yum install rubygems 1.1安装ruby脚本运行使用的包. [[email protected] ~]# gem install redis-3.0.0.gem Successfully installe

redis集群的搭建详细教程

1 Redis-cluster架构图             redis-cluster投票:容错  (至少要三个才可以,才能超过半数) 架构细节: (1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽. (2)节点的fail是通过集群中超过半数的节点检测失效时才生效. (3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可. (4)redis-cluster把所有的物理节点映射到[0-

Redis集群~StackExchange.redis连接Twemproxy代理服务器

回到目录 本文是Redis集群系列的一篇文章,主要介绍使用StackExchange.Redis进行Twemproxy(文中简称TW)代理服务的连接过程,事务上,对于TW来说,我们需要理解一下它的物理架构,它类似于Nugix,主要实现的是请求转发,但它还有一个重要的功能,那就是自动分片,这对于大数据是很必要的,你的服务器需要横向扩展时,不需要告诉客户端,这是一种很理解化的设计模式,当然,也对于Redis来说,在配置TW之后,是可以被全美支持的! 关于tw和Redis集群的设计图 关于StackE

通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool

一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使用redis客户端可以连接集群(我使用的redis desktop manager) 在java中通过jedis连接redis单机也成功,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool, 我以命令行方式操作是没问题的

Redis集群~StackExchange.redis连接Sentinel服务器并订阅相关事件(原创)

回到目录 对于redis-sentinel我在之前的文章中已经说过,它是一个仲裁者,当主master挂了后,它将在所有slave服务器中进行选举,选举的原则当然可以看它的官方文章,这与我们使用者没有什么关系,而对于sentinel来说,它在进行主从切换时,会触发相关事件,这是和我们开发人员有关系的,如当+switch-master事件被触发时,说明当前Sentinal已经完成了一次主从的切换,并所有服务已经正常运转了. 下面是我这几天作的测试,对于Twemproxy代理和Sentinal哨兵都已

lua连接redis集群

连接redis集群需要用到llua-resty-redis-cluster模块 github地址:https://github.com/cuiweixie/lua-resty-redis-cluster 下载完成后,只需要用到包中2个文件rediscluster.lua和redis_slot.c .c文件无法在nginx配置文件中引入,需要编译成.so文件,编译命令:  gcc SOURCE_FILES -fPIC -shared -o TARGET 如下则是连接redis集群代码: local