spring boot + redis4.0.2 详细案例

本文大纲

       1.从无到有。在centos7 中安装redis4.0

       2.spring boot 配置 redis 案例

       3.使用总结

一:从无到有。在centos7 中安装redis4.0

       1.安装 oracle vm virtualBox 虚拟机。

           下载地址:http://download.virtualbox.org/virtualbox/5.2.0/VirtualBox-5.2.0-118431-Win.exe。运行安装即可

           配置项:网络链接方式---桥接网卡

       2.安装centos7

          2.1下载地址:http://mirrors.aliyun.com/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso。

          2.2添加虚拟系统。选择刚下载的centos7系统

          2.3装机完成后需要配置静态IP地址,参考博客  http://blog.csdn.net/johnnycode/article/details/50184073 。里面有详细的配置方式。

          2.4关闭防火墙 iptables

       3.安装redis4.0

          3.1 wget http://download.redis.io/releases/redis-4.0.2.tar.gz /usr/local/redis 将redis4下载到指定位置,我个人习惯放再user/local下面

          3.2 解压  

                # cd /usr/local/redis

                # tar xzvf redis-4.0.2.tar.gz

          3.3 然后执行make编译源码

                # cd redis-4.0.2

                # make

          3.4.make命令执行完成后,会在src目录下生成6个可执行文件,

                分别是redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump、redis-sentinel。我们只需要将redis-server,redis-cli 拷贝到/usr/local/bin/下面即可

          3.5 因为我们需要使用spring boot 去操作redis。所以要修改redis.conf 配置文件。

               # vi /usr/local/redis-4.0.2/redis.conf

                 - 1.将 bind 127.0.0.1  注释掉

                 - 2.设置访问密码  requirepass 123456

          3.6 启动 redis

              # redis-server /usr/local/redis-4.0.2/redis.conf

          3.7 测试-需要另开一个客户端

              # redis-cli

              127.0.0.1:6379> auth 123456   -- 上面配置的密码

              127.0.0.1:6379> set name csdn
              OK
              127.0.0.1:6379> get name
             "csdn"
              

二。创建spring boot (我是用的eclipse 版本是 Neon 4.6 RC3)

       1.新建spring boot 项目。选择redis

       

       2.修改application.yml 配置文件

      spring:
          redis:
              host: 192.168.1.xx
              port: 6379
              password: 123456

      3.添加redis组件

         

      @Component
      public class RedisComponent {

          @Autowired
          private StringRedisTemplate stringRedisTemplate;

          public void set(String key, String value) {
               ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
               if (!this.stringRedisTemplate.hasKey(key)) {
                   ops.set(key, value);
                    System.out.println("set key success");
               } else {
              // 存在则打印之前的value值
                  System.out.println("this key = " + ops.get(key));
                }
           }

            public String get(String key) {
                 return this.stringRedisTemplate.opsForValue().get(key);
           }

           public void del(String key) {
                this.stringRedisTemplate.delete(key);
           }

    }

       4.测试类

      


     

      @Autowired
      private RedisComponent redisComponent;
      @Test
      public void set() {
          roncooRedisComponent.set("name", "今天是个好日子");
      }
     @Test
     public void get() {
         System.out.println(roncooRedisComponent.get("name"));
     }
 
      @Test
      public void del() {
         roncooRedisComponent.del("name");
      }



     5.测试结果
        
     测试  set 
     


                

             测试  get

      

    测试  del

  

    三:使用总结

         这次做boot+redis配置遇到了一个小问题。就是没有配置redis密码。导致boot访问被拒。算是一个小小的经验教训吧。

猜你喜欢

转载自blog.csdn.net/jamesi987/article/details/78577663