前言
好久没来博客了,最近也不太关注安全方面的事情,毕竟在筹备毕业的事情,肯定很多精力就投放在毕设上了。整好今日闲来无事,便把之前毕设中整合Redis的架构方式写下来吧,也方便以后看。
正文
需要整合Redis,需先在Maven中导入相关的依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
还需配置相关redis属性
redis.properties
##########################
## redis缓存配置
##########################
# redis主机IP
redis.host=192.168.100.101
# redis端口
redis.port=6379
# 链接超时
# redis.timeout=2000
# 密码
redis.password=root
# 指定redis数据库
redis.dbIndex=0
##########################
## redis连接池配置
##########################
# 最大连接数
redis.maxTotal=30
# 最大空闲连接数
redis.maxIdle=10
# 获取链接最大等待毫秒
redis.maxWaitMillis=1000
# 获取链接时检查有效性
redis.testOnBorrow=true
# 归还链接时检查是否有效
redis.testOnReturn=true
# 在空闲时监测有效性
# redis.testWhileIdle=true
# 链接耗尽时是否阻塞
# redis.blockWhenExhausted=true
# 每次释放链接的最大数目
# redis.numTestsPerEvictionRun=1024
# 释放链接的扫描间隔(毫秒)
# redis.timeBetweenEvictionRunsMillis=30000
# 最小空闲时间
# redis.minEvictableIdleTimeMillis=1800000
# 链接空闲多久后释放
# redis.softMinEvictableIdleTimeMillis=10000
spring-redis.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: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
">
<!--导入配置文件-->
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:redis.properties" />
</bean>
<!--设置jedisPool链接池的配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.testOnReturn}"/>
</bean>
<!--redis链接密码-->
<!-- <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">-->
<!-- <constructor-arg name="thePassword" value="${redis.password}"/>-->
<!-- </bean>-->
<!--spring-data-redis2.0以上的配置-->
<!-- <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">-->
<!-- <property name="hostName" value="${redis.host}"/>-->
<!-- <property name="port" value="${redis.port}"/>-->
<!-- <property name="password" ref="redisPassword" />-->
<!-- <property name="database" value="${redis.dbIndex}"/>-->
<!-- </bean>-->
<!--配置jedis链接工厂 spring-data-redis2.0中
建议改为构造器传入一个RedisStandaloneConfiguration 单机
RedisSentinelConfiguration 主从复制
RedisClusterConfiguration 集群-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!--注销掉的部分为spring-data-redis2.0以下的版本配置的方式-->
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
<property name="password" value="${redis.password}" />
<property name="database" value="${redis.dbIndex}"/>
<!--spring-data-redis2.0以上建议获取的方式-->
<!-- <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"/>-->
</bean>
<!--手动设置 key 与 value的序列化方式-->
<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<!--配置jedis模板 -->
<bean id = "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer" ref="keySerializer" />
<property name="valueSerializer" ref="valueSerializer" />
<property name="hashKeySerializer" ref="keySerializer" />
<property name="hashValueSerializer" ref="valueSerializer" />
</bean>
<!--也可以StringRedisTemplate 专注于String的操作 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<!--<property name="connectionFactory" ref="jedisConnectionFactory"></property>-->
<!--在StringRedisTemplate与redisTemplate不同,可以直接造构造器中传入ConnectionFactory-->
<constructor-arg name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="keySerializer" />
<property name="valueSerializer" ref="valueSerializer" />
<property name="hashKeySerializer" ref="keySerializer" />
<property name="hashValueSerializer" ref="valueSerializer" />
</bean>
</beans>
有了基本的redis配置信息后,就需要让服务器知道该配置文件,整合到服务器中运行
修改web.xml文件
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定配置文件位置和名称 如果不设置,默认找/WEB-INF/<servlet-name>-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在配置文件spring-redis.xml中引用了RedisTemplate的模板来操作Redis,所以就需要编写个操作类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class RedisController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void setPushString(String key,String value){
redisTemplate.opsForList().rightPush(key, value);
}
}
自动注入后会发现,redisTemplate的值是空的,会抛出NullPointerException异常
经过一番折腾,才知道自动注入是无法通过new等关键词进行创建调用。网上搜索的解决方案我都一一试了,皆无法解决该问题。
后面咨询大神得知自动注入的属性需要静态初始化变量,才能通过New关键词创建并调用。
解决方案如下,创建一个RedisPlugin类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class RedisPlugin {
@Autowired
private RedisController redisController;
// 静态初使化当前类
public static RedisController redisPlugin;
//注解@PostConstruct,这样方法就会在Bean初始化之后被Spring容器执行
@PostConstruct
public void init() {
redisPlugin = redisController;
}
public void setPushString(String key, String value){
redisPlugin.setPushString(key,value);
}
}
如此一来,我们只需要
RedisPlugin redis = new RedisPlugin();
redis.setPushString("onekey","hello");
便可以成功set一个键值
总结
整体逻辑跟Mybatis搭建差不多,但Mybatis是通过Mapping的xml文件进行关系映射。也算一种不错的解决方式,适用与非API接口的清空。如果道友们有更好的自动注解方式,也可以在下方留言学习一下!