SSM整合Redis
阿新 • • 發佈:2018-12-17
1.檢視6379埠是否開放
[[email protected] src]# firewall-cmd --permanent --zone=public --list-ports
下圖為查詢後的結果
如果沒有開放6379埠則根據下面步驟操作
1.1永久的新增該埠。去掉--permanent則表示臨時。
firewall-cmd --permanent --zone=public --add-port=需要開放的埠號/tcp
1.2載入配置,使得修改有效。
firewall-cmd --reload
2.設定客戶端的protected-mode 為no (禁用保護模式)
config set protected-mode "no"
3.進行最基礎的測試
這裡只需要匯入一個最基本的Jedis包即可
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
import org.junit.Test; import redis.clients.jedis.Jedis; /** * author:wlm * name: * function: * 2018/10/26 18:38 */ public class javaToRedisTest { @Test public void redisConnectionTest(){ Jedis jedis= new Jedis("172.20.10.2",6379); jedis.set("test","測試"); System.out.println(jedis.get("test")); } }
到這裡如果測試沒問題接下來就開始整合Redis到SSM中去
一.所需要的jar包(注意版本)
<!--redis--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- config redis data and client jar--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!--commons-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>
二.redis.properties檔案
redis.host=172.30.20.3
redis.port=6379
redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true
三.redis-context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--掃描redis配置檔案-->
<context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties"/>
<!--設定連線池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="testOnReturn" value="${redis.testOnReturn}" />
</bean>
<!--設定連結屬性-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}"
p:port="${redis.port}"
p:password=""
p:pool-config-ref="poolConfig"
p:timeout="100000"/>
<!-- Jedis模板配置 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
</beans>
四.把redis-context.xml檔案引入到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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 引入redis配置檔案 -->
<import resource="classpath:redis-context.xml"/>
</beans>
五.測試
/**
* author:wlm
* name:
* function:
* 2018/10/22 10:12
*/
@Controller
public class StudentController {
@Autowired
private RedisTemplate redisTemplate; //注入
@RequestMapping("add")
public String addStudent(){
try{
redisTemplate.opsForValue().set("test", "測試");
Log4j2Controller.info("value:"+redisTemplate.opsForValue().get("chen"));
}catch(Exception e){
Log4j2Controller.error("Redis-error");
}
return null;
}
}
如果有更好的寫法歡迎留言討論(〃'▽'〃)