How to implement springboot integrated redis sentinel master-slave
1. Environment
spring boot 2.3.12.RELEASE
JDK 1.8
IntelliJ IDEA development tool
Redis sentinel master-slave construction
2. POM file
Others in the pom file are ignored, only system dependencies related to redis are displayed
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 重点:redis依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 对象池框架,redis依赖 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
3. Application.yml configuration
Ignore the configuration of springboot
spring convention is greater than configuration. For the default one, there is no need to reflect it in the configuration file
spring: redis: # redis库 database: 1 # redis节点的密码 password: jwssw # 集群配置 sentinel: # 集群哨兵节点配置,多个节点之间用英文逗号分割 nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381 # 主节点名称 master: mymaster # 密码 password: jwssw
Note that if the redis sentinel configuration file adds requirepass (access key), [password] must be added to the sentinel node, otherwise there is no need to add it.
4. reidsTemplate configuration
This configuration file can be loaded directly into the startup class, because the startup class is also a configuration class of springboot
/** * 方法描述: 初始化redis连接 * * @param factory redis连接工厂 * @return {@link RedisTemplate} */ @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { // 新建redisTemplate对象 RedisTemplate<String, Object> template = new RedisTemplate<>(); // 设置工厂 template.setConnectionFactory(factory); // 键值类型 template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); // 返回redisTemplate对象 return template; }
5. Unit test (JUnit4)
@RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { // 注入redisTemplate对象 @Autowired RedisTemplate<String, Object> redisTemplate; @Test public void setOrGetTest() { // redis键值 String redisKey = "name"; // 向redis存放内容 redisTemplate.opsForValue().set(redisKey, "张三" + new Random().nextInt()); // 获取redis中的内容并打印 System.out.println(redisTemplate.opsForValue().get(redisKey)); } }
The above is the detailed content of How to implement springboot integrated redis sentinel master-slave. For more information, please follow other related articles on the PHP Chinese website!

Redisoutperformstraditionaldatabasesinspeedforread/writeoperationsduetoitsin-memorynature,whiletraditionaldatabasesexcelincomplexqueriesanddataintegrity.1)Redisisidealforreal-timeanalyticsandcaching,offeringphenomenalperformance.2)Traditionaldatabase

UseRedisinsteadofatraditionaldatabasewhenyourapplicationrequiresspeedandreal-timedataprocessing,suchasforcaching,sessionmanagement,orreal-timeanalytics.Redisexcelsin:1)Caching,reducingloadonprimarydatabases;2)Sessionmanagement,simplifyingdatahandling

Redis goes beyond SQL databases because of its high performance and flexibility. 1) Redis achieves extremely fast read and write speed through memory storage. 2) It supports a variety of data structures, such as lists and collections, suitable for complex data processing. 3) Single-threaded model simplifies development, but high concurrency may become a bottleneck.

Redis is superior to traditional databases in high concurrency and low latency scenarios, but is not suitable for complex queries and transaction processing. 1.Redis uses memory storage, fast read and write speed, suitable for high concurrency and low latency requirements. 2. Traditional databases are based on disk, support complex queries and transaction processing, and have strong data consistency and persistence. 3. Redis is suitable as a supplement or substitute for traditional databases, but it needs to be selected according to specific business needs.

Redisisahigh-performancein-memorydatastructurestorethatexcelsinspeedandversatility.1)Itsupportsvariousdatastructureslikestrings,lists,andsets.2)Redisisanin-memorydatabasewithpersistenceoptions,ensuringfastperformanceanddatasafety.3)Itoffersatomicoper

Redis is primarily a database, but it is more than just a database. 1. As a database, Redis supports persistence and is suitable for high-performance needs. 2. As a cache, Redis improves application response speed. 3. As a message broker, Redis supports publish-subscribe mode, suitable for real-time communication.

Redisisamultifacetedtoolthatservesasadatabase,server,andmore.Itfunctionsasanin-memorydatastructurestore,supportsvariousdatastructures,andcanbeusedasacache,messagebroker,sessionstorage,andfordistributedlocking.

Redisisanopen-source,in-memorydatastructurestoreusedasadatabase,cache,andmessagebroker,excellinginspeedandversatility.Itiswidelyusedforcaching,real-timeanalytics,sessionmanagement,andleaderboardsduetoitssupportforvariousdatastructuresandfastdataacces


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
