Redis03

一、主从复制

一主二从

主机:master 读写

从机:slave 只读

复制代码
redis.conf配置文件
requirepass 123456
masterauth 123456
​
​
​
docker run -id -p 6379:6379 --name redis7.2 -v  /opt/redis/redis.conf:/usr/local/bin/redis.conf -v  /usr/local/docker/data:/data  redis:7.0.2  redis-server /usr/local/bin/redis.conf
​
docker run -id -p 6380:6379 --name redis7.22 -v  /opt/redis/redis2.conf:/usr/local/bin/redis.conf -v  /usr/local/docker/data2:/data  redis:7.0.2  redis-server /usr/local/bin/redis.conf
​
docker run -id -p 6381:6379 --name redis7.23 -v  /opt/redis/redis3.conf:/usr/local/bin/redis.conf -v  /usr/local/docker/data3:/data  redis:7.0.2  redis-server /usr/local/bin/redis.conf
复制代码

默认情况下,直接启动,三台服务,三台服务都是master主机。

实现主从复制、读写分离

选6379为master,

在6380 和 6381上,分别执行slaveof ip 6379 ,让当前主机为slave从机,让6379为master主机。

测试:

1、防火墙开放端口

firewall-cmd --zone=public --add-port=6379-6381/tcp --permanent

2、云服务器网络安全组放行(腾讯防火墙放行)

6379 6380 5381

3、重启docker容器

systemctl restart docker

4、依次启动三个容器,在从机上守护主机

docker start redis7.2

docker start redis7.22

docker start redis7.23

客户端连接:

复制代码
redis-cli -h ip -p 6380 -a 123456
slaveof  ip 6379
info replication

主从切换(反客为主)

主机故障,从机变主机的过程。

手动切换

在从机上执行

复制代码
slaveof no one   让自己变为master
其他服务器守护新的主机
slaveof ip port

哨兵模式

可以实现自动故障切换。

二、springboot整合redis

1、添加依赖

复制代码
<!--redis依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2、添加配置

复制代码
spring:
  redis:
    host: 1.94.230.82
    port: 6381
    password: 123456
    database: 0

3、使用模版工具类完成redis的增删改查

复制代码
package com.hl.mybatis03.web;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
@RequestMapping("/redis")
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;
​
    @RequestMapping("/test1")
    public Object test1(){
        //新增
        redisTemplate.opsForValue().set("name", "zhangsan");
        //查询
        return redisTemplate.opsForValue().get("name");
    }
​
}

4、测试

5、redis 序列化及常用方法

复制代码
@SpringBootApplication
public class Mybatis03Application {
​
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // 必须为 String 序列化
        return template;
    }

package com.hl.mybatis03.web;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
import java.util.ArrayList;
import java.util.List;
​
@RestController
@RequestMapping("/redis")
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
​
    @RequestMapping("/test1")
    public Object test1(){
        //新增 修改
        redisTemplate.opsForValue().set("name", "zhangsan");
        redisTemplate.opsForValue().set("age",20);
        List list = new ArrayList();
        list.add("beijing");
        list.add("nanjing");
        list.add("bianjing");
        redisTemplate.opsForValue().set("list",list);
​
        redisTemplate.opsForValue().increment("age");//自增加1
        redisTemplate.opsForValue().increment("age",10); //自增长度  自定义
​
        redisTemplate.opsForValue().append("name"," hello! "); //追加内容
​
        String name = (String)redisTemplate.opsForValue().get("name");
        System.out.println(name.length());
​
        redisTemplate.opsForValue().getAndDelete("list");
​
​
        return null;
    }
​
}

6、springboot+mysql+redis 业务整合

复制代码
@Service
public class QueenServiceImpl implements QueenService{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QueenMapper queenMapper;
    @Override
    public List<Queen> getQueens() {
        Object obj = redisTemplate.opsForValue().get("queen");
        List<Queen> list = null;
        if(obj !=null ){
            list = (List<Queen>)obj;
        }else{
            list = queenMapper.getQueens();
            redisTemplate.opsForValue().set("queen",list);
        }
​
        return list;
    }
}
相关推荐
欢呼的太阳1 小时前
数据库设计Step by Step (10)——范式化
服务器·数据库·oracle
折哥的程序人生 · 物流技术专研1 小时前
第4篇:Lambda 简化策略模式(Java 8+)
java·设计模式·策略模式·函数式编程·lambda·代码简化·扩充系列
researcher-Jiang2 小时前
高性能计算之OpenMP——超算习堂学习1
android·java·学习
夜雪一千2 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
西门吹-禅3 小时前
java springboot N+1问题
java·开发语言·spring boot
DLYSB_3 小时前
生命通道:如何用 HIS 医疗系统直连网络声光终端,打造“零延误”的危急值响应网关?
java·网络·数据库·报警灯
weixin_BYSJ19874 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php
AI小码4 小时前
LLM 应用的缓存工程:当每次 API 调用都在燃烧成本
java·人工智能·spring·计算机·llm·编程·api
大E帝国子民14 小时前
MacOS 安装Seismic Unix
服务器·macos·unix
德福危险4 小时前
富有想象力的XSS盲打:靶机练习之Tempus_fugit5
服务器·网络·xss