spring集成redis,集成redis集群
原文:http://chentian114.iteye.com/blog/2292323
1、通过spring-data-redis集成redis
pom.xml依赖包
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.chen</groupId>
- <artifactId>test_redis02</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>war</packaging>
- <build />
- <properties>
- <spring.version>3.1.2.RELEASE</spring.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <!-- spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <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.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- <version>2.4.2</version>
- </dependency>
- </dependencies>
- </project>
spring application-reids.xml配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- ">
- <!-- 加载配置文件 -->
- <context:property-placeholder location="classpath:redis.properties"/>
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxIdle" value="${redis.maxIdle}" />
- <!-- <property name="maxActive" value="${redis.maxActive}" />
- <property name="maxWait" value="${redis.maxWait}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
- </bean>
- <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
- <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
- <property name="connectionFactory" ref="connectionFactory" />
- </bean>
- </beans>
UserDaoImpl.java
- package com.chen.dao;
- import java.io.Serializable;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.dao.DataAccessException;
- import org.springframework.data.redis.connection.RedisConnection;
- import org.springframework.data.redis.core.RedisCallback;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Repository;
- import com.chen.pojo.User;
- @Repository
- public class UserDaoImpl implements UserDao {
- @Autowired
- private RedisTemplate<Serializable,Serializable> redisTemplate;
- public void saveUser(final User user) {
- redisTemplate.execute(new RedisCallback<Object>(){
- @Override
- public Object doInRedis(RedisConnection connection)
- throws DataAccessException {
- String str= "user.uid."+user.getId();
- byte[] key = redisTemplate.getStringSerializer().serialize(str);
- connection.set(key,redisTemplate.getStringSerializer().serialize(user.getName()));
- return null;
- }
- });
- }
- public User getUser(final long id) {
- return redisTemplate.execute(new RedisCallback<User>(){
- @Override
- public User doInRedis(RedisConnection connection) throws DataAccessException {
- byte[] key = redisTemplate.getStringSerializer().serialize("user.uid." + id);
- if(connection.exists(key)) {
- byte[] value = connection.get(key);
- String name = redisTemplate.getStringSerializer().deserialize(value);
- User user = new User();
- user.setName(name);
- user.setId(id);
- return user;
- }
- return null;
- }
- });
- }
- }
2、通过jedis集成redis
spring applicationContext.xml配置文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
- ">
- <!-- 加载配置文件 -->
- <context:property-placeholder location="classpath:redis.properties"/>
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxIdle" value="${redis.maxIdle}" />
- <!-- <property name="maxActive" value="${redis.maxActive}" />
- <property name="maxWait" value="${redis.maxWait}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" /> -->
- </bean>
- <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
- <constructor-arg index="0" ref="jedisPoolConfig" />
- <constructor-arg index="1">
- <list>
- <bean class="redis.clients.jedis.JedisShardInfo">
- <constructor-arg name="host" value="${redis.host}" />
- <constructor-arg name="port" value="${redis.port}" />
- <!-- <constructor-arg name="timeout" value="${redis.timeout}" /> -->
- </bean>
- </list>
- </constructor-arg>
- </bean>
- </beans>
RedisDataSourceImpl.java
- package com.chen.redis;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import redis.clients.jedis.ShardedJedis;
- import redis.clients.jedis.ShardedJedisPool;
- @Repository("redisDataSource")
- public class RedisDataSourceImpl implements RedisDataSource {
- @Autowired
- private ShardedJedisPool shardedJedisPool;
- public ShardedJedis getRedisClient() {
- try{
- ShardedJedis shardJedis = shardedJedisPool.getResource();
- return shardJedis;
- }catch(Exception e){
- e.printStackTrace();
- }
- return null;
- }
- public void returnResource(ShardedJedis shardedJedis) {
- shardedJedisPool.returnResource(shardedJedis);
- }
- public void returnResource(ShardedJedis shardedJedis, boolean broken) {
- if(broken){
- shardedJedisPool.returnBrokenResource(shardedJedis);
- }else{
- shardedJedisPool.returnResource(shardedJedis);
- }
- }
- }
RedisClientTemplate.java
- package com.chen.redis;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import redis.clients.jedis.ShardedJedis;
- @Repository("redisClientTemplate")
- public class RedisClientTemplate {
- @Autowired
- private RedisDataSource redisDataSource;
- public void disconnect(){
- ShardedJedis shardedJedis = redisDataSource.getRedisClient();
- shardedJedis.disconnect();
- }
- public String set(String key,String val){
- String result = null ;
- ShardedJedis jedis = redisDataSource.getRedisClient();
- if(jedis ==null)return null;
- boolean broken = false;
- try{
- result = jedis.set(key, val);
- }catch(Exception e){
- e.printStackTrace();
- broken = true ;
- }finally{
- redisDataSource.returnResource(jedis,broken);
- }
- return result;
- }
- public String get(String key){
- String result = null;
- ShardedJedis jedis = redisDataSource.getRedisClient();
- if(jedis==null)return result;
- boolean broken = false;
- try{
- result = jedis.get(key);
- }catch(Exception e){
- e.printStackTrace();
- broken= true;
- }finally{
- redisDataSource.returnResource(jedis,broken);
- }
- return result;
- }
- }
DemoTest.java
spring集成redis,集成redis集群的更多相关文章
- Redis+Twemproxy+HAProxy集群(转) 干货
原文地址:Redis+Twemproxy+HAProxy集群 干货 Redis主从模式 Redis数据库与传统数据库属于并行关系,也就是说传统的关系型数据库保存的是结构化数据,而Redis保存的是一 ...
- redis:哨兵集群配置
最少配置1主2从3哨兵 一.引言 上一篇文章我们详细的讲解了Redis的主从集群模式,其实这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改,但是有一 ...
- Redis 高可用集群
Redis 高可用集群 Redis 的集群主从模型是一种高可用的集群架构.本章主要内容有:高可用集群的搭建,Jedis连接集群,新增集群节点,删除集群节点,其他配置补充说明. 高可用集群搭建 集群(c ...
- Redis存储Tomcat集群的Session
Redis存储Tomcat集群的Session 如何 做到把新开发的代码推送到到生产系统中部署,生产系统要能够零宕机.对使用用户零影响. 设想 是使用集群来搞定,通过通知负载均衡Nginx,取下集群中 ...
- Redis 3.0 集群搭建
Redis 3.0 集群搭建 开启两个虚拟机 分别在两个虚拟机上开启3个Redis实例 3主3从两个虚拟机里的实例互为主备 下面分别在两个虚拟机上安装,网络设置参照codis集群的前两个主机 分别关闭 ...
- Redis 3.0集群 Window搭建方案
Redis 3.0集群 Window搭建方案 1.集群安装前准备 安装Ruby环境,安装:rubyinstaller-2.3.0-x64.exe http://dl.bintray.com/onecl ...
- Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享
Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...
- Redis进阶实践之十 Redis主从复制的集群模式
一.引言 Redis的基本数据类型,高级特性,与Lua脚本的整合等相关知识点都学完了,说是学完了,只是完成了当前的学习计划,在以后的时间还需继续深入研究和学习.从今天开始来讲一下有关Re ...
- Redis进阶实践之十一 Redis的Cluster集群搭建
一.引言 本文档只对Redis的Cluster集群做简单的介绍,并没有对分布式系统的详细概念做深入的探讨.本文只是提供了有关如何设置集群.测试和操作集群的说明,而不涉及Redis集群规范中涵 ...
- Redis进阶实践之十二 Redis的Cluster集群动态扩容
一.引言 上一篇文章我们一步一步的教大家搭建了Redis的Cluster集群环境,形成了3个主节点和3个从节点的Cluster的环境.当然,大家可以使用 Cluster info 命令查看Cl ...
随机推荐
- 全排列---(dfs)
全排列输入一个数n,按字典序输出1-n的全排列 #include "cstdio" #include "cstring" ],ans[],n; void dfs ...
- 【51NOD】1486 大大走格子
[算法]动态规划+组合数学 [题意]有一个h行w列的棋盘,定义一些格子为不能走的黑点,现在要求从左上角走到右下角的方案数. [题解] 大概能考虑到离散化黑点后,中间的空格子直接用组合数计算. 然后解决 ...
- python3 uper(),继承实现原理,封装
抽象类:本身不能被实例化,也不应该不实例化,它的作用就定义标准,并不用具体实现 import abc class Parent(metaclass=abc.ABCMeta): x=1 @abc.abs ...
- Kuangbin 带你飞 KMP扩展KMP Manacher
首先是几份模版 KMP void kmp_pre(char x[],int m,int fail[]) { int i,j; j = fail[] = -; i = ; while (i < m ...
- 利用cron监视后台进程状态
利用cron监视后台进程状态 http://blog.csdn.net/dyx810601/article/details/72967758 1. 利用cron监视后台进程状态,如果进程死掉或服务器重 ...
- linux服务与进程
linux服务与进程 http://www.cnblogs.com/jamesbd/p/3567654.html linux服务与进程 1.应用程序 2.服务脚本 3.配置文件 4.查看进程 5.查看 ...
- 一步步疑难解析 —— Python 异步编程构建博客
声明:该项目学习资源主要来自廖雪峰的Python教程,参见 http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6 ...
- 胖AP与瘦AP区别
一.胖AP组网方案 1.漫游问题 用户从一个胖AP的覆盖区域走到另一个胖AP的覆盖区域,会重新连接信号强的一个胖AP,重新进行认证,重新获取IP地址,存在断网现象: 2.无法保证WLAN的安全性 为了 ...
- Spring的自动装配
在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法: 自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...
- 巧用nth_element求容器前n%大小的那个数
#include <algorithm> #include <vector> #include <iostream> #include <string> ...