赞
踩
目录
1、添加依赖
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>5.0.2</version>
- </dependency>
示例:
- public class JdeisTest {
- public static void main(String[] args) {
-
- //连接redis
- Jedis jedis = new Jedis("192.168.200.129",6379);
- //密码
- jedis.auth("123456");
- System.out.println(jedis.ping());
-
- //keys
- Set<String> keys = jedis.keys("*");
- System.out.println(keys);
-
- //string
- jedis.set("jedis","hello-jedis");
- System.out.println(jedis.get("jedis"));
- jedis.expire("jedis",10L);
-
- //list
- jedis.lpush("list","list1","list2","list3","list4","list5");
- List<String> list = jedis.lrange("list", 0, -1);
- for (String s : list) {
- System.out.println(s);
- }
- //set
- jedis.sadd("set","set1");
- jedis.sadd("set","set2");
- jedis.sadd("set","set3");
- Set<String> set = jedis.smembers("set");
- for (String s : set) {
- System.out.println(s);
- }
- //从存储在键处的设置值中删除指定成员
- long set1 = jedis.srem("set", "1");
-
- //hash
- jedis.hset("hash","username","lisi");
- System.out.println(jedis.hget("hash","username"));
- Map map = new HashMap();
- map.put("age","20");
- map.put("address","杭州");
- jedis.hmset("hash2", map);
- List<String> hash = jedis.hmget("hash2","age","address");
- for (String s : hash) {
- System.out.println(s);
- }
- //zset
- jedis.zadd("zset",10d,"zset1");
- jedis.zadd("zset",10d,"zset2");
- List<String> zset = jedis.zrange("zset", 0, -1);
- for (String member : zset) {
- System.out.println(member);
- }
-
- }
- }
结果:
优点:
缺点:
添加依赖
- <dependency>
- <groupId>io.lettuce</groupId>
- <artifactId>lettuce-core</artifactId>
- <version>6.2.6.RELEASE</version>
- </dependency>
- public class LettuceTest {
- public static void main(String[] args) {
-
- //使用构建器链式编程来builder我们RedisURI
- RedisURI uri = RedisURI.builder()
- .withHost("192.168.200.129")
- .withPort(6379).
- withAuthentication("default", "123456")
- .build();
- //创建连接交白
- RedisClient redisClient = RedisClient.create(uri);
- StatefulRedisConnection<String, String> connect = redisClient.connect();
- //通过connect 创建操作的command
- RedisCommands<String, String> command = connect.sync();
-
- List<String> keys = command.keys("*");
- System.out.println("keys:" + keys);
-
- command.set("name", "三张");
- System.out.println(command.get("name"));
- //list
- command.lpush("list","list1","list2","list3","list4","list5");
- List<String> list = command.lrange("list", 0, -1);
- for (String s : list) {
- System.out.println(s);
- }
- //set
- command.sadd("set","set1");
- command.sadd("set","set2");
- command.sadd("set","set3");
- Set<String> set = command.smembers("set");
- for (String s : set) {
- System.out.println(s);
- }
-
- //hash
- command.hset("hash","username","lisi");
- System.out.println(command.hget("hash","username"));
- Map map = new HashMap();
- map.put("age","20");
- map.put("address","杭州");
- command.hmset("hash2", map);
- List<KeyValue<String, String>> hmget = command.hmget("hash2", "age", "address");
- for (KeyValue<String, String> stringStringKeyValue : hmget) {
- System.out.println(stringStringKeyValue);
- }
- //zset
- command.zadd("zset",10d,"zset1");
- command.zadd("zset",10d,"zset2");
- List<String> zset = command.zrange("zset", 0, -1);
- for (String member : zset) {
- System.out.println(member);
- }
- //释放资源
- connect.close();
- redisClient.close();
-
-
- }
- }
优点:
缺点:
创建一个springboot工程
添加依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
- </dependency>
配置redis文件
- spring:
- redis:
- port: 6379 #端口号
- host: 192.168.200.129 #虚拟机IP地址
- password: 123456 #密码
- database: 0 #redis默认数据库
- timeout: 5000
写测试方法
- public class RedisTemplateTest {
- private static StringRedisTemplate redisTemplate;
-
- @Autowired()
- public void setRedisTemplate(StringRedisTemplate redisTemplate) {
- RedisTools.redisTemplate = redisTemplate;
- }
-
- public static void stringDemo() {
- // 设置字符串值
- redisTemplate.opsForValue().set("key1", "value1");
- // 获取字符串值
- String value = redisTemplate.opsForValue().get("key1");
- System.out.println("String value: " + value);
- }
-
- public static void listDemo() {
- // 在列表左侧插入元素
- redisTemplate.opsForList().leftPush("list1", "value1");
- redisTemplate.opsForList().leftPush("list1", "value2");
-
- // 获取列表范围内的元素
- List<String> values = redisTemplate.opsForList().range("list1", 0, -1);
- System.out.println("List values: " + values);
- }
-
- public static void setDemo() {
- // 向集合中添加元素
- redisTemplate.opsForSet().add("set1", "value1");
- redisTemplate.opsForSet().add("set1", "value2");
-
- // 获取集合所有元素
- Set<String> values = redisTemplate.opsForSet().members("set1");
- System.out.println("Set values: " + values);
- }
-
- public static void hashDemo() {
- // 设置哈希字段的值
- redisTemplate.opsForHash().put("hash1", "field1", "value1");
- redisTemplate.opsForHash().put("hash1", "field2", "value2");
-
- // 获取哈希字段的值
- String value = (String) redisTemplate.opsForHash().get("hash1", "field1");
- System.out.println("Hash value: " + value);
- }
-
- // 添加成员到有序集合
- public static void addToZSet() {
- // 添加成员到有序集合
- redisTemplate.opsForZSet().add("myset", "Alice", 90);
- redisTemplate.opsForZSet().add("myset", "Bob", 85);
- redisTemplate.opsForZSet().add("myset", "Charlie", 95);
- redisTemplate.opsForZSet().add("myset", "David", 80);
- }
-
- // 获取有序集合的成员列表
- public static Set<String> getZSetMembers() {
- return redisTemplate.opsForZSet().range("myset", 0, -1);
- }
-
- // 获取有序集合的成员列表及分数
- public static Set<ZSetOperations.TypedTuple<String>> getZSetWithScores() {
- return redisTemplate.opsForZSet().rangeWithScores("myset", 0, -1);
- }
- }
写controller测试
- @GetMapping("/")
- public String index(){
- RedisTools.get("k1");
- RedisTools.stringDemo();
- RedisTools.listDemo();
- RedisTools.setDemo();
- RedisTools.hashDemo();
- RedisTools.addToZSet();
- Set<String> zSetMembers = RedisTools.getZSetMembers();
- System.out.println(zSetMembers);
- Set<ZSetOperations.TypedTuple<String>> zSetWithScores = ·
- RedisTools.getZSetWithScores();
- for (ZSetOperations.TypedTuple<String> tuple : zSetWithScores) {
- String member = tuple.getValue();
- double score = tuple.getScore();
- System.out.println("Member: " + member + ", Score: " + score);
- }
- System.out.println("------------------");
-
- }
启动项目:访问8080
当我们使用redisTemplate,保存对象时会出现乱码的现象
解决方案1、创建使用StringRedisTemplate 对象
解决方案2、我们自己定义RedisTemplate模板
- @Configuration
- public class RedisConfig {
-
- @Bean
- public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
- // 将template 泛型设置为 <String, Object>
- RedisTemplate<String, Object> template = new RedisTemplate();
- // 连接工厂,不必修改
- template.setConnectionFactory(redisConnectionFactory);
- /*
- * 序列化设置
- */
- // key、hash的key 采用 String序列化方式
- template.setKeySerializer(RedisSerializer.string());
- template.setHashKeySerializer(RedisSerializer.string());
- // value、hash的value 采用 Jackson 序列化方式
- template.setValueSerializer(RedisSerializer.json());
- template.setHashValueSerializer(RedisSerializer.json());
- template.afterPropertiesSet();
-
- return template;
- }
- }
-
注:如果使用java代码设置的中文值,在redis-cli中查看是中文乱码
1、如果是这种乱码:
"\"bbbb\xe5\x9f\xba\xe5\xb0\xbc\xe5\xa4\xaa\xe7\xbe\x8e\""
那么解决办法是,启动redis-cli的时候,后面加“–raw”后缀,就可以了
1、添加依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>io.lettuce</groupId>
- <artifactId>lettuce-core</artifactId>
- </dependency>
2、修改yml文件
- spring:
- redis:
- password: 123456 #密码
- cluster:
- max-redirects: 3
- nodes: 192.168.111.185:6381,192.168.111.185:6382,192.168.111.184:6383,192.168.111.184:6384
- lettuce:
- pool:
- max-active: 8
- max-wait: -1ms
- max-idle: 8
- min-idle: 0
spring.redis.password
: 这是连接Redis所需的密码。
spring.redis.cluster.max-redirects
: 这是Redis Cluster在执行命令时的最大重定向次数。默认值是3
。
spring.redis.cluster.nodes
: 这是Redis Cluster的节点信息。在这个例子中,有四个节点,分别是192.168.111.185:6381
、192.168.111.185:6382
、192.168.111.184:6383
、192.168.111.184:6384
。每个节点由IP地址和端口号组成,用逗号分隔。
spring.redis.lettuce.pool.max-active
: 这是Lettuce连接池的最大活动连接数。默认值是8
。
spring.redis.lettuce.pool.max-wait
: 这是Lettuce连接池中获取连接时的最大等待时间。默认值是-1ms
,表示无限等待。
spring.redis.lettuce.pool.max-idle
: 这是Lettuce连接池的最大空闲连接数。默认值是8
。
spring.redis.lettuce.pool.min-idle
: 这是Lettuce连接池的最小空闲连接数。默认值是0
。
3、启动项目测试
注意:当有一个主机宕机时,springboot会报错
原因:SpringBoot客户端没有动态感知到RedisCluster的最新集群信息
解决方案:刷新节点集群拓扑动态感应
yml添加一下配置
- #支持集群拓扑动态感应刷新,自适应拓扑刷新是否使用所有可用的更新,默认false关团
- spring.redis.lettuce.cluster.refresh.adaptive=true
- #定时刷新
- spring.redis.lettuce.cluster.refresh.period=2000
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。