Spring Boot redis 配置

銜接上篇文章 :Spring Boot 項目集成 Swagger 實例文檔 

瞭解 Redis 並在 Spring Boot 項目中使用 Redis

Spring中使用RedisTemplate操作Redis

redis修改密碼不生效問題

 

redis.conf 參數配置

redisConfig類:

@Configuration
public class RedisConfig {

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(
            RedisConnectionFactory redisConnectionFactory) {

        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        //template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
            RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

redis依賴:

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

linux環境下redis.conf配置文件修改:

用到的命令:

編輯文件
vim redis.conf

關鍵字搜索 
/關鍵字
鍵入 n 查找下一出

修改 
    鍵入  i
退出
    鍵入 esc
保存 
    :wq
不保存
    :q!

配置連接密碼:

requirepass root

啓用守護進程: 

daemonize yes

取消保護模式:

protected-mode no

 註釋掉bind 127.0.0.1 關閉只能本機訪問的限制:

#bind 127.0.0.1

防火牆配置  開放6379端口:

查看6379防火牆狀態
firewall-cmd --zone=public --query-port=6379/tcp

開放端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent 

重載  ==>執行此命令 開放端口才能生效
firewall-cmd --reload

再次查看   返回爲yes 即表示配置成功
firewall-cmd --zone=public --query-port=6379/tcp

修改完配置文件 記得重啓服務 :!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

./redis-server ./redis.conf

 

redis配置範例:

有個問題:springboot 2.x版本中默認客戶端是用 lettuce實現的 ,若將配置文件中的jedis 替換爲lettuce則項目啓動報錯

springboot系列文章之 集成redis 服務 (Lettuce & Jedis)

  • application.properties中配置Redis連接信息
# Redis數據庫索引(默認爲0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=192.168.200.129
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認爲空)
spring.redis.password=root
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-idle=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=-1
# 連接池中的最大空閒連接
spring.redis.jedis.pool.max-active=5
# 連接池中的最小空閒連接
spring.redis.jedis.pool.min-idle=1
# 連接超時時間(毫秒)
spring.redis.timeout=5000

測試redis存儲對象

打印結果:People(name=ww, address=ee)----------------------{name=jack, class=1, age=27}

//  redisTemplate.setKeySerializer(new StringRedisSerializer());  設置key的編碼
        //方式一:
        redisTemplate.opsForValue().set("p", new People("ww", "ee"));
        People p = (People) redisTemplate.opsForValue().get("p");
        //方式二:
        Map<String, Object> testMap = new HashMap();
        testMap.put("name", "jack");
        testMap.put("age", 27);
        testMap.put("class", "1");
        redisTemplate.opsForHash().putAll("hash1", testMap);
        //
        System.out.println(p + "----------------------" + redisTemplate.opsForHash().entries("hash1"));

關於redis集羣:

Redis 集羣教程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章