标签:主从复制 http spring redis www springframework org schema
失败记录 ,虽然最终没有成功,但是原理还是知道的
1、spring reids简单实现注解
1、导入相应依赖
<dependency> <groupId>redis.clients</groupId> 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缓存名称)
<?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: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 > <context:property-placeholder location="classpath:redis-config.properties" <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 <cache:annotation-driven cache-manager="cacheManager" <!-- redis相关配置 <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" <property name="maxWaitMillis" value="${redis.maxWait}" <property name="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"> <property name="connectionFactory" ref="JedisConnectionFactory" </bean> <!-- spring自己的缓存管理器,这里定义了缓存位置名称 ,即注解中的value 名称和Ehcache有点像,是吧,哈哈--> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="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>; privateString; publicRedisTemplate<String,Object>getRedisTemplate(){ return; } 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=key.toString(); Object=null; =.execute(newRedisCallback<Object>(){ publicObjectdoInRedis(RedisConnectionconnection) throws{ byte[]=.getBytes(); byte[]=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=key.toString(); finalObject=value; finallong=86400; .execute(newRedisCallback<Long>(){ publicLongdoInRedis(RedisConnectionconnection) throws{ byte[]=.getBytes(); byte[]=toByteArray(valuef); connection.set(keyb,); if(liveTime >0){ connection.expire(keyb,); } return1L; } }); } privatebyte[]toByteArray(Objectobj){ byte[]=null; ByteArrayOutputStream=newByteArrayOutputStream(); try{ ObjectOutputStream=newObjectOutputStream(bos); .writeObject(obj); .flush(); =.toByteArray(); .close(); .close(); }catch(IOException){ .printStackTrace(); } return; } privateObjecttoObject(byte[]bytes){ Object=null; try{ ByteArrayInputStream=newByteArrayInputStream(bytes); ObjectInputStream=newObjectInputStream(bis); =.readObject(); .close(); .close(); }catch(IOException){ .printStackTrace(); }catch(ClassNotFoundException){ .printStackTrace(); } return; } @Override publicvoidevict(Objectkey){ System.out.println("del key"); finalString=key.toString(); .execute(newRedisCallback<Long>(){ publicLongdoInRedis(RedisConnectionconnection) throws{ returnconnection.del(keyf.getBytes()); } }); } @Override publicvoidclear(){ System.out.println("clear key"); .execute(newRedisCallback<String>(){ publicStringdoInRedis(RedisConnectionconnection) throws{ connection.flushDb(); return"ok"; } }); } public<T>Tget(Objectkey,Class<T>type){ returnnull; } publicValueWrapperputIfAbsent(Objectkey,Objectvalue){ returnnull; } } |
5、service 使用注解开发
@Service publicclassPersonService{ @Autowired publicPersonMapper; @Cacheable(value="common",key="'id_'+#id") publicPersonselectByPrimaryKey(longid){ System.out.println("======================"); System.out.println("======================"); System.out.println("======================"); return.selectByPrimaryKey(id); } @CachePut(value="common",key="#person.getName()") publicvoidinsertSelective(Personperson){ .insertSelective(person); System.out.println("########################"); System.out.println("########################"); System.out.println("########################"); } @CacheEvict(value="common",key="'id_'+#id") publicvoiddeleteByPrimaryKey(longid){ .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连接工厂中添加的是一个集群信息,其他的和单片机其实是一样的
<?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: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 > <!-- 引入配置文件 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:properties/redis-cluster.properties" </bean> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 <cache:annotation-driven cache-manager="cacheManager" <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}" <!--最大空闲数--> <property name="maxIdle" value="${redis.maxIdle}" <!--最大建立连接等待时间--> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个--> <property name="testOnBorrow" value="${redis.testOnBorrow}" </bean> <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration"> <property name="maxRedirects" value="${redis.maxRedirects}"></property> <property name="clusterNodes"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.host0}"> </constructor-arg> <constructor-arg name="port" value="${redis.port0}"> </constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.host1}"> </constructor-arg> <constructor-arg name="port" value="${redis.port1}"> </constructor-arg> </bean> > <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.host2}"> </constructor-arg> <constructor-arg name="port" value="${redis.port2}"> </constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.host2}"> </constructor-arg> <constructor-arg name="port" value="${redis.port2}"> </constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.host3}"> </constructor-arg> <constructor-arg name="port" value="${redis.port3}"> </constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${redis.host4}"> </constructor-arg> <constructor-arg name="port" value="${redis.port5}"> </constructor-arg> </bean> </set> </property> </bean> <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/> <constructor-arg name="poolConfig" ref="jedisPoolConfig"/> <property name="password" value="${redis.password}" <property name="timeout" value="${redis.timeout}" </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!-- 如果不配置Serializer,那么存储的时候缺省使用String, 如果用User类型存储,那么会提示错误User can't cast to String!! <property name="connectionFactory" ref="jeidsConnectionFactory" <property name="keySerializer" <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" </property> <property name="valueSerializer" <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <!-- spring自己的缓存管理器,这里定义了缓存位置名称 ,即注解中的value --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <!-- 这里可以配置多个redis --> <bean class="com.redis.redis.RedisCache"> <property name="redisTemplate" ref="redisTemplate" <property name="name" value="defaultCache"/> </bean> </set> </property> </bean> </beans> |
4、代码位置
3、主从复制,哨兵模式
1.、主从复制配置文件 (这里是添加了两个redis ,一个是主,一个是slave,下面的mymaster 具体看主从复制那个教程)
# Redis #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 配置文件(这里没有添加缓存管理器)
<?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: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 > <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${im.hs.server.redis.maxTotal}" <property name="minIdle" value="${im.hs.server.redis.minIdle}" <property name="maxWaitMillis" value="${im.hs.server.redis.maxWaitTime}" <property name="maxIdle" value="${im.hs.server.redis.maxIdle}" <property name="testOnBorrow" value="${im.hs.server.redis.testOnBorrow}" <property name="testOnReturn" value="true" <property name="testWhileIdle" value="true" </bean> <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="${im.hs.server.redis.sentinel.masterName}"/> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${sentinel1.host}"></constructor-arg> <constructor-arg name="port" value="${sentinel1.port}"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="${sentinel2.host}"></constructor-arg> <constructor-arg name="port" value="${sentinel2.port}"></constructor-arg> </bean> </set> </property> </bean> <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"> </constructor-arg> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jeidsConnectionFactory"/> </bean> </beans> |
3、代码位置
标签:主从复制,
http,
spring,
redis,
www,
springframework,
org,
schema
From: https://blog.51cto.com/u_15773996/5984206