CentOS7下安裝Redis — 單節點

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

1. 環境準備

??? 安裝編譯所需要的包:

yum install gcc tcl

2. 下載redis

http://download.redis.io/releases/redis-3.2.7.tar.gz

3. 安裝redis

## 創建redis的安裝目錄
mkdir /usr/local/redis## 解壓redis
tar -zxvf redis-3.2.7.tar## 重命名安裝目錄
mv redis-3.2.7.tar /opt/redis## 進入安裝目錄
cd /opt/redis## 編譯安裝redis
make PREFIX=/usr/local/redis install

??? 安裝完成后,進入redis的目錄下會有一個bin目錄,目錄中有redis的腳本:

## redis的腳本
redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-sentinel  redis-server

4. 將redis啟動腳本

## 拷貝redis的啟動腳本到init.d目錄
cp /opt/redis/utils/redis_init_script /etc/rc.d/init.d/redis## 編輯腳本
vi /etc/rc.d/init.d/redis

??? 需要修改這個腳本,修改后的腳本如下:

#!/bin/sh
#chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cliPIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/${REDISPORT}.conf"case "$1" instart)if [ -f $PIDFILE ]thenecho "$PIDFILE exists, process is already running or crashed"elseecho "Starting Redis server..."$EXEC $CONF &fi;;stop)if [ ! -f $PIDFILE ]thenecho "$PIDFILE does not exist, process is not running"elsePID=$(cat $PIDFILE)echo "Stopping ..."$CLIEXEC -p $REDISPORT shutdownwhile [ -x /proc/${PID} ]doecho "Waiting for Redis to shutdown ..."sleep 1doneecho "Redis stopped"fi;;*)echo "Please use start or stop as first argument";;
esac

5. 將redis注冊為服務

## 注冊redis腳本為服務
chkconfig --add redis## 開啟服務
systemctl start redis.service## 開啟啟動服務
systemctl enable redis.service

6. 設置防火墻

## 開放端口
firewall-cmd --zone=public --add-port=6379/tcp --permanent## 重啟防火墻
firewall-cmd --reload

7. 修改redis的配置文件

## 創建配置文件的目錄
mkdir /usr/local/redis/conf## 服務redis配置文件到這個目錄
cp /opt/redis/redis.conf /usr/local/redis/conf/6379.conf## 編輯配置文件
vi /usr/local/redis/conf/6379.conf

??? 將daemonize改為yes,并開放遠程連接,修改bind為0.0.0.0。

8. 添加環境變量

## 編輯profile文件
vi /etc/profile## 加入redis環境變量
#redis env
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin## 編譯修改后的profile
source /etc/profile## 重啟redis服務
systemctl restart redis.service

9. 通過redis-cli測試:

## 命令行輸入
redis-cli## 如果成功連接redis會出現
127.0.0.1:6379> 

10. 使用jedis連接

??? 下載jedis的jar包:

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

??? 編寫代碼連接redis:

package cn.net.bysoft.redis.test;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;import redis.clients.jedis.Jedis;public class RedisTest {private final Log log = LogFactory.getLog(RedisTest.class);@Testpublic void test() {String key = "hello";Jedis jedis = new Jedis("192.168.240.132", 6379);jedis.set(key, "world");String str = jedis.get(key);log.info("==>" + str);jedis.del(key);str = jedis.get(key);log.info("==>" + str);jedis.close();}}

??? 運行上面的代碼,成功連接的話,會輸出:

2017-02-11 14:10:39,748  INFO [RedisTest.java:20] : ==>world
2017-02-11 14:10:39,753  INFO [RedisTest.java:24] : ==>null

11. 使用spring連接redis

??? 下載jar包:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version>
</dependency>

??? 連接池的配置文件:

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- Jedis鏈接池配置 --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="testWhileIdle" value="true" /><property name="minEvictableIdleTimeMillis" value="60000" /><property name="timeBetweenEvictionRunsMillis" value="30000" /><property name="numTestsPerEvictionRun" value="-1" /><property name="maxTotal" value="8" /><property name="maxIdle" value="8" /><property name="minIdle" value="0" /></bean><bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"><constructor-arg index="0" ref="jedisPoolConfig" /><constructor-arg index="1"><list><bean class="redis.clients.jedis.JedisShardInfo"><constructor-arg index="0" value="192.168.240.132" /><constructor-arg index="1" value="6379" type="int" /></bean></list></constructor-arg></bean>
</beans>

??? spring配置文件:

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.2.xsd"default-autowire="byName" default-lazy-init="false"><!-- 采用注釋的方式配置bean --><context:annotation-config /><!-- 配置要掃描的包 --><context:component-scan base-package="cn.net.bysoft.redis.test" /><!-- proxy-target-class默認"false",更改為"ture"使用CGLib動態代理 --><aop:aspectj-autoproxy proxy-target-class="true" />	<import resource="spring-redis.xml" />
</beans>

??? 測試代碼:

package cn.net.bysoft.redis.test;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-context.xml" })
public class RedisSpringTest {private static final Log log = LogFactory.getLog(RedisSpringTest.class);@Autowiredprivate ShardedJedisPool shardedJedisPool;@Testpublic void test() {ShardedJedis jedis = shardedJedisPool.getResource();String key = "hello";String value = "";jedis.set(key, "world");value = jedis.get(key);log.info(key + "=" + value);jedis.del(key); // 刪數據value = jedis.get(key); // 取數據log.info(key + "=" + value);}}

??? 如果成功連接,會輸出:

2017-02-11 14:19:56,776  INFO [AbstractTestContextBootstrapper.java:258] : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
2017-02-11 14:19:56,793  INFO [AbstractTestContextBootstrapper.java:207] : Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
2017-02-11 14:19:56,796  INFO [AbstractTestContextBootstrapper.java:185] : Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@7ca48474, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@337d0578, org.springframework.test.context.support.DirtiesContextTestExecutionListener@59e84876, org.springframework.test.context.transaction.TransactionalTestExecutionListener@61a485d2, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@39fb3ab6]
2017-02-11 14:19:56,903  INFO [XmlBeanDefinitionReader.java:317] : Loading XML bean definitions from class path resource [spring/spring-context.xml]
2017-02-11 14:19:57,127  INFO [XmlBeanDefinitionReader.java:317] : Loading XML bean definitions from class path resource [spring/spring-redis.xml]
2017-02-11 14:19:57,160  INFO [AbstractApplicationContext.java:578] : Refreshing org.springframework.context.support.GenericApplicationContext@3532ec19: startup date [Sat Feb 11 14:19:57 CST 2017]; root of context hierarchy
2017-02-11 14:19:57,455  INFO [RedisSpringTest.java:31] : hello=world
2017-02-11 14:19:57,457  INFO [RedisSpringTest.java:35] : hello=null
2017-02-11 14:19:57,459  INFO [AbstractApplicationContext.java:960] : Closing org.springframework.context.support.GenericApplicationContext@3532ec19: startup date [Sat Feb 11 14:19:57 CST 2017]; root of context hierarchy

?

轉載于:https://my.oschina.net/u/2450666/blog/835977

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457213.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457213.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457213.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

筆記本中美化代碼的方法

這里向大家推薦一個很好用的記筆記軟件,微軟的OneNote,這個筆記軟件,支持分區和分區組的創建,而且入門簡單,界面簡潔,很適合從word過渡過來的人來記筆記! 不過如果直接記筆記,對于程序員來說,可能希望代碼在筆記本上更好看一些,那么應該怎么辦呢?下面提供了在OneNote中,讓代碼…

工具使用——印象(匯總)

作者&#xff1a;桂。 時間&#xff1a;2017-02-09 23:11:30 鏈接&#xff1a;http://www.cnblogs.com/xingshansi/articles/6384097.html 說明&#xff1a;轉載請注明出處&#xff0c;謝謝。 前言 本文僅僅介紹印象筆記的使用&#xff0c;至于挖掘機哪家強&#xff0c;本文不…

java final修飾屬性_Java final關鍵字用來修飾類、方法、屬性

1.final修飾類&#xff1a;這個類不能被繼承。如&#xff1a;String類、StringBuffer類、System類。2.final修飾方法&#xff1a;不能被重寫。如&#xff1a;Object類的getClass()方法。3.final修飾屬性&#xff1a;此屬性就是一個常量&#xff0c;一旦初始化就不可再被賦值。習…

SQL SERVER 數據導出JSON

執行下面的存儲過程&#xff1a; SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE[dbo].[SerializeJSON](ParameterSQL AS VARCHAR(MAX))ASBEGINDECLARE SQL NVARCHAR(MAX)DECLARE XMLString VARCHAR(MAX)DECLARE XML XMLDECLARE Paramlist NVARCHAR(1000)SET …

JSP+Javabean+Servlet實現用戶注冊

在entity包下新建javabean 也就是實體類User 注意id用 Integer 而不用 int&#xff0c; 因為 int 自動初始化為0 public class User { private Integer id; private String username; private String password; 后面是set和get方法... 在Servlet包下創建servlet 右擊Servlet…

main的方法是Java_Java中的main()方法

在Java中&#xff0c;main()方法是Java應用程序的入口方法&#xff0c;也就是說&#xff0c;程序在運行的時候&#xff0c;第一個執行的方法就是main()方法&#xff0c;這個方法和其他的方法有很大的不同&#xff0c;比如方法的名字必須是main&#xff0c;方法必須是public sta…

深入理解Python的logging模塊:從基礎到高級

在Python編程中&#xff0c;日志記錄是一種重要的調試和錯誤追蹤工具。Python的logging模塊提供了一種靈活的框架&#xff0c;用于發出日志消息&#xff0c;這些消息可以被發送到各種輸出源&#xff0c;如控制臺、文件、HTTP GET/POST位置等。本文將深入探討Python的logging模塊…

http請求連接

1、在Info.plist中添加NSAppTransportSecurity類型Dictionary。2、在NSAppTransportSecurity下添加NSAllowsArbitraryLoads類型Boolean,值設為YES轉載于:https://www.cnblogs.com/liuting-1204/p/5919233.html

數據庫不完全恢復 以及恢復到測試環境:

sample 1: 1.清空歸檔日志 RMAN> crosscheck archivelog all; RMAN> delete achivelog all; 2.清空數據文件。 select name from v$datafile; rm v$datafile 3.恢復數據 ##check file date: ##把db數據恢復到&#xff1a;2017-02-05 00:00:00 ls -lt /ngenprdblog/ ls…

centos7安裝java6_CentOS7.6安裝jdk1.8

2、登錄Linux服務器&#xff0c;通過rz命令將jdk導入服務器如果沒有rz命令 需要先安裝lrzszyum install lrzsz -y3、將jdk壓縮包解壓到指定路徑 -C 指定路徑4、配置環境變量編輯/etc/profile文件 在末尾加上以下內容 wq保存退出source /etc/profile文件 使配置文件生效export J…

ubuntu安裝wkhtmltopdf

下載安裝wkhtmltox系統環境 http://wkhtmltopdf.org/downloads.html wget https://bitbucket.org/wkhtmltopdf/wkhtmltopdf/downloads/wkhtmltox-0.13.0-alpha-7b36694_linux-precise-amd64.deb dpkg -i 安裝包名字 當我把它生成pdf的時候我想讓每個塊都是一頁&#xff0c;經過…

人生苦短,我用python——當我在玩python的時候我玩些什么 -

程序的基本思路 用一個txt文件記錄電腦的一天內累計使用時間累計使用時間超過若干小時就會自動關機程序開機自動運行 為什么我最后選擇了python 想著怎么寫、搜資料的時候就發現Java并不適合&#xff0c;雖然不是不能實現&#xff0c;但有好幾個問題解決起來都有點麻煩。對我這…

IO流的練習5 —— 讀取文件中的字符串,排序后寫入另一文件中

需求&#xff1a;已知s.txt文件中有這樣的一個字符串&#xff1a;“hcexfgijkamdnoqrzstuvwybpl”     請編寫程序讀取數據內容&#xff0c;把數據排序后寫入ss.txt中。分析&#xff1a;   A&#xff1a;讀取文件中的數據   B&#xff1a;把數據存在一個字符串中   C…

java解析未知key json_Gson解析JSON中動態未知字段key的方法

前面一篇文章我介紹了Gson的解析的基本方法。但我們在享受Gson解析的高度封裝帶來的便利時&#xff0c;有時可能會遇到一些特殊情況&#xff0c;比如json數據中的字段key是動態可變的時候&#xff0c;由于Gson是使用靜態注解的方式來設置實體對象的&#xff0c;因此我們很難直接…

Twisted入門教程(5)

2019獨角獸企業重金招聘Python工程師標準>>> 第五部分&#xff1a;由Twited支持的詩歌下載服務客戶端 你可以從這里從頭開始閱讀這個系列 抽象地構建客戶端 在第四部分中&#xff0c;我們構建了第一個使用Twisted的客戶端。它確實能很好地工作&#xff0c;但仍有提高…

Jquery 學習之基礎一

1.添加一個CSS類 $("button").click(function(){ $("#div1").addClass("important blue");}); 2.移除一個類 $("button").click(function(){ $("h1,h2,p").removeClass("blue");}); 3.切換類 $("button&…

**print('人生苦短 我愛Python')**

print(‘人生苦短 我愛Python’) 一、變量 **""" 1.代碼自上而下執行 2_運算符和表達式.一行一句&#xff0c;不要把多個語句寫到一行上&#xff0c;可讀性不好 3中文只能出現在引號里&#xff0c;其他地方不能出現中文 4不能隨意縮進 """**pr…

java線程提高速度_如何在JAVA中減慢線程速度

我有這個類,我在其中運行10次for循環.該類實現了Runnable接口.現在在main()中我創建了2個線程.現在兩個都將循環運行到10.但我想檢查每個線程的循環計數.如果t1超過7,則讓它休眠1秒,以便讓t2完成.但是如何實現這一目標呢&#xff1f;請參閱代碼.我嘗試但看起來完全愚蠢.只是如何…

(轉ORCLE導入導出命令)

oracle數據庫導入導出命令&#xff01;Oracle數據導入導出imp/exp 功能&#xff1a;Oracle數據導入導出imp/exp就相當與oracle數據還原與備份。 大多情況都可以用Oracle數據導入導出完成數據的備份和還原&#xff08;不會造成數據的丟失&#xff09;。 Oracle有個好處&…

筆記本(華碩UL80VT)軟件超頻setFSB

Warning !!!If you are a beginner, do not use this software. This software is for power users only. Use "SetFSB.exe" at your own risk.試了setfsb各種版本&#xff0c;基本不能打開。還有官網的免費版&#xff0c;居然不能用&#xff0c;真是很奇怪。 官網&a…