spring redis 注解开发 单片机 集群 主从复制
来源:互联网 发布:多益网络线上二笔 编辑:程序博客网 时间:2024/06/01 09:01
失败记录 ,虽然最终没有成功,但是原理还是知道的
1、spring reids简单实现注解
http://blog.csdn.net/fighterandknight/article/details/53432276/
1、导入相应依赖
<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>
2、redis配置信息properties
redis.host=127.0.0.1
redis.port=6379
redis.password=
redis.maxIdle=100
redis.maxActive=300
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=10000
3、spring redis 配置文件
1、poolConfig ,导入reids信息,注意新旧版本参数名字不一样哦
2、JedisConnectionFactory,redis连接工厂,将poolConfig导入,并配置host,port,ip password等
3、redis处理类 JedisTemplate,将连接工厂导入进来,(spring boot中用来处理key的序列化)
4、缓存管理器,配置cache(这个时候,需要自己写一个RedisCach类,将JedisTemplate 导入,并设置缓存位置名称name,(这里就是相当于Ehcache 管理管理工厂)这个时候还可以配置多个redis缓存名称)
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">
<context:property-placeholderlocation="classpath:redis-config.properties" />
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-drivencache-manager="cacheManager" />
<!-- redis相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxIdle" value="${redis.maxIdle}" />
<propertyname="maxWaitMillis" value="${redis.maxWait}" />
<propertyname="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="JedisConnectionFactory" 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.RedisTemplate">
<propertyname="connectionFactory" ref="JedisConnectionFactory" />
</bean>
<!-- spring自己的缓存管理器,这里定义了缓存位置名称,即注解中的value 名称和Ehcache有点像,是吧,哈哈-->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<propertyname="caches">
<set>
<!-- 这里可以配置多个redis -->
<bean class="com.cn.util.RedisCache">
<property name="redisTemplate" ref="redisTemplate" />
<property name="name" value="default"/>
</bean>
<bean class="com.cn.util.RedisCache">
<property name="redisTemplate" ref="redisTemplate" />
<property name="name" value="common"/>
<!-- common名称要在类或方法的注解中使用 -->
</bean>
</set>
</property>
</bean>
</beans>
4、RedisCache为使用注解开发,缓存类
publicclassRedisCacheimplementsCache{
privateRedisTemplate<String,Object> redisTemplate;
privateString name;
publicRedisTemplate<String,Object>getRedisTemplate(){
return redisTemplate;
}
publicvoidsetRedisTemplate(RedisTemplate<String,Object>redisTemplate){
this.redisTemplate =redisTemplate;
}
publicvoidsetName(Stringname){
this.name =name;
}
@Override
publicStringgetName(){
returnthis.name;
}
@Override
publicObjectgetNativeCache(){
returnthis.redisTemplate;
}
@Override
publicValueWrapperget(Objectkey){
System.out.println("get key");
finalString keyf =key.toString();
Object object =null;
object = redisTemplate.execute(newRedisCallback<Object>(){
publicObjectdoInRedis(RedisConnectionconnection)
throws DataAccessException {
byte[] key = keyf.getBytes();
byte[] value =connection.get(key);
if(value ==null){
returnnull;
}
returntoObject(value);
}
});
return(object !=null?newSimpleValueWrapper(object):null);
}
@Override
publicvoidput(Objectkey,Objectvalue){
System.out.println("put key");
finalString keyf =key.toString();
finalObject valuef =value;
finallong liveTime =86400;
redisTemplate.execute(newRedisCallback<Long>(){
publicLongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
byte[] keyb = keyf.getBytes();
byte[] valueb =toByteArray(valuef);
connection.set(keyb, valueb);
if(liveTime >0){
connection.expire(keyb, liveTime);
}
return1L;
}
});
}
privatebyte[]toByteArray(Objectobj){
byte[] bytes =null;
ByteArrayOutputStream bos =newByteArrayOutputStream();
try{
ObjectOutputStream oos =newObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bytes = bos.toByteArray();
oos.close();
bos.close();
}catch(IOException ex){
ex.printStackTrace();
}
return bytes;
}
privateObjecttoObject(byte[]bytes){
Object obj =null;
try{
ByteArrayInputStream bis =newByteArrayInputStream(bytes);
ObjectInputStream ois =newObjectInputStream(bis);
obj = ois.readObject();
ois.close();
bis.close();
}catch(IOException ex){
ex.printStackTrace();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}
return obj;
}
@Override
publicvoidevict(Objectkey){
System.out.println("del key");
finalString keyf =key.toString();
redisTemplate.execute(newRedisCallback<Long>(){
publicLongdoInRedis(RedisConnectionconnection)
throws DataAccessException {
returnconnection.del(keyf.getBytes());
}
});
}
@Override
publicvoidclear(){
System.out.println("clear key");
redisTemplate.execute(newRedisCallback<String>(){
publicStringdoInRedis(RedisConnectionconnection)
throws DataAccessException {
connection.flushDb();
return"ok";
}
});
}
public<T>Tget(Objectkey,Class<T>type){
returnnull;
}
publicValueWrapperputIfAbsent(Objectkey,Objectvalue){
returnnull;
}
}
5、service 使用注解开发
@Service
publicclassPersonService{
@Autowired
publicPersonMapper personMapper;
@Cacheable(value="common",key="'id_'+#id")
publicPersonselectByPrimaryKey(longid){
System.out.println("======================");
System.out.println("======================");
System.out.println("======================");
return personMapper.selectByPrimaryKey(id);
}
@CachePut(value="common",key="#person.getName()")
publicvoidinsertSelective(Personperson){
personMapper.insertSelective(person);
System.out.println("########################");
System.out.println("########################");
System.out.println("########################");
}
@CacheEvict(value="common",key="'id_'+#id")
publicvoiddeleteByPrimaryKey(longid){
personMapper.deleteByPrimaryKey(id);
System.out.println("******************************");
System.out.println("******************************");
System.out.println("******************************");
}
}
6、代码位置
2、spring 集群 注解
1、导入依赖包
2、集群 信息redis-cluster.properties
redis.host0=192.168.1.235
redis.port0=7000
redis.host1=192.168.1.235
redis.port1=7001
redis.host2=192.168.1.235
redis.port2=7002
redis.host3=192.168.1.235
redis.port3=7003
redis.host4=192.168.1.235
redis.port4=7004
redis.host5=192.168.1.235
redis.port5=7005
redis.maxRedirects=3
redis.maxIdle=30
redis.maxTotal=100
redis.minIdle=5
redis.maxWaitMillis=30000
redis.testOnBorrow=true
redis.testOnReturn=true
redis.testWhileIdle=true
redis.timeout=3000
3、编辑spring redis 配置文件
1、 相当于jedis连接工厂中添加的是一个集群信息,其他的和单片机其实是一样的
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">
<!-- 引入配置文件 -->
<beanid="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location" value="classpath:properties/redis-cluster.properties" />
</bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-drivencache-manager="cacheManager" />
<beanid="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxTotal" value="${redis.maxTotal}" />
<!--最大空闲数-->
<propertyname="maxIdle" value="${redis.maxIdle}" />
<!--最大建立连接等待时间-->
<propertyname="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
<propertyname="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<beanid="redisClusterConfiguration"class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<propertyname="maxRedirects" value="${redis.maxRedirects}"></property>
<propertyname="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"
value="${redis.host0}">
</constructor-arg>
<constructor-argname="port"
value="${redis.port0}">
</constructor-arg>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"
value="${redis.host1}">
</constructor-arg>
<constructor-argname="port"
value="${redis.port1}">
</constructor-arg>
</bean>
>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"
value="${redis.host2}">
</constructor-arg>
<constructor-argname="port"
value="${redis.port2}">
</constructor-arg>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"
value="${redis.host2}">
</constructor-arg>
<constructor-argname="port"
value="${redis.port2}">
</constructor-arg>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"
value="${redis.host3}">
</constructor-arg>
<constructor-argname="port"
value="${redis.port3}">
</constructor-arg>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host"
value="${redis.host4}">
</constructor-arg>
<constructor-argname="port"
value="${redis.port5}">
</constructor-arg>
</bean>
</set>
</property>
</bean>
<beanid="jeidsConnectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<constructor-argname="clusterConfig" ref="redisClusterConfiguration"/>
<constructor-argname="poolConfig" ref="jedisPoolConfig"/>
<propertyname="password" value="${redis.password}" />
<propertyname="timeout" value="${redis.timeout}" />
</bean>
<beanid="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!-- 如果不配置Serializer,那么存储的时候缺省使用String,
如果用User类型存储,那么会提示错误User can't cast to String!! -->
<propertyname="connectionFactory" ref="jeidsConnectionFactory" />
<propertyname="keySerializer" >
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<propertyname="valueSerializer" >
<beanclass="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<propertyname="hashKeySerializer">
<beanclass="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<propertyname="hashValueSerializer">
<beanclass="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<!-- spring自己的缓存管理器,这里定义了缓存位置名称,即注解中的value -->
<beanid="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<propertyname="caches">
<set>
<!-- 这里可以配置多个redis -->
<beanclass="com.redis.redis.RedisCache">
<propertyname="redisTemplate" ref="redisTemplate" />
<propertyname="name" value="defaultCache"/>
</bean>
</set>
</property>
</bean>
</beans>
4、代码位置
3、主从复制,哨兵模式
1.、主从复制配置文件 (这里是添加了两个redis ,一个是主,一个是slave,下面的mymaster 具体看主从复制那个教程)
# Redis settings
#sentinel1的IP和端口
sentinel1.host=192.168.1.233
sentinel1.port=26379
#sentinel2的IP和端口
sentinel2.host=192.168.1.233
sentinel2.port=26378
im.hs.server.redis.maxIdle=500
#最大连接数,超过此连接时操作redis会报错
im.hs.server.redis.maxTotal=5000
im.hs.server.redis.maxWaitTime=1000
im.hs.server.redis.testOnBorrow=true
#最小闲置连接数,spring启动的时候自动建立该数目的连接供应用程序使用,不够的时候会申请。
im.hs.server.redis.minIdle=300
im.hs.server.redis.sentinel.masterName=mymaster
2、spring redis 配置文件(这里没有添加缓存管理器)
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<beanid="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxTotal" value="${im.hs.server.redis.maxTotal}" />
<propertyname="minIdle" value="${im.hs.server.redis.minIdle}" />
<propertyname="maxWaitMillis" value="${im.hs.server.redis.maxWaitTime}" />
<propertyname="maxIdle" value="${im.hs.server.redis.maxIdle}" />
<propertyname="testOnBorrow" value="${im.hs.server.redis.testOnBorrow}" />
<propertyname="testOnReturn" value="true" />
<propertyname="testWhileIdle" value="true" />
</bean>
<beanid="redisSentinelConfiguration"class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<propertyname="master">
<beanclass="org.springframework.data.redis.connection.RedisNode">
<propertyname="name" value="${im.hs.server.redis.sentinel.masterName}"/>
</bean>
</property>
<propertyname="sentinels">
<set>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host" value="${sentinel1.host}"></constructor-arg>
<constructor-argname="port" value="${sentinel1.port}"></constructor-arg>
</bean>
<beanclass="org.springframework.data.redis.connection.RedisNode">
<constructor-argname="host" value="${sentinel2.host}"></constructor-arg>
<constructor-argname="port" value="${sentinel2.port}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<beanid="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-argname="sentinelConfig"
ref="redisSentinelConfiguration">
</constructor-arg>
<constructor-argname="poolConfig" ref="poolConfig"></constructor-arg>
</bean>
<beanid="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<propertyname="connectionFactory" ref="jeidsConnectionFactory"/>
</bean>
</beans>
3、代码位置
- spring redis 注解开发 单片机 集群 主从复制
- Redis集群主从复制
- redis主从复制+sentinel集群
- spring整合redis(集群、主从)
- 《redis集群sentinel主从复制》笔记
- Redis主从复制和集群配置
- redis集群与主从复制的关系
- redis主从复制和集群实现原理
- Redis主从复制和集群配置
- Redis 主从复制(服务集群)
- Redis主从复制和集群配置
- redis主从复制和集群实现原理
- Redis主从复制和集群配置
- Redis主从复制和集群配置
- Redis主从复制和集群配置
- Redis主从复制和集群配置
- Redis主从复制和集群配置
- Redis主从复制和集群配置
- GLSL 基础
- S16模板与泛型编程
- CSRF攻击详解
- 企业级搜索elasticsearch应用03-前置处理器
- Spring源码分析-深入浅出AOP(图文分析)
- spring redis 注解开发 单片机 集群 主从复制
- 分布式缓存Redis之Pipeline(管道)
- 设计模式(一)总纲
- 润乾集算器与帆软报表集成(直接通过jdbc,类DB存储过程方式调用)
- 增删改插
- Bootstrap3模板
- Java数组
- mysql乐观锁总结和实践
- 基于密度的聚类方法-OPTICS