查看“Spring Boot 连 Redis 主从模式”的源代码
←
Spring Boot 连 Redis 主从模式
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
=== pom.xml === <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.github.jihch</groupId> <artifactId>spring-boot-redis-master-slave</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.13</version> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> </project> </syntaxhighlight> === application.yml === <syntaxhighlight lang="yaml"> spring: redis: host: 127.0.0.1 port: 6379 password: vn4sj5kbxdaG </syntaxhighlight> === WriteToMasterReadFromReplicaConfiguration.java === <syntaxhighlight lang="java"> package io.github.jihch.config; import io.lettuce.core.ReadFrom; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; @Configuration public class WriteToMasterReadFromReplicaConfiguration { @Autowired private RedisProperties redisProperties; @Bean public RedisConnectionFactory redisConnectionFactory() { LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder() .readFrom(ReadFrom.REPLICA_PREFERRED) .build(); RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort()); serverConfig.setPassword(redisProperties.getPassword()); return new LettuceConnectionFactory(serverConfig, clientConfig); } } </syntaxhighlight> === 具体实现机制 === 虽然 application.yml 只进行了 redis master server 的配置,但这确实是一套读写分离的实现,最关键的地方在 <code>WriteToMasterReadFromReplicaConfiguration.java</code> 这个配置类; 经过了这个配置类中对 <code>RedisConnectionFactory</code> 在容器中的 bean 的定义使得 RedisTemplate 的 bean 在底层应用了往 master 上写、从 replica 上读; 这个类的配置让更底层的 Lettuce 在成功连接了 master 之后,通过 '''<big>info</big>''' 命令,在 master 上得到了 replica 列表,之后跟 replica 成功建立了 AIO 的 channel 并保存在了内存中; 之后再有的读操作就直接通过这些到 replica 的 AIO channel 交互了。 所以虽然停止了 redis 的 master server 后台会一直报连不上的异常,但读操作仍然可以通过和 replica 的 AIO channel 交互得到数据。 === 参考 === https://docs.spring.io/spring-data/redis/docs/2.5.12/reference/html/#redis:write-to-master-read-from-replica https://github.com/lettuce-io/lettuce-core/wiki/ReadFrom-Settings#masterreplica-connections-masterslave https://github.com/lettuce-io/lettuce-core/wiki/ReadFrom-Settings#read-from-settings
返回至
Spring Boot 连 Redis 主从模式
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息