ssm框架的maven web專案整合redis服務
阿新 • • 發佈:2018-11-06
一、Linux下redis的安裝
單檯安裝,測試用
開啟https://redis.io/download,往下翻,Installation ,有詳細的安裝、啟動和簡單測試的命令
二、整合redis服務
下面就是在study專案中整合redis服務(根據自己的專案進行相應的修改):
1、pom.xml檔案中增加redis需要的jar包配置
<properties> <redis.version>2.9.0</redis.version> </properties>View Code<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${redis.version}</version> </dependency>
2、applicationContext.xml檔案增加配置
<!-- jedisPool配置資訊 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxWaitMillis" valueView Code="${redis.maxWaitMillis}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!--配置redis資料來源--> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy"> <constructor-arg name="poolConfig" ref="poolConfig" /> <!-- 載入jedisPool配置資訊 --> <constructor-arg name="host" value="${redis.host}" /><!-- redis主機地址 --> <constructor-arg name="port" value="${redis.port}"/> <!-- redis連線埠 --> <!-- <constructor-arg name="password" value="${redis.pass}"/> --> <!-- 密碼 --> <!--<constructor-arg name="database" value="${redis.database}"/> 資料庫 --> <constructor-arg name="timeout" value="${redis.timeout}"/> <!--連線超時 --> </bean>
3、增加redis.properties配置檔案,路徑:src/main/resources
# Redis配置
redis.host=192.168.25.128
redis.port=6379
#驗證密碼
redis.pass=
# 超時時間
redis.timeout=2000
# 最大連線數
redis.maxTotal=100
# 最大空閒數
redis.maxIdle=10
# 最小空閒數
redis.minIdle=5
redis.maxActive=600
# 獲取連線時等待的最長時間
redis.maxWaitMillis=1000
# 測試連線可用性
redis.testOnBorrow=true
View Code
4、新增redisService.java檔案,只寫了幾個簡單的方法
package com.su.study.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service("redis")
public class RedisService {
/**
* 連線池
*/
@Autowired
private JedisPool jedisPool;
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String get(String key) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String setex(String key, String value, int seconds) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.setex(key, seconds, value);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Long del(String key) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Boolean exists(String key) {
Jedis jedis = jedisPool.getResource();
try {
return jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
View Code
5、新增TestController.java檔案
package com.su.study.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.su.study.service.RedisService;
@Controller
@RequestMapping("/test")
public class TestController {
/**
* redis服務
*/
@Autowired
private RedisService redis;
@RequestMapping(value = "/redis", method=RequestMethod.POST)
@ResponseBody
public Object redisTest() {
redis.set("test", "abc");
return redis.get("test");
}
}
View Code
啟動專案,使用postman模擬請求,http://localhost:8080/study/test/redis
檢視redis
可能遇到的問題:
i、專案連不上redis
redis拒絕我們的連線是因為redis.conf 檔案中預設開啟bind 127.0.0.1,開啟這個設定後redis 只接收來自於該 IP 地址的請求,如果不進行設定,那麼將處理所有請求,所以可以把這個註釋掉或者修改成0.0.0.0(或者使用redis服務的ip);如果還不能連線,檢查下是否是因為防火牆原因導致的,自行百度下。
ii、啟動專案後,postman請求出現錯誤,Unable to validate object,修改redis.conf檔案中配置protected-mode,關閉保護模式。 命令:config set protected-mode no