java import redis_JAVA中 redisTemplate 和 jedis的配合使用操作

首先项目A,也就是SpringBOOT项目中使用redisTemplate 来做REDIS的缓存时,你会发现存到REDIS里边的KEY和VALUE,redisTemplat使用jdkSerializeable存储二进制字节编码

项目B中使用jedis时,存储起来的是字符串,导致项目A要调用项目缓存的键值对时,获取不到

解决方案:

修改项目A的redisTemplate的序列方式

@Configuration

@EnableCaching

public class RedisConfig extends CachingConfigurerSupport {

/**

* redis模板,存储关键字是字符串,值是Jdk序列化

* @param factory

* @return

* @Description:

*/

@Bean

public RedisTemplate, ?> redisTemplate(RedisConnectionFactory factory) {

RedisTemplate, ?> redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(factory);

//key序列化方式;但是如果方法上有Long等非String类型的话,会报类型转换错误;

RedisSerializer redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;

redisTemplate.setKeySerializer(redisSerializer);

redisTemplate.setHashKeySerializer(redisSerializer);

//默认使用JdkSerializationRedisSerializer序列化方式;会出现乱码,改成StringRedisSerializer

StringRedisSerializer stringSerializer = new StringRedisSerializer();

redisTemplate.setKeySerializer(stringSerializer);

redisTemplate.setValueSerializer(stringSerializer);

redisTemplate.setHashKeySerializer(stringSerializer);

redisTemplate.setHashValueSerializer(stringSerializer);

return redisTemplate;

}

}

补充:RedisTemplate初始化和创建(非Spring注入方式)

概述

在工作中, 可能会在非Spring项目(如Spark,Flink作业)中去操作Redis, 重复造轮子去写工具类没有太大的意义, 使用RedisTemplate已经足够丰富和完善了,使用New的方式进行创建即可, 不同的spring-data-redis的版本会略有不同, 下面以2.3.0和1.8.9做为示例.

2.3.0

maven

org.springframework.data

spring-data-redis

2.3.0.RELEASE

redis.clients

jedis

3.3.0

代码

import org.springframework.data.redis.connection.RedisClusterConfiguration;

import org.springframework.data.redis.connection.RedisNode;

import org.springframework.data.redis.connection.RedisStandaloneConfiguration;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import java.util.Collections;

import java.util.List;

import java.util.Objects;

public class RedisTest {

public static void main(String[] args) {

//单机模式

RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();

rsc.setPort(6379);

rsc.setPassword("123456");

rsc.setHostName("192.168.1.1");

//集群模式

RedisClusterConfiguration rcc = new RedisClusterConfiguration();

rcc.setPassword("123456");

List nodes = Collections.singletonList(new RedisNode("192.168.1.1", 6379));

rcc.setClusterNodes(nodes);

RedisTemplate template = new RedisTemplate<>();

//单机模式

JedisConnectionFactory fac = new JedisConnectionFactory(rsc);

//集群模式

//JedisConnectionFactory fac = new JedisConnectionFactory(rcc);

fac.afterPropertiesSet();

template.setConnectionFactory(fac);

template.setDefaultSerializer(new StringRedisSerializer());

template.afterPropertiesSet();

ValueOperations op = template.opsForValue();

final String key = "123_tmp";

final String value = "abc";

template.delete(key);

op.set(key, value);

assert Objects.equals(op.get(key), value);

}

}

集群方式运行报错

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: ERR This instance has cluster support disabled

解决

在redis.conf下将cluster-enabled改为yes

如果只有一个节点, 改为单机模式

1.8.9

maven

org.springframework.data

spring-data-redis

1.8.9.RELEASE

redis.clients

jedis

2.9.0

代码

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import redis.clients.jedis.JedisPoolConfig;

import redis.clients.jedis.JedisShardInfo;

import java.util.Objects;

public class RedisTest {

public static void main(String[] args) {

RedisTemplate template = new RedisTemplate<>();

JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());

JedisShardInfo shardInfo = new JedisShardInfo("192.168.1.1", 6379);

shardInfo.setPassword("123456");

fac.setShardInfo(shardInfo);

template.setConnectionFactory(fac);

template.setDefaultSerializer(new StringRedisSerializer());

template.afterPropertiesSet();

ValueOperations op = template.opsForValue();

final String key = "123_tmp";

final String value = "abc";

template.delete(key);

op.set(key, value);

assert Objects.equals(op.get(key), value);

}

}

这里有个小细节, 如果不调用setShardInfo()方法, 那么需要执行下面的代码, afterPropertiesSet()用来初始化

JedisConnectionFactory fac = new JedisConnectionFactory(new JedisPoolConfig());

fac.setPort(6379);

fac.setPassword("123456");

fac.setHostName("192.168.1.1");

fac.afterPropertiesSet();

说明

RedisTemplate的构造方法有多种, 上面所举例子为其中的一种; 不通过SpringBoot自动装配的方式, 必须手动去执行afterPropertiesSet()进行初始化; 可参考SpringBoot整合redis的方式, 查看对应实现

6badff88277da0d527a46c43435f45a3.png

3703ccbe85aee1194a96fcc25007c0a2.png

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Jedis工具类来扫描其他Spring Boot项目使用Redis地址。首先,需要在pom.xml文件添加Jedis依赖,然后在Java代码使用Jedis的API来连接Redis服务器并扫描Redis地址。具体实现可以参考以下代码: ```java import redis.clients.jedis.Jedis; import redis.clients.jedis.ScanParams; import redis.clients.jedis.ScanResult; public class RedisScanner { public static void main(String[] args) { // 连接Redis服务器 Jedis jedis = new Jedis("localhost", 6379); // 扫描Redis地址 ScanParams params = new ScanParams().match("*"); String cursor = ""; do { ScanResult<String> result = jedis.scan(cursor, params); for (String key : result.getResult()) { System.out.println(key); } cursor = result.getStringCursor(); } while (!cursor.equals("")); // 关闭连接 jedis.close(); } } ``` ### 回答2: 要使用Java代码静态扫描其他Spring Boot项目使用Redis地址,可以采取以下步骤: 1. 引入必要的依赖: 在项目的pom.xml文件添加Spring Boot和Redis相关的依赖,例如: ```xml <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 创建一个Java类来执行扫描操作: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.ApplicationContext; @SpringBootApplication public class RedisScanner { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(RedisScanner.class, args); RedisProperties redisProperties = context.getBean(RedisProperties.class); System.out.println("Redis地址:" + redisProperties.getHost() + ":" + redisProperties.getPort()); } } ``` 3. 运行扫描程序: 在命令行或IDE执行扫描程序的main方法,它将启动一个Spring Boot应用程序并输出Redis地址。 注意:确保已经正确配置了被扫描项目的Redis连接信息,扫描程序需要能够获取到该信息才能输出正确的Redis地址。 通过以上步骤,你可以使用Java代码静态扫描其他Spring Boot项目使用Redis地址,并将其输出到控制台或其他你需要的地方。 ### 回答3: 要使用Java代码静态扫描其他Spring Boot项目使用Redis地址,可以使用以下步骤: 1. 导入相关的Java类库和依赖:首先需要导入Spring Boot的相关类库,如spring-boot-starter-data-redis,以及RedisJava客户端类库,如jedis或Lettuce。 2. 获取其他项目的源代码:将其他Spring Boot项目的源代码进行获取,可以通过Git克隆或下载压缩包的方式获取。 3. 遍历源代码文件:通过递归方式遍历其他项目的源代码文件夹,可以使用Java的File类和FileUtils类。 4. 搜索Redis连接配置:在遍历的过程,对每个文件进行解析,搜索Redis连接配置的相关代码。可以通过正则表达式匹配关键字,如"RedisTemplate"、"Redisson"等。 5. 提取Redis地址:根据找到的连接配置代码,提取其Redis地址信息。可以通过字符串截取、正则表达式或代码解析等方式提取Redis连接地址。 6. 封装结果:将提取到的Redis地址保存到一个列表或其他数据结构,用于后续的使用或输出。 7. 输出结果:根据需求来定义如何输出结果,可以将结果保存到日志文件、数据库或控制台显示出来。 需要注意的是,以上步骤仅是一个大致的思路,具体的实现方式可能因项目的结构和要求而有所不同。在实现过程,还需要考虑文件过滤、异常处理、性能调优等因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值