你知道spring boot项目中yml配置文件中不配置redis的连接信息,启动本地的redis服务,能连接吗?答案是可以连接的的,why?以下给你揭秘。
/**
* RedisTemplate配置
* @param
* @return
*/
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);// 需要配置一下StringSerializer,不然key会乱码 /XX/XX
template.afterPropertiesSet();
return template;
}
/**
* Thread-safe factory of Redis connections.
*
* @author Costin Leau
* @author Christoph Strobl
*/
public interface RedisConnectionFactory extends PersistenceExceptionTranslator {
/**
* Provides a suitable connection for interacting with Redis.
*
* @return connection for interacting with Redis.
*/
RedisConnection getConnection();
/**
* Provides a suitable connection for interacting with Redis Cluster.
*
* @return
* @throws
* @since 1.7
*/
RedisClusterConnection getClusterConnection();
public class JedisConnectionFactory implements InitializingBean, DisposableBean, RedisConnectionFactory {
private final static Log log = LogFactory.getLog(JedisConnectionFactory.class);
private static final ExceptionTranslationStrategy EXCEPTION_TRANSLATION = new PassThroughExceptionTranslationStrategy(
JedisConverters.exceptionConverter());
private static final Method SET_TIMEOUT_METHOD;
private static final Method GET_TIMEOUT_METHOD;
static {
// We need to configure Jedis socket timeout via reflection since the method-name was changed between releases.
Method setTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "setTimeout", int.class);
if (setTimeoutMethodCandidate == null) {
// Jedis V 2.7.x changed the setTimeout method to setSoTimeout
setTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "setSoTimeout", int.class);
}
SET_TIMEOUT_METHOD = setTimeoutMethodCandidate;
Method getTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "getTimeout");
if (getTimeoutMethodCandidate == null) {
getTimeoutMethodCandidate = ReflectionUtils.findMethod(JedisShardInfo.class, "getSoTimeout");
}
GET_TIMEOUT_METHOD = getTimeoutMethodCandidate;
}
private JedisShardInfo shardInfo;
private String hostName = "localhost";
private int port = Protocol.DEFAULT_PORT;
private int timeout = Protocol.DEFAULT_TIMEOUT;
private String password;
private boolean usePool = true;
private boolean useSsl = false;
private Pool<Jedis> pool;
private JedisPoolConfig poolConfig = new JedisPoolConfig();
private int dbIndex = 0;
private String clientName;
private boolean convertPipelineAndTxResults = true;
private RedisSentinelConfiguration sentinelConfig;
private RedisClusterConfiguration clusterConfig;
private JedisCluster cluster;
private ClusterCommandExecutor clusterCommandExecutor;
public final class Protocol {
private static final String ASK_RESPONSE = "ASK";
private static final String MOVED_RESPONSE = "MOVED";
private static final String CLUSTERDOWN_RESPONSE = "CLUSTERDOWN";
private static final String BUSY_RESPONSE = "BUSY";
private static final String NOSCRIPT_RESPONSE = "NOSCRIPT";
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = 6379;
public static final int DEFAULT_SENTINEL_PORT = 26379;
public static final int DEFAULT_TIMEOUT = 2000;
public static final int DEFAULT_DATABASE = 0;
public static final String CHARSET = "UTF-8";
public static final byte DOLLAR_BYTE = '$';
public static final byte ASTERISK_BYTE = '*';
public static final byte PLUS_BYTE = '+';
public static final byte MINUS_BYTE = '-';
public static final byte COLON_BYTE = ':';
public static final String SENTINEL_MASTERS = "masters";
public static final String SENTINEL_GET_MASTER_ADDR_BY_NAME = "get-master-addr-by-name";
public static final String SENTINEL_RESET = "reset";
public static final String SENTINEL_SLAVES = "slaves";
public static final String SENTINEL_FAILOVER = "failover";
public static final String SENTINEL_MONITOR = "monitor";
public static final String SENTINEL_REMOVE = "remove";
public static final String SENTINEL_SET = "set";
看到没?底层框架就默认连接了private String hostName = "localhost";和
public static final int DEFAULT_PORT = 6379;
分享到:
相关推荐
RedisTemplate, Object> redisTemplate(RedisConnectionFactory factory)自定义RedisTemplate配置类,是个固定模板,可以拿来直接使用,可以直接传入Key:String和Value:Object这种类型,注意Object类需要实现序列化...
public RedisTemplate, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate, Object> template = new RedisTemplate(); template.setConnectionFactory(factory); // 配置序列化方式,...
Redis+RedisTemplate分布式锁
"通过RedisTemplate连接多个...本文详细介绍了通过RedisTemplate连接多个Redis数据库的过程,包括配置文件的设置、添加RedisTemplate的Bean、连接多个Redis数据库等内容,对大家的学习或者工作具有一定的参考学习价值。
10. **性能优化**:优化Redis性能包括设置合适的内存大小、调整缓存策略、选择合适的数据结构以及正确使用pipeline或multi命令来减少网络通信次数。 综上所述,"redistemplate"可能是一个封装了上述特性的Redis...
1. 配置Redis连接工厂,如`JedisConnectionFactory`,并设置相应的连接参数。 2. 在Spring配置中声明`RedisTemplate`,并指定键值序列化器(如StringRedisSerializer)和其他配置项。 3. 通过Spring容器获取`...
包含SSM+redisTemplate+shiro
public RedisTemplate, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate, Object> template = new RedisTemplate(); template.setConnectionFactory(factory); // 使用JSON序列化 ...
RedisTemplate 默认使用 JdkSerializationRedisSerializer 进行序列化和反序列化,但这种方式的效率并不高,且可能会出现兼容性问题。你可以自定义序列化器,如使用Jackson2JsonRedisSerializer,确保存取数据时的...
详细介绍RedisTemplate5种数据结构,并且有每种不同数据结构的用法的例子
同时,我们还需要配置RedisTemplate的bean,包括设置KeySerializer、ValueSerializer和HashKeySerializer、HashValueSerializer等,以确保数据在传输过程中的正确序列化和反序列化。 接着,RedisTemplate提供了丰富...
RedisTemplate是Spring Data Redis模块中的一个核心组件,用于在Java应用中方便地操作Redis数据库。在RedisTemplate中,opsForZSet方法提供了对有序集合(Sorted Set)的操作接口。有序集合是Redis中一种非常重要的...
public RedisTemplate, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate, Object> template = new RedisTemplate(); template.setConnectionFactory(factory); // 配置序列化器等... ...
redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } } ``` 这里的 `redisConnectionFactory()` 方法用于创建连接工厂。 三、执行 Lua 脚本 在 Spring Boot 中,需要使用 ...
Spring 专门提供了进行 Redis 操作的抽象 RedisTemplate,底层依然使用 Jedis 或 lettuce。SpringBoot 2 默认使用 lettuce。 Redis 安装 请参考:带你学 Redis:Redis安装(二) 代码案例 引入 spring-boot-starter-...
// 默认是0,这里设置为1 return template; } } ``` 现在我们有了两个不同数据库的`RedisTemplate`,可以在业务代码中根据需求选择使用。例如,如果某个服务需要使用数据库1,我们可以这样注入: ```java @...
Redis之RedisTemplate使用(RedisTemplate工具类) String类型相关操作 List类型相关操作 Set类型相关操作 Map类型相关操作 ZSet类型相关操作
在Spring框架中,RedisTemplate的配置通常会在`application.yml`或`application.properties`中进行,通过`spring.data.redis`相关的配置项来设置Redis服务器的地址、端口、密码等信息。例如: ```yaml spring: ...
public RedisTemplate, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate, Object> template = new RedisTemplate(); template....