(四)基於SSM+Redis+Nginx+FastDFS的部落格網站
阿新 • • 發佈:2018-11-03
Redis是用C語言開發的一個開源的高效能鍵值對(key-value)資料庫。它通過提供多種鍵值資料型別來適應不同場景下的儲存需求,目前為止Redis支援的鍵值資料型別如字串型別、雜湊型別、列表型別、集合型別、有序集合型別。
菜鳥教程裡有基本的使用命令:點選開啟連結
樓主是使用的阿里雲伺服器,一開始時也是通過直接在阿里雲伺服器裡面安裝redis,阿里有新使用者+9.9的套餐,開通後有贈送redis資料庫,所以就改用贈送的資料庫了,無需自己配置。
下面就講解下直接在centos伺服器上配置redis。redis是6379埠,記得別忘記了設定防火牆規則,如果是自己
本機測試的話,可直接通過service iptables stop來關閉防火牆。
redis是C語言開發,建議在linux上執行,本教程使用Centos6.4作為安裝環境。
安裝redis需要先將官網下載的原始碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc:yum install gcc-c++
版本說明
本教程使用redis3.0版本。3.0版本主要增加了redis叢集功能。
原始碼下載
從官網下載
http://download.redis.io/releases/redis-3.0.0.tar.gz
將redis-3.0.0.tar.gz拷貝到/usr/local下
解壓原始碼
tar -zxvf redis-3.0.0.tar.gz
進入解壓後的目錄進行編譯
cd /usr/local/redis-3.0.0
make
安裝到指定目錄,如 /usr/local/redis
cd /usr/local/redis-3.0.0
make PREFIX=/usr/local/redis install
redis.conf
redis.conf是redis的配置檔案,redis.conf在redis原始碼目錄。
注意修改port作為redis程序的埠,port預設6379。
拷貝配置檔案到安裝目錄下
進入原始碼目錄,裡面有一份配置檔案 redis.conf,然後將其拷貝到安裝路徑下
cd /usr/local/redis
mkdir conf
cp /usr/local/redis-3.0.0/redis.conf /usr/local/redis/bin
直接執行bin/redis-server將以前端模式啟動,前端模式啟動的缺點是ssh命令視窗關閉則redis-server程式結束,不推薦使用此方法。推薦使用後端模式啟動。修改redis.conf配置檔案, daemonize yes 以後端模式啟動。
執行如下命令啟動redis:
cd /usr/local/redis
./bin/redis-server ./redis.conf
啟動成功後,在介面啟動redis客戶端。
cd /usr/local/redis/bin
./redis-cli
後續的可參見菜鳥教程裡的命令使用redis
接下來先測試下在JAVA中通過jedis客戶端訪問redis。
package com.tdrip.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisClientTest {
@Test
public void testSpringJedisSingle() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
Jedis jedis = pool.getResource();
jedis.set("key1", "sosmmh");
String string = jedis.get("key1");
System.out.println(string);
jedis.close();
pool.close();
}
@Test
public void testJedisSingle() {
Jedis jedis = new Jedis("192.168.1.101", 6379);
jedis.set("name", "bar");
String name = jedis.get("name");
System.out.println(name);
jedis.close();
}
@Test
public void pool() {
JedisPoolConfig config = new JedisPoolConfig();
//最大連線數
config.setMaxTotal(30);
//最大連線空閒數
config.setMaxIdle(2);
JedisPool pool = new JedisPool(config, "192.168.1.101", 6379);
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.set("name", "lisi");
String name = jedis.get("name");
System.out.println(name);
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(jedis != null){
//關閉連線
jedis.close();
}
}
}
}
接下來是整合spring:兩個檔案,一個是redis的連線資訊檔案,一個是整合spring的xml檔案
- redis.properties
redis.host=192.168.1.101 redis.port=6379 redis.timeout=2000 redis.password=foobared
- applicationContext-jedis.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 連線池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大連線數 --> <property name="maxTotal" value="30" /> <!-- 最大空閒連線數 --> <property name="maxIdle" value="10" /> <!-- 每次釋放連線的最大數目 --> <property name="numTestsPerEvictionRun" value="1024" /> <!-- 釋放連線的掃描間隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 連線最小空閒時間 --> <property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 連線空閒多久後釋放, 當空閒時間>該值 且 空閒連線>最大空閒連線數 時直接釋放 --> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <!-- 獲取連線時的最大等待毫秒數,小於零:阻塞不確定的時間,預設-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在獲取連線的時候檢查有效性, 預設false --> <property name="testOnBorrow" value="true" /> <!-- 在空閒時檢查有效性, 預設false --> <property name="testWhileIdle" value="true" /> <!-- 連線耗盡時是否阻塞, false報異常,ture阻塞直到超時, 預設true --> <property name="blockWhenExhausted" value="false" /> </bean> <!-- redis單機 通過連線池 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close"> <constructor-arg name="poolConfig" ref="jedisPoolConfig"/> <constructor-arg name="host" value="${redis.host}"/> <constructor-arg name="port" value="${redis.port}"/> <constructor-arg name="timeout" value="${redis.timeout}"/><!-- redis有配置密碼的話設定此項 --> <constructor-arg name="password" value="${redis.password}" /> </bean> <bean id="jedisClient" class="com.tdrip.dao.impl.JedisClientSingle"/> </beans>