首页 > 数据库 >linux 安装运行redis与Spring连接配置,10分钟快速入门

linux 安装运行redis与Spring连接配置,10分钟快速入门

时间:2022-12-18 00:33:20浏览次数:62  
标签:10 Spring redis springframework redisTemplate import org RedisTemplate

linux是一方面是开源免费的,另一面其长时间运行的可靠性远高于其他系统,所以是目前国内绝大多数公司部署项目的首选。其中部署项目往往需要配套诸如Mysql、maven、redis等等应用服务。最近学习了在lLinus上安装运行redis所以做一个记录,方便日后用到是查阅。如果能帮到有需要的人也是挺好的。目的是快速

步骤主要分为两部分:

一、linux 安装运行:1.下载安装 || 2.防火墙设置 || 3.运行启动三部分;

二、redis在SpringBoot连接配置使用:1.Spring项目连接redis配置 || 2.Springboot项目使用redis快速入门案例

1.下载安装

1)Linux版下载地址: https://download.redis.io/releases/

2)将文件安装包上传到Linux的soft目录下

 

 

3)到soft文件夹解压安装包到usr/local文件夹下

cd /soft
tar -zxvf redis-4.0.0.tar.gz -C /usr/local

4)安装Redis依赖的gcc环境

yum install gcc-c++

5)进入redis解压的文件下,执行编译并安装

cd /usr/local/redis-4.0.0
make && make install

6)安装完成后进入src文件夹,把上一层的执行文件redis.conf,拷贝到当前的src目录下

cd /usr/local/redis-4.0.0/src
cp ../redis.conf .

7)修改redis.conf文件,让他在后台启动不要霸屏,影响操作其他程序。将文件中的daemonize no 修改为daemonize yes。redis服务默认是只允许本机连接,所以需要修改文件,设置外部服务器范围白名单:将bin bind 127.0.0.1修改为bind 127.0.0.1 192.168.208.128 (空格+白名单服务器地址)

vim redis.conf

2.修改防火墙设置,让Linux允许外部服务器方位redis

1)开放redis程序防火墙允许外部服务器访问

firewall--cmd --zone=public --add-port=6379/tcp --permanent

2)重新加载防火墙

firewall-cmd --reload

3)检查防火墙应用端口开放情况,看到6379端口即为设置成功

firewall-cmd --list-port

 

 

 3.运行redis服务

1)跳转到应用程序src文件夹

cd /usr/local/redis_4.0.0/src

2)打开运行文件

./redis-server redis.conf

 

 二、SpringBoot在连接配置使用redis

1、连接配置

1)在项目的pom.xml文件中导入spring data redis的maven坐标

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

2)在项目的application.yml中加入redis相关配置

 spring:
    redis:
        host: 192.168.78.128   #Linux主机地址
        port: 6379    
        database: 0
        jedis:
          pool:
            max-active: 10
            max-idle: 5
  #      password:没有密码可以不写    

3)编写Redis的配置类RedisConfig定义RedisTemplate

package com.itheima.reggie.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        //设置键序列化器,讲key序列化,方便后续查看缓存内容
        template.setKeySerializer(new StringRedisSerializer());
        return template;
    }
}

2.应用入门案例

@Test
public class RedisTest {
  @Autowired //注入RedisTemplate对象,用于操作Redis
  private RedisTemplate redisTemplate
public void redisTest1() {
        redisTemplate.opsForValue().set(key1,"这是第一个redis缓存",3, TimeUnit.MINUTES);//存入一个键为key1的redis,存活时间为3分钟
     redisTemplate.opsForValue().set(key2,"这是第二redis缓存",3, TimeUnit.MINUTES);//存入一个键为key2的redis,存活时间为3分钟
     Set keys = redisTemplate.keys("key*");//获取所有以key开沟的所有键
     redisTemplate.delete(keys);//删除所有这些key

上面的简单案例只是redis的缓存增删操作,redis还有不同数据类型的命令需要学习,这些只是个人的学习记录,后面学习到根深或者跟多的应用的时候再分享吧。希望对你也能有帮助,欢迎一起讨论。

标签:10,Spring,redis,springframework,redisTemplate,import,org,RedisTemplate
From: https://www.cnblogs.com/goldofwater/p/16989880.html

相关文章