zoukankan      html  css  js  c++  java
  • SpringBoot整合NoSql--(一)Redis

    简介: 

      Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

    1.首先在虚拟机上的Centos上安装完成redis,并且完成redis.conf文件的配置(后台启动,密码,IP等等),启动redis-server redis.conf

    2. 查看是否启动:

     

     3.使用win下的RedisDesktopManager连接虚拟机上的redis

     4.创建项目:

    spring-boot-starter-data-redis默认使用的Redis工具是Lettuce,我在这里使用的是Jedis,所以exclusion他
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>io.lettuce</groupId>
                        <artifactId>lettuce-core</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

    配置文件:application.properties,其中需要配置哪些看自己需求,前面几个是需要的

    spring.redis.database=0
    spring.redis.host=192.168.205.100
    spring.redis.port=6379
    spring.redis.password=123456
    #spring.redis.lettuce.pool.max-active=
    #spring.redis.lettuce.pool.max-idle=
    #spring.redis.lettuce.pool.max-wait=
    #spring.redis.lettuce.pool.min-idle=
    #spring.redis.lettuce.shutdown-timeout=
    #连接池最大连接数 spring.redis.jedis.pool.max-active=8 #连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 #连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1ms #连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0

    注意:

      SpringBoot的自动配置类中提供了RedisAutoConfiguration进行Redis配置,源码如下,application.properties中的配置文件将被注入到RedisProperties中,如果开发者没有提供RedisTemplate和StringRedisTemplate这两个类,SpringBoot会默认提供这两个实例。

    @Configuration
    @ConditionalOnClass({RedisOperations.class})
    @EnableConfigurationProperties({RedisProperties.class})
    @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
    public class RedisAutoConfiguration {
        public RedisAutoConfiguration() {
        }
    
        @Bean
        @ConditionalOnMissingBean(
            name = {"redisTemplate"}
        )
        public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
            RedisTemplate<Object, Object> template = new RedisTemplate();
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
    
        @Bean
        @ConditionalOnMissingBean
        public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
            StringRedisTemplate template = new StringRedisTemplate();
            template.setConnectionFactory(redisConnectionFactory);
            return template;
        }
    }

    Controller:

    @RestController
    public class BookController {
        @Autowired
        RedisTemplate redisTemplate;
    
        @Autowired
        StringRedisTemplate stringRedisTemplate;
    
        @GetMapping("/test1")
        public void test1() {
            //使用stringRedisTemplate
            ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
            ops1.set("name", "三国演义");
            String name = ops1.get("name");
            System.out.println(name);
    
    //        使用redisTemplate
            ValueOperations ops2 = redisTemplate.opsForValue();
            Book b1 = new Book();
            b1.setId(1);
            b1.setName("红楼梦");
            b1.setAuthor("曹雪芹");
            ops2.set("b1", b1);
            Book book = (Book) ops2.get("b1");
            System.out.println(book);
        }
    }

    启动,访问http://localhost:8080/test1

    控制台表示,已经成功存储并且可以获取到redis中的数据了:

     redis中:

  • 相关阅读:
    从SVN下检出项目内容【步骤】
    添加购物车,或者存入缓冲中
    接触的电商项目中使用框架编写代码的常用点
    sql语句中【模糊查询like的使用】
    总结:String类型与Int类型的转换【实现插入操作主键自增】
    自定义TextView跑马灯效果
    Re-installation failed due to different application signatures解决方案
    使用Afinal提交的数据到服务器时,数据中带空格或是有换行操作时报错【处理方案】
    解决ScrollView嵌套ListView冲突问题,并且添加阻尼效果
    使用eclipse截取客户端当前页图片
  • 原文地址:https://www.cnblogs.com/crazy-lc/p/12346190.html
Copyright © 2011-2022 走看看