Steps to implement custom Redis connections in SpringBoot
Install Redis
docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -v /mydata/redis/conf/:/etc/redis/ -d redis redis-server /etc/redis/
Pom file lead package
<dependency> <groupId></groupId> <artifactId>redisson-spring-boot-starter</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
3. Custom properties
Add the configuration you want to the file
redis: config: host: 192.168.200.142 #Fill in the IP of the computer you installed with Redis port: 6379 password: Your password pool-size: 10 min-idle-size: 5 idle-timeout: 30000 connect-timeout: 5000 retry-attempts: 3 retry-interval: 1000 ping-interval: 60000 keep-alive: true
4. Read the configuration
@Data @ConfigurationProperties(prefix = "", ignoreInvalidFields = true) public class RedisClientConfigProperties { private String host; private int port; private String password; private int poolSize = 64; private int minIdleSize = 10; private int idleTimeout = 10000; private int connectTimeout = 10000; private int retryAttempts = 3; private int retryInterval = 1000; private int pingInterval = 0; private boolean keepAlive = true; }
- The meaning of each parameter is shown below
ignoreInvalidFields field meaning
When this property is set to true, Spring ignores properties in the configuration file that do not match the configuration class field. This means that if there are properties in the configuration file that do not exist with the configuration class field, Spring does not throw an exception, but ignores these properties.
Injection container
@Configuration @EnableConfigurationProperties() public class RedisClientConfig { @Bean("redissonClient") public RedissonClient redissonClient(ConfigurableApplicationContext applicationContext, RedisClientConfigProperties properties) { Config config = new Config(); //Set the encoder (new JsonJacksonCodec()); () .setAddress("redis://" + () + ":" + ()) .setPassword(()) .setConnectionPoolSize(()) .setConnectionMinimumIdleSize(()) .setIdleConnectionTimeout(()) .setConnectTimeout(()) .setRetryAttempts(()) .setRetryInterval(()) .setPingConnectionInterval(()) .setKeepAlive(()) ; return (config); }
Meaning of each parameter
-
setAddress
: The address and port of the connection -
setPassword
:password -
setConnectionPoolSize
: Set the size of the connection pool -
setConnectionMinimumIdleSize
: Set the minimum number of idle connections for the connection pool -
setIdleConnectionTimeout
: Set the maximum idle time of the connection (unit: milliseconds). Idle connections exceeding this time will be closed. -
setConnectTimeout
: Set the connection timeout time (unit: milliseconds) -
setRetryAttempts
: Set the number of connection retry times -
setRetryInterval
: Set the interval time for connection retry (unit: milliseconds) -
setPingConnectionInterval
: Set the time interval (unit: milliseconds) for periodic checking whether the connection is available -
setKeepAlive
: Set whether to maintain long connections
This is the article about the process steps for SpringBoot to implement custom Redis connections. For more relevant SpringBoot custom Redis connection content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
How to use Java CompletableFuture
This article mainly introduces the usage of Java CompletableFuture, which has good reference value. I hope it will be helpful to everyone. If there are any errors or no complete considerations, I hope you will be very encouraged.2024-07-07Simple implementation of springboot thread pool monitoring
This article mainly introduces the simple implementation of springboot thread pool monitoring. The example code is introduced in this article in detail and has certain reference value. Interested friends can refer to it.2022-01-01Spring WebFlux Guide
This article mainly introduces the use guide of Spring WebFlux to help everyone better understand and learn to use the Spring framework. Interested friends can learn about it.2021-05-05Java implements the code of command interaction under the docker container in Linux (centos) (Configuration Wizard)
The editor encountered a requirement when developing a project, because the system deployment requires multiple machines to be split and deployed every time, which is very troublesome. How to configure it more conveniently? Today, the editor will introduce to you the code (configuration wizard) for implementing command interaction in the docker container in Linux (centos). Interested friends, let’s take a look.2021-05-05How much do you know about the 27 most core annotations in Spring Boot?
This article mainly introduces the 27 most core annotations that explain Spring Boot in detail. How much do you know? The article introduces the sample code in detail, which has a certain reference learning value for everyone's study or work. If you need it, please follow the editor to study together.2019-08-08MybatisPlus auto-fill creation (update) time issue
When developing database-related applications, manually setting the creation and update time will lead to code redundancy. MybatisPlus provides automatic filling function. By implementing the MetaObjectHandler interface and rewriting the insertFill and updateFill methods, fields such as creation time and update time can be automatically maintained, greatly simplifying the code. This not only improves development efficiency, but also ensures data traceability.2024-09-09How Java uses interfaces to avoid function callbacks
This article mainly introduces how Java uses interfaces to avoid function callbacks. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let's take a look with the editor2018-02-02New Java access to mysql database tool class operation code
This article introduces you to the new Java method to access the mysql database tool class through example code. This article introduces you very detailedly through example code, which has certain reference value for your study or work. If you need it, please refer to it.2021-12-12ssh framework implements file upload and download instance code
This article mainly introduces the example code for uploading and downloading of ssh framework files. The example analyzes the usage skills of Spring+struts+Hibernate, which are very practical. Friends who need it can refer to it.2017-03-03Complete example of MyBatis one-to-many nested query
This article mainly introduces relevant information about MyBatis one-to-many nested query. The article introduces the sample code in detail, which has certain reference learning value for everyone's study or work. Friends who need it, please learn with the editor below.2021-01-01