首页 > 数据库 >spring redis 注解开发 单片机 集群 主从复制

spring redis 注解开发 单片机 集群 主从复制

时间:2023-01-02 22:05:12浏览次数:41  
标签:主从复制 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、代码位置

 

spring redis 注解开发 单片机 集群 主从复制_spring

 

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、代码位置

 

spring redis 注解开发 单片机 集群 主从复制_redis_02

 

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

相关文章

  • 基于Springboot+SSM框架旅游系统项目开发与设计(附源码资料)-毕业设计
    1.项目简介这是一个Springboot旅游网站管理系统,管理员角色包含以下功能:管理员登录,用户管理,旅游路线管理,旅游景点管理,酒店管理,旅游攻略管理,车票管理,订单管理,数据分......
  • 深入 Spring 系列之静态资源处理
    1.背景前一段时间,WebIDE开源的过程中,无意间接触到 ​​webjars​​,觉得比较有趣,于是研究并整理了一下。webjars是将前端的库(比如jQuery)打包成Jar文件,然后使用基于JV......
  • Docker配置redis相关
    三主三从配置---首先要保证dockerqidongsystemctlstartdocker---拉取redis镜像dockerpullredis:6.0.8---运行redis实例dockerrun-d--nameredis-node-1--nethost-......
  • Spring Security 源码分析(四):Spring Social实现微信社交登录
    前言在上一章Spring-Security源码分析三-Spring-Social社交登录过程中,我们已经实现了使用​​SpringSocial​​​+​​Security​​的QQ社交登录。本章我们将实现微信的社......
  • Springboot 整合 Dubbo/ZooKeeper 详解 SOA 案例
    一、为啥整合Dubbo实现SOADubbo不单单只是高性能的RPC调用框架,更是SOA服务治理的一种方案。核心:1.远程通信,向本地调用一样调用远程方法。2.集群容错3.服务自动发......
  • Spring 事务源码(三):事务相关对象的创建
    事务源码(二)中,已经分析了beanDefinition的加载,下面来创建对应beanDefinition的bean。1、PropertySourcesPlaceholderConfigurer创建占位符处理的beanPropertyS......
  • Spring注解综合应用
    需求通过注解方式,实现下面xml的配置(实现“控制层(controller)--业务逻辑层(service)--dao层--数据源”的关系)<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="ht......
  • Spring AOP源码(四):创建被代理类的代理对象
    在AOP源码(三):创建AOP相关的Bean中,介绍了Spring创建AOP的Advisor、AnnotationAwareAspectJAutoProxyCreator的创建,其中被代理类的代理对象是如何创建的未做说明,下面来......
  • spring cache
    SpringCache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。SpringCache提供了一层抽象,底层可以切换不同的cache实现。具体就是通过Cache......
  • Spring注解:使用注解的方式完成IOC
    补充:xml配置最开始(Spring1.x),Spring都是通过xml配置控制层(controller)--业务逻辑层(service)--dao层--数据源的关系,但是比较复杂Spring2.x的时候,随着JDK1.5支持注解的方式,......