linux搭建redis以及在Spring中配置

linux搭建redis

1.下载

wget http://download.redis.io/releases/redis-5.0.4.tar.gz

2.解压

tar -zxvf redis-5.0.4.tar.gz

3.编译安装

cd redis-5.0.4
make MALLOC=libc
cd src && make install

4.修改配置脚本

vim redis-5.0.4/reids.conf
  1. 输入 /requirepass foobared 回车,将#去掉 foodbared改为自己的密码
  2. 输入 /bind 127.0.0.1 回车,将其#,或者地址改为0.0.0.0,开放公网访问
  3. 输入 /port 6379 回车,可以修改端口号
  4. 输入 /protected-mode yes 回车,改为no,公网可以访问
  5. 输入 /daemonize no 回车,改为yes,以后台进程启动redis

5.设置开机启动

 1.创建启动文件

这个文件名是什么名字,后面启动就用什么名字

vim /etc/init.d/redis

 2. 添加文件内容

#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
### BEGIN INIT INFO
# Provides:     redis_6379
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    Redis data structure server
# Description:          Redis data structure server. See https://redis.io
### END INIT INFO

REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli

PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"

case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis server..."
                $EXEC $CONF
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -a "password" -p $REDISPORT shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis to shutdown ..."
                    sleep 1
                done
                echo "Redis stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

内容是从 redis5.0.4/utils/redis_init_script cpye的不过要在最开始添加

# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database

用来设置chkconfig命令

 3. 创建相应文件

REDISPORT=6379  # 端口号
EXEC=/usr/local/bin/redis-server # 执行脚本的地址
CLIEXEC=/usr/local/bin/redis-cli # 客户端执行脚本的地址
PIDFILE=/var/run/redis_${REDISPORT}.pid # 进程id文件地址,启动redis后才能看见
CONF="/etc/redis/${REDISPORT}.conf" #配置文件地址
# 如果设置密码这里需要注意 否则无法stop
$REDIS_CLI -a "password" -p $REDISPORT SHUTDOWN   # password 改为密码如果没有密码 删除 -a "password"

源码中配置文件保存在/etc/redis/${REDISPORT}.conf,根据端口号创建配置文件即可并将文件复制到相应目录

mkdir /etc/redis
cp redis5.0.4/redis.conf  /etc/redis/6379.conf

 4. 设置权限

chmod 755 /etc/init.d/redis

 5. 启动测试

/etc/init.d/redis start

 6. 设置开机启动

chkconfig --add /etc/init.d/redis
chkconfig redis on

 失败问题排查

查看第一步的配置文件信息,是否和第二步的文件信息一致,vim粘贴会出现粘贴不全的问题

6. redis的重启

# redis启动
./src/redis-server redis.conf
service redis start

# redis的关闭
# 查看进程号
ps -ef | grep redis
# kill
kill 进程号 
service redis stop

Spring中配置redis

1.maven依赖

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>

2. xml文件

创建spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans.xsd
        				http://www.springframework.org/schema/mvc
                         http://www.springframework.org/schema/mvc/spring-mvc.xsd
        				http://code.alibabatech.com/schema/dubbo
                         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        				http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context.xsd">

    <!--Jedis连接池的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal">
            <value>200</value>
        </property>
        <property name="maxIdle">
            <value>50</value>
        </property>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>
    
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="127.0.0.1" />
        <constructor-arg name="port" value="6379" type="int" />
        <constructor-arg name="timeout" value="30000" type="int" />
        <constructor-arg name="password" value="123456" type="java.lang.String" />
        <!-- 0代表 Protocol.DEFAULT_DATABASE -->
        <constructor-arg name="database" value="0" type="int" />
    </bean>
</beans>

导入到spring-mvc.xml中

    <import resource="spring-redis.xml" />

3.java代码

在service中使用即可

 @Autowired
 JedisPool jedisPool;

redis查看工具

链接: RedisDesktopManager.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值