Redis服务端的启动和关闭
1.启动Redis服务:redis-server -port 6379 (注:端口可以不指定,默认开启6379端口)
2.关闭Redis服务:考虑到redis正在将内存中的数据拷贝到硬盘,所以强行终止进程可能导致数据丢失,所以争取的方式是通过客户端发送 shutdown命令。
1.查看6379端口是否开放
[root@localhost 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;
}
}
如果有更好的写法欢迎留言讨论(〃'▽'〃)