/jt-manage/src/test/java/com/jt/redis/TestSentinel.java
public class TestSentinel {
@Test
public void testSentinel(){
//IP:端口
Set<String> sentinels = new HashSet<>();
sentinels.add("192.168.126.166:26379");
JedisSentinelPool pool =
new JedisSentinelPool("mymaster", sentinels);
//new JedisSentinelPool(masterName, sentinels, poolConfig)
Jedis jedis = pool.getResource();
jedis.set("shaobing","哨兵测试");
System.out.println("获取数据:"+jedis.get("shaobing"));
pool.returnResource(jedis);
}
}
/jt-manage/src/main/resources/property/redis.properties
redis.maxTotal=1000
redis.sentinel=192.168.126.166:26379
redis.mastername=mymaster
/jt-manage/src/main/resources/spring/applicationContext-redis.xml
<bean id="sentinelPool" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg name="masterName" value="${redis.mastername}"/>
<constructor-arg name="sentinels">
<set>
<value>${redis.sentinel}value>
set>
constructor-arg>
<constructor-arg name="poolConfig" ref="poolConfig"/>
bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
bean>
/jt-common/src/main/java/com/jt/common/service/RedisService.java
@Service
public class RedisService {
//引入哨兵的配置
@Autowired(required=false)
private JedisSentinelPool sentinelPool;
public void set(String key,String value){
Jedis jedis = sentinelPool.getResource();
jedis.set(key, value);
sentinelPool.returnResource(jedis);
}
public void set(String key,String value,int seconds){
Jedis jedis = sentinelPool.getResource();
jedis.setex(key, seconds, value);
sentinelPool.returnResource(jedis);
}
public String get(String key){
Jedis jedis = sentinelPool.getResource();
String result = jedis.get(key);
sentinelPool.returnResource(jedis);
return result;
}
}
1.分片作用
2.哨兵作用
说明:采用redis集群配置,在集群内部实现通信和选举,同时实现分片的扩容和高可用。
说明:因为选举机制投票平票而导致出现多台主机的现象称之为脑裂。
解决方案:让节点个数增加(保证超过半数即可)。
规模:3主6从
端口:7000-7008共9台
1.复制redis.conf文件
切换到redis根目录
cd /usr/local/src/redis-3.2.8/
创建cluster/7000文件夹
mkdir -p cluster/7000
复制redis.conf到7000目录下
cp redis.conf cluster/7000
2.关闭aof,使用rdb模式
打开复制出来的redis.conf文件
cd cluster
vim 7000/redis.conf
修改完成退出vim
使用 cp -r 7000 700x 命令复制出剩余的8个文件夹
[root@localhost cluster]# cp -r 7000 7002
[root@localhost cluster]# cp -r 7000 7003
[root@localhost cluster]# cp -r 7000 7004
[root@localhost cluster]# cp -r 7000 7005
[root@localhost cluster]# cp -r 7000 7006
[root@localhost cluster]# cp -r 7000 7007
[root@localhost cluster]# cp -r 7000 7008
打开redis.conf文件
vim 7001/redis.conf
在vim中,使用替换命令,将配置文件中多处7000修改为指定端口
:%s/7000/7001/g
重复上面的操作,依次完成7002到7008的端口修改
使用vim新建start.sh脚本
vim start.sh
启动 start.sh
./start.sh
创建redis集群,在redis根目录下运行
./src/redis-trib.rb create --replicas 2 192.168.126.166:7000 192.168.126.166:7001 192.168.126.166:7002 192.168.126.166:7003 192.168.126.166:7004 192.168.126.166:7005 192.168.126.166:7006 192.168.126.166:7007 192.168.126.166:7008
说明:将redis主机关闭后,检测高可用是否能实现,并且当主机重启后,能否实现自动挂载。
核心思想:实例化对象
需求:
1.多例对象的创建。
2。有些对象,例如接口/抽象类等不能直接实例化,接口需要通过代理创建实例,抽象类需要子类创建实例。
3.需要对对象进行二次封装。
说明:静态工厂中必须要有静态方法。
调用方式:类名.静态方法名
Spring配置方式:
/jt-manage/src/main/resources/spring/factory.xml
<bean id="calendarA" class="com.jt.manage.factory.StaticFactory" factory-method="getInstance"/>
工厂模式编辑:
/jt-manage/src/main/java/com/jt/manage/factory/StaticFactory.java
public class StaticFactory {
public static Calendar getInstance(){
return Calendar.getInstance();
}
}
说明:实例工厂要求必须先创建工厂对象,然后通过工厂方法调用获取对象。
配置文件:
/jt-manage/src/main/resources/spring/factory.xml
<bean id="instanceFactory" class="com.jt.manage.factory.InstanceFactory"/>
<bean id="calendarB" factory-bean="instanceFactory" factory-method="getInstance"/>
工厂定义:
/jt-manage/src/main/java/com/jt/manage/factory/InstanceFactory.java
public class InstanceFactory {
public Calendar getInstance(){
return Calendar.getInstance();
}
}
说明:该模式由Spring内部调用,不需要做多余的配置,但是需要实现特定的接口FactoryBean
/jt-manage/src/main/java/com/jt/manage/factory/SpringFactory.java
public class SpringFactory implements FactoryBean<Calendar>{
@Override
public Calendar getObject() throws Exception {
return Calendar.getInstance();
}
@Override
public Class<?> getObjectType() {
return Calendar.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
配置文件定义:
/jt-manage/src/main/resources/spring/factory.xml
<bean id="calendarC" class="com.jt.manage.factory.SpringFactory"/>
工具类测试:
/jt-manage/src/test/java/com/jt/factory/TestFactory.java
@Test
public void testFactory(){
//1.实例化Spring容器
ApplicationContext context =
new ClassPathXmlApplicationContext("spring/factory.xml");
Calendar calendarA = (Calendar) context.getBean("calendarA");
System.out.println(calendarA.getTime());
Calendar calendarB = (Calendar) context.getBean("calendarB");
System.out.println(calendarB.getTime());
Calendar calendarC = (Calendar) context.getBean("calendarB");
System.out.println(calendarC.getTime());
}
@Test
public void testCluster(){
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.126.166",7000));
nodes.add(new HostAndPort("192.168.126.166",7001));
nodes.add(new HostAndPort("192.168.126.166",7002));
nodes.add(new HostAndPort("192.168.126.166",7003));
nodes.add(new HostAndPort("192.168.126.166",7004));
nodes.add(new HostAndPort("192.168.126.166",7005));
nodes.add(new HostAndPort("192.168.126.166",7006));
nodes.add(new HostAndPort("192.168.126.166",7007));
nodes.add(new HostAndPort("192.168.126.166",7008));
JedisCluster jedisCluster = new JedisCluster(nodes);
jedisCluster.set("aa", "小提莫哪里逃");
System.out.println(jedisCluster.get("aa"));
}
/jt-manage/src/main/resources/property/redis.properties
#最小空闲数
redis.minIdle=100
#最大空闲数
redis.maxIdle=300
#最大连接数
redis.maxTotal=1000
#客户端超时时间单位是毫秒
redis.timeout=5000
#最大建立连接等待时间
redis.maxWait=1000
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
#redis cluster
redis.cluster0=192.168.126.166:7000
redis.cluster1=192.168.126.166:7001
redis.cluster2=192.168.126.166:7002
redis.cluster3=192.168.126.166:7003
redis.cluster4=192.168.126.166:7004
redis.cluster5=192.168.126.166:7005
redis.cluster6=192.168.126.166:7006
redis.cluster7=192.168.126.166:7007
redis.cluster8=192.168.126.166:7008
/jt-manage/src/main/resources/spring/applicationContext-redis.xml
<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}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="minIdle" value="${redis.minIdle}" />
bean>
<bean id="jedisCluster" class="com.jt.common.factory.JedisClusterFactory">
<property name="propertySource">
<value>classpath:/property/redis.propertiesvalue>
property>
<property name="poolConfig" ref="poolConfig"/>
<property name="redisNodePrefix" value="redis.cluster"/>
bean>
/jt-common/src/main/java/com/jt/common/factory/JedisClusterFactory.java
public class JedisClusterFactory implements FactoryBean<JedisCluster>{
//引入pro配置文件
private Resource propertySource;
private JedisPoolConfig poolConfig;
private String redisNodePrefix;
//获取Set集合信息
public Set<HostAndPort> getNodes(){
Set<HostAndPort> nodes = new HashSet<>();
//操作pro对象
Properties pro = new Properties();
try {
pro.load(propertySource.getInputStream());
//遍历获取nodes节点数据
for (Object key : pro.keySet()) {
//将key变为String类型 目的判断字符串方便
String strKey = (String) key;
//判断哪个是redis节点数据
if(strKey.startsWith(redisNodePrefix)){
//IP:端口
String value = pro.getProperty(strKey);
String[] args = value.split(":");
HostAndPort hostAndPort =
new HostAndPort(args[0],Integer.parseInt(args[1]));
nodes.add(hostAndPort);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return nodes;
}
@Override
public JedisCluster getObject() throws Exception {
Set<HostAndPort> nodes = getNodes();
return new JedisCluster(nodes, poolConfig);
}
@Override
public Class<?> getObjectType() {
return JedisCluster.class;
}
@Override
public boolean isSingleton() {
return false;
}
public Resource getPropertySource() {
return propertySource;
}
public void setPropertySource(Resource propertySource) {
this.propertySource = propertySource;
}
public JedisPoolConfig getPoolConfig() {
return poolConfig;
}
public void setPoolConfig(JedisPoolConfig poolConfig) {
this.poolConfig = poolConfig;
}
public String getRedisNodePrefix() {
return redisNodePrefix;
}
public void setRedisNodePrefix(String redisNodePrefix) {
this.redisNodePrefix = redisNodePrefix;
}
}