redis 集成到 spring 中

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:   redis  集成到 spring 中         package com.

 

redis  集成到 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:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" 
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tool="http://www.springframework.org/schema/tool" 
	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/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/jee
		http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/util
		http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/tool
		http://www.springframework.org/schema/tool/spring-tool.xsd">
	
	<!-- other redis connection configuration begin-->
	<bean id="otherRedis" 
	      class="com.trace.db.otherredis.RedisEntry"
	      scope="singleton" >
		<property name="hostName" value="127.0.0.1" />
		<property name="portNumber" value="6379" />
		<property name="password"><null/></property>
	</bean>
	<bean id="redisconnectionconfig" 
	      class="com.trace.db.otherredis.RedisConnectionConfigBean"
	      scope="singleton" >
		<property name="hostConfig">
			<list>
		      <ref bean="otherRedis" />
		    </list>
		</property>
	</bean>
	<bean id="redisConnectionContext" 
	      class="com.trace.db.otherredis.RedisConnectionContext"
	      scope="singleton" >
		<property name="redisConfig" ref="redisconnectionconfig"/>
	</bean>
	<!-- other redis connection configuration fin-->
</beans>

 

 

package com.trace.db.otherredis;

public class RedisEntry {
	private String hostName;
	private int portNumber;
	private String password;
	
	public RedisEntry()
	{
		setHostName("localhost");
		setPortNumber(6379);
		setPassword(null);
	}
	
	
	
	public String getHostName() {
		return hostName;
	}
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}
	public int getPortNumber() {
		return portNumber;
	}
	public void setPortNumber(int portNumber) {
		this.portNumber = portNumber;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 

package com.trace.db.otherredis;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo; 

public class RedisConnectionConfigBean extends JedisPoolConfig {
	
	private Logger log = Logger.getLogger(RedisConnectionConfigBean.class);
	private static final int REDIS_MAX_ACTIVE = 1000;
	private static final int REDIS_MAX_IDLE  = 60;
	private static final int REDIS_MAX_WAIT  = 4000;
	private static final boolean  REDIS_TEST_ON_BORROW = true;
	
	private List<RedisEntry> hostConfig = null;
	
	
	public RedisConnectionConfigBean()
	{
		super();
		log.info("RedisConnectionConfigBean Constructor");
		this.setMaxActive(RedisConnectionConfigBean.REDIS_MAX_ACTIVE);
		this.setMaxIdle(RedisConnectionConfigBean.REDIS_MAX_IDLE);
		this.setMaxWait(RedisConnectionConfigBean.REDIS_MAX_WAIT);
		this.setTestOnBorrow(RedisConnectionConfigBean.REDIS_TEST_ON_BORROW);
	}
	 

	 
	public List<JedisShardInfo> getShardList()
	{
		if(null == hostConfig || hostConfig.isEmpty())
		{
			return null;
		}
		List<JedisShardInfo> reltList = new ArrayList<JedisShardInfo>();
		for(RedisEntry entry : hostConfig)
		{
			JedisShardInfo localInfo = new JedisShardInfo(entry.getHostName(), entry.getPortNumber());
			localInfo.setPassword(entry.getPassword());
			log.info("JedisConnectionConfigBean getShardList entry for "+ entry.getHostName() + ":" + entry.getPortNumber());
			reltList.add(localInfo);
		}//end for
		return reltList;
	}
	
	public void setHostConfig(List<RedisEntry> hostConfig) {
		log.info("JedisConnectionConfigBean setHostConfig");
		this.hostConfig = hostConfig;
	}
	 
	public List<RedisEntry> getHostConfig() {
		return hostConfig;
	}
}

 

 

 

package com.trace.db.otherredis;

import java.util.List;
import java.util.Map; 

import org.apache.log4j.Logger;  

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
 

public class RedisConnectionContext {
	private Logger log = Logger.getLogger(RedisConnectionContext.class);
	
	private RedisConnectionConfigBean redisConfig = null;
	private ShardedJedisPool shardedJedisPool = null;
	/**
	 * 
	 */
	public RedisConnectionContext()
	{ 
		log.debug("JedisConnectionFactory constructor");
	}
	
	
	private ShardedJedisPool getShardedJedisPool()
	{
		if(null == redisConfig)
		{
			log.error("getShardedJedisPool with null config");
			return null;
		}
		if(null == shardedJedisPool)
		{
			log.error("getShardedJedisPool while no pool exist, create a new one");
			shardedJedisPool = new ShardedJedisPool(redisConfig, redisConfig.getShardList());
		}
		return shardedJedisPool;
	}
	
	private ShardedJedis getRedisConnection()
	{
		ShardedJedis localJedis = null;
		try
		{
			localJedis = this.getShardedJedisPool().getResource();
			if(null == localJedis)
			{
				log.error("getRedisConnection: can NOT get instance");
			}
			return localJedis;
		} catch (Exception e)
		{
			log.error("getRedisConnection Exception");
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unused")
	private void closeRedisConnection(ShardedJedis paramJedis)
	{
		if (null != paramJedis)
		{
			try
			{
				this.getShardedJedisPool().returnResource(paramJedis);
			} catch (Exception e)
			{
				log.error("closeRedisConnection Exception");
				e.printStackTrace();
			}
		}
		paramJedis = null;
	}
	
	
	
	// next operations 
	 public  void  setRedisMap(String taskId, Map<String, String> eventMap){
		 log.debug("setRedisMap: taskId: " + taskId);
		 this.getRedisConnection().hmset(taskId,eventMap);
	 }
	 
	 
	 public    List<String>  getRedisMap(String taskId,String... field){
		 log.debug("getRedisMap: taskId: " + taskId + " field:" + field) ;	
		 return   this.getRedisConnection().hmget(taskId,field);
	 }
	 
	
	
	
	
	
	
	
	
	public void setRedisConfig(RedisConnectionConfigBean paramJedisConfig) {
		redisConfig = paramJedisConfig;
	}
	 
	public RedisConnectionConfigBean getRedisConfig() {
		return redisConfig;
	}
	
}

 

 

 log.info("init context: " + ProbServiceConfigs.SCTX_OTHER_REDIS_SUPPORT);
 ApplicationContext otherRedisContext =  new ClassPathXmlApplicationContext(ProbServiceConfigs.SCTX_OTHER_REDIS_SUPPORT);
 paramContextEvent.getServletContext().setAttribute(ProbServiceConfigs.SCTX_OTHER_REDIS_SUPPORT, otherRedisContext);
	

RedisConnectionContext redisCtx = (RedisConnectionContext) otherRedisContext.getBean("redisConnectionContext");	

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
1月前
|
Java Spring 容器
Spring系列文章:Spring6集成MyBatis3.5
Spring系列文章:Spring6集成MyBatis3.5
|
1月前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
376 0
|
14天前
|
安全 Java 测试技术
Spring Boot集成支付宝支付:概念与实战
【4月更文挑战第29天】在电子商务和在线业务应用中,集成有效且安全的支付解决方案是至关重要的。支付宝作为中国领先的支付服务提供商,其支付功能的集成可以显著提升用户体验。本篇博客将详细介绍如何在Spring Boot应用中集成支付宝支付功能,并提供一个实战示例。
36 2
|
1天前
|
NoSQL Java MongoDB
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
【5月更文挑战第11天】本文介绍了如何将非关系型数据库MongoDB与Spring Boot框架集成,以实现高效灵活的数据管理。Spring Boot简化了Spring应用的构建和部署,MongoDB则以其对灵活数据结构的处理能力受到青睐。集成步骤包括:添加MongoDB依赖、配置连接信息、创建数据访问对象(DAO)以及进行数据操作。通过这种方式,开发者可以充分利用两者优势,应对各种数据需求。在实际应用中,结合微服务架构等技术,可以构建高性能、可扩展的系统。掌握MongoDB与Spring Boot集成对于提升开发效率和项目质量至关重要,未来有望在更多领域得到广泛应用。
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
|
4天前
|
安全 Java 数据库连接
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
|
12天前
|
缓存 NoSQL Java
springboot业务开发--springboot集成redis解决缓存雪崩穿透问题
该文介绍了缓存使用中可能出现的三个问题及解决方案:缓存穿透、缓存击穿和缓存雪崩。为防止缓存穿透,可校验请求数据并缓存空值;缓存击穿可采用限流、热点数据预加载或加锁策略;缓存雪崩则需避免同一时间大量缓存失效,可设置随机过期时间。文章还提及了Spring Boot中Redis缓存的配置,包括缓存null值、使用前缀和自定义过期时间,并提供了改造代码以实现缓存到期时间的个性化设置。
|
14天前
|
存储 NoSQL Java
Spring Boot与Redis:整合与实战
【4月更文挑战第29天】Redis,作为一个高性能的键值存储数据库,广泛应用于缓存、消息队列、会话存储等多种场景中。在Spring Boot应用中整合Redis可以显著提高数据处理的效率和应用的响应速度。
28 0
|
19天前
|
XML NoSQL Java
spring整合redis出错
spring整合redis出错
17 0
|
28天前
|
缓存 Java Spring
单体项目中资源管理模块集成Spring Cache
该内容是关于将Spring Cache集成到资源管理模块以实现缓存同步的说明。主要策略包括:查询时添加到缓存,增删改时删除相关缓存。示例代码展示了@Service类中使用@Transactional和@Cacheable注解进行缓存操作,以及在RedisTemplate中处理缓存的示例。
24 5
|
1月前
|
Java 测试技术 Spring
Spring系列文章:Spring集成Log4j2⽇志框架、整合JUnit
Spring系列文章:Spring集成Log4j2⽇志框架、整合JUnit