当前位置: 首页 > news >正文

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.82port: 6381password: 123456database: 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 {@Autowiredprivate RedisTemplate redisTemplate;
​@RequestMapping("/test1")public Object test1(){//新增redisTemplate.opsForValue().set("name", "zhangsan");//查询return redisTemplate.opsForValue().get("name");}
​
}

4、测试

5、redis 序列化及常用方法

@SpringBootApplication
public class Mybatis03Application {
​@Beanpublic 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 {@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate 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");//自增加1redisTemplate.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{@Autowiredprivate RedisTemplate redisTemplate;@Autowiredprivate QueenMapper queenMapper;@Overridepublic 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;}
}
http://www.lqws.cn/news/458839.html

相关文章:

  • STM32[笔记]--开发环境的安装
  • 三种经典算法无人机三维路径规划对比(SMA、HHO、GWO三种算法),Matlab代码实现
  • Python 中设置布尔值参数为 True 来启用验证
  • Rsync异地备份的使用方式
  • 网络编程简介与Netty实战:从入门到高性能Echo服务器
  • Node.js 在前端开发中的作用与 npm 的核心理解
  • FPGA基础 -- Verilog 的值变转储文件(VCD:Value Change Dump)
  • Spring Boot + MyBatis + Vue:全栈开发中的最佳实践
  • 压铸件超声波清洗设备有哪些常见的故障原因?
  • Android Kotlin 用法对比Java使用小结
  • 阿里云OSS对象云储存入门操作
  • 前端工程结构设计指南:如何让模块解耦、易维护、可拓展
  • 讯方“教学有方”平台获华为昇腾应用开发技术认证!
  • Linux系统时间不对导致mysql初始化失败:Data Dictionary initialization failed.(数据字典版本验证失败)
  • 【案例分享】如何用 DHTMLX Scheduler 构建灵活高效的资源调度系统?
  • Vue 比较两个数组对象,页面展示差异数据值
  • 1.22Node.js 中操作 Redis
  • 党建赋能 医校协同|广州附医华南医院与湖南中医药高等专科学校签约携手共育英才
  • Unity3D仿星露谷物语开发67之创建新的NPC
  • HTTP Server
  • 基于RISV-V的矿业网关,支持矿鸿等国产系统
  • 树莓派倾斜传感器实验指导书
  • 为什么你的vue项目连接不到后端
  • Linux 内核同步管理全解:原理 + 实战 + 考点
  • 【服务器R环境架构】基于 micromamba下载 R 库包
  • 企业实践 | 银河麒麟KylinOS-V10(SP3)高级服务器操作系统基础安装指南
  • 无人机吊舱热成像伪彩模式设计分析
  • Hadoop 技术生态体系
  • 如何填写“appium inspector”内容?
  • RAG工程落地:处理文档中表格数据