前言:此篇博客主要针对于刚学redis的同学,或者刚学完redis 的同学还不能独立搭建与项目整合参考使用
(注:此篇只讲解redis的缓存部分,关于redis的主从复制、哨兵模式等等将在以后的博客中进行讲解)
本篇博客主要讲解
(虚拟机环境搭建可参考:)
1:redis的环境搭建
2:redis整合项目
3:一些软件的使用简介(Xshell)
4:redis整合部分bugger分析
正文:
软件:
linux-Redis:Redis 是一个高性能的key-value数据库,相对与MySQL优点在于数据存储在内存而不是硬盘,读取快
下载地址:https://download.youkuaiyun.com/download/tolode/10900445
Xshell:在windows系统中通过命令操作linux系统的工具,也可用于其他系统。页面清晰,在进行修改相关配置时,已注释和 未注释色彩分明,是个非常不错的工具
下载地址:https://download.youkuaiyun.com/download/tolode/10897759
一:在windows使用Xshell进行操作linux系统
1.1:在linux系统的控制台查询当前端口号 ifconfig
1.2 :根据查询到的端口号进行xshell连接 ssh ip
连接不上原因分析
1:端口错误、再次查看确定,
2:端口不同、两个系统进行互ping
1.3:上传redis文件到linux系统
快捷键:Ctrl+Alt+F
关闭弹出窗口,出现sftp:/root> 开始上传文件
可直接进行拖拽文件
1.4:对redis进行解压,编译安装
解压: tar xvf redis-3.2.11.tar.gz
编译安装: make && make install
1.5:开启redis: redis-cli
1.6:查询redis是否开启(端口号:默认6379):ps -ef|grep redis
1.7:redis的一些操作命令
查询密码: config get requirepass
设置密码:config set requirepass root
密码验证:auth root
删除一个key:del key
删除当前数据库中的所有key:flushdb
删除所有数据库中的key: flushall
二:Java项目整合Redis

2.1:pom或者jar(jedis、spring-data-redis)
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
<dependency>
2.2: redis.properties
redis.hostname=192.168.1.104
redis.port=6379
redis.password=root
redis.timeout=2000
redis.usePool=true
redis.default.db=0
redis.maxTotal=600
redis.maxIdle=300
redis.timeBetweenEvictionRunsMillis=30000
redis.minEvictableIdleTimeMillis=30000
redis.testOnBorrow=false
redis.encode=utf-8
redis.expire=604800000
redis.unlock=false
2.3:application-reids.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,
另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven />
<mvc:annotation-driven />
<context:component-scan base-package="com.redis.*" />
<context:property-placeholder location="classpath:config/redis.properties"/>
<!-- jedis 配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.hostname}" />
<property name="password" value="${redis.password}" />
<property name="timeout" value="${redis.timeout}"></property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="default"></property>
</bean>
<bean class="com.redis.util.RedisCache">
<property name="redisTemplate" ref="redisTemplate" />
<property name="name" value="data"/>
<!-- common名称要在类或方法的注解中使用 -->
</bean>
</set>
</property>
</bean>
</beans>
三:进行单元测试
3.1需要pom或jar
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
3.2:单元测试java代码
@Test
public void test1Jedis(){
//host链接云主机的外网地址
Jedis jedis=new Jedis("192.168.1.103", 6379);
jedis.auth("root");//加入这一段代码
String name=jedis.get("xiaomin");
jedis.set("age", "123");
System.out.println(age);
jedis.close();
}
四:项目整合
原本项目不变:主要在业务层进行缓存,我这里是使用注解的方式。
原始项目框架:Spring+SpringMVC+JDBCTemplate
package cn.redis.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class ServiceInpl implements cn.redis.test.Service{
@Autowired
private Dao dao;
@Override
@Cacheable(value = "data" ,key="'name'") //这里的值data与配置文件中的data名称一致,key是自定义的键
public User showUser(int i) {
return dao.showUser(i);
}
}
项目下载地址:https://download.youkuaiyun.com/download/tolode/1089126
五:Bugger分析
详情链接:https://blog.youkuaiyun.com/tolode/article/details/85857541
结束语:要是大家碰到什么问题,可以在下方进行留言,我们可以一起探讨
1164



被折叠的 条评论
为什么被折叠?



