spring、spring-data-redis整合使用
一、Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
从2010年3月15日起,Redis的开发工作由VMware主持。从2013年5月开始,Redis的开发由Pivotal赞助。
目前:相信很多人都知道redis的应用和普遍性吧,因为它的灵活性,让我们在使用的时候就更加得心应手了。
这里介绍spring-data-redis的基本用法,这里需要依赖包jedis的支持
二、redis的部署
1、具体的部署配置查看:http://www.cnblogs.com/ll409546297/p/6993778.html
2、这里需要注意几个地方(主要是配置redis.conf)
1)因为我是守护线程启动,后台自启。需要修改daemonize 为yes
daemonize yes

2)因为缓存服务器,一般都是外部服务器,在使用的时候需要设置密码和注释掉bind 127.0.0.1。不然在使用的时候会抛出pool 和 password异常
密码设置:requirepass 123456 (找到相关注解解除,并修改密码)

注释掉bind 127.0.0.1

3、基本测试一下就可以了(可以重启测试一下)

4、这里redis的部署基本完成了。
三、pom.xml(导包)
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
四、配置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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 配置连接池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="10"></property>
<property name="maxIdle" value="10"></property>
<property name="minIdle" value="2"></property>
<property name="maxWaitMillis" value="15000"></property>
<property name="minEvictableIdleTimeMillis" value="300000"></property>
<property name="numTestsPerEvictionRun" value="3"></property>
<property name="timeBetweenEvictionRunsMillis" value="60000"></property>
<property name="testOnBorrow" value="true"></property>
<property name="testOnReturn" value="true"></property>
<property name="testWhileIdle" value="true"></property>
</bean>
<!-- 连接工厂 -->
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.5.100"/>
<property name="port" value="6379"/>
<property name="password" value="123456"/>
<property name="usePool" value="true"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<!-- 用于数据交互 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory"/>
</bean>
</beans>
五、测试
JedisShardInfo jedisShardInfo = new JedisShardInfo("192.168.5.100", 6379);
jedisShardInfo.setPassword("123456");//密码认证
Jedis jedis = new Jedis(jedisShardInfo);
jedis.set("fucking", "fucking");
System.out.println(jedis.get("fucking"));
六、使用
@Autowired
private RedisTemplate<String, String> redisTemplate; redisTemplate.opsForValue().set("test", "test");
String obj = redisTemplate.opsForValue().get("test");
System.out.println(obj);
注意:这里注入不一定只有一个,可以是不同类型
spring、spring-data-redis整合使用的更多相关文章
- Spring Cloud Data Flow整合UAA使用外置数据库和API接口
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<Spring Cloud Data Flow整合Cloudfoundry UAA服务做权限控制 ...
- Spring Cloud Data Flow整合UAA之使用LDAP进行账号管理
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Data Flow整合UAA的文章已经写了两篇,之前的方案是把用户信息保存在数据库中: ...
- spring boot shiro redis整合基于角色和权限的安全管理-Java编程
一.概述 本博客主要讲解spring boot整合Apache的shiro框架,实现基于角色的安全访问控制或者基于权限的访问安全控制,其中还使用到分布式缓存redis进行用户认证信息的缓存,减少数据库 ...
- Spring mvc Data Redis—Pub/Sub(附Web项目源码)
一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...
- Spring Cloud Data Flow整合Cloudfoundry UAA服务做权限控制
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 关于Spring Cloud Data Flow这里不多介绍,有兴趣可以看下面的文章.本文主要介绍如何整合Dat ...
- Redis整合Spring实现分布式锁
spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis是对Redis的封装 <dependencies> <!-- 添加spring- ...
- Spring Data Redis Stream的使用
一.背景 Stream类型是 redis5之后新增的类型,在这篇文章中,我们实现使用Spring boot data redis来消费Redis Stream中的数据.实现独立消费和消费组消费. 二. ...
- Spring+Mybatis基于注解整合Redis
基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...
- spring redis整合
1,利用spring-data-redis整合 项目使用的pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- spring mvc Spring Data Redis RedisTemplate [转]
http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...
随机推荐
- IOS 摇一摇的方法
● 监控摇一摇的方法 ● 方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂) ● 方法2:iOS自带的Shake监控API(非常简单) ● 判断摇一摇的步骤:实现3个摇一摇监听方法 ● ...
- IOS UIDevice距离传感器(打开 关闭)
● 什么是传感器 ● 传感器是一种感应\检测装置, 目前已经广泛应用于智能手机上 ● iPhone5中内置的传感器有 ● 运动传感器\加速度传感器\加速计(Motion/Acceleromet ...
- 2018.11.21 struts2获得servletAPI方式及如何获得参数
访问servletAPI方式 第一种:通过ActionContext (重点及常用 都是获得原生对象) 原理 Action配置 被引入的配置文件 在页面调用取值 第二种:通过ServletAction ...
- ABP问题记录
按照<Asp.NET Core2.0与 EF的ABP框架入门视频教程>(https://ke.qq.com/course/287301)下载了3.9版本的ABP,开始学习,下面记录遇到的问 ...
- stn,spatial transformer network总结
对整篇paper的一个总结:https://blog.csdn.net/xbinworld/article/details/69049680 github:1.https://github.com/D ...
- css的基础用法(上)
css定义: CSS层叠式样表(Cascading Style Sheets)是一种用来表现html或xml等文件样式的计算机语言.CSS不仅可以静态的修饰网页,还可以配合各种脚本语言动态地对网页个 ...
- MySQL数据库主从(主主)配置
一.系统环境: centos7.4 (centos 1708) mysql 5.7 master主机的IP地址为192.168.159.50 slave主机的IP地址为192.168.159.51 M ...
- 你不知道的javaScript笔记(7)
异步:现在与将来 分块的程序 可以把JavaScript 程序写在单独的js 文件中,这个程序是由多个块组成的,这些块 中只有一个是现在执行,其余在捡来执行,最常见的块单位是函数. 例如: funct ...
- 【TOJ 4309】表达式求值(模拟栈)
Description Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20,23)的值是20 ,add(10,98) 的值是108等等.经 ...
- POJ2406 Power Strings(KMP)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56162 Accepted: 23370 Description Giv ...