SpringBoot2.0 rapid integration Jedis redis client connection service

Transfer:   https://blog.csdn.net/ljk126wy/article/details/93303627

 

SpringBoot2.0 rapid integration Jedis redis client connection service
1. brief redis
2. autoconfiguration integrated client Jedis
2.1 Jedis dependent incorporated
2.2 Jedis Properties defined class
2.3 defines Jedis clients automatically configure class
4. Test
1. brief redis
redis official described as follows:

Redis is an open source (BSD licensed), in-memory data structure
store, used as a database, cache and message broker. It supports data
structures such as strings, hashes, lists, sets, sorted sets with
range queries, bitmaps, hyperloglogs, geospatial indexes with radius
queries and streams. Redis has built-in replication, Lua scripting,
LRU eviction, transactions and different levels of on-disk
persistence, and provides high availability via Redis Sentinel and
automatic partitioning with Redis Cluster

Redis is an open source (BSD license) data structure stored in memory, as a database, cache and message broker. It supports a string (strings), Hash (of the hashes), a list of the ordered set (Lists), set (sets), with a range of query (sortedsets), bitmap (to the bitmaps), Super (hyperloglogs) logs with the query and the radius geospatial index streams and other data structures. Redis has a built-in replication, lua scripts, transactions, affairs and different levels of persistent disk, and provides high availability by automatically partitioning and Redis Redis Sentinel cluster

2. Automatic configuration integrated Jedis client
configuration you should first install redis service on a local or remote machine, you can refer to other Windows installation Redis tutorial can I expect Baidu or subsequent blog tutorial.

By introducing a spring-boot-starter-data-redis RedisTemplate we can use the object to perform various operations on the redis actually RedisTemplate is encapsulated on Lettuce and Jedis, the default is to use Lettuce.

But today we'll show you how to use the bypass RedisTemplate Jedis redis client to connect through SpringBoot auto-configuration feature.
Version Jedis autoconfiguration operation three points in total

Introducing jedis dependent
defined Jedis Properties class
definitions Jedis autoconfiguration class
2.1 Jedis dependent incorporated
initially introduced Jedis dependent code is as follows:

        <dependency>
            <the groupId> redis.clients </ the groupId>
            <the artifactId> jedis </ the artifactId>
        </ dependency>

because SpringBoot spring-boot-starter-data -redis integrated Jedis jedis we introduced directly without dependency relationship of jedis the version number.

2.2 Jedis Properties class defines
the configuration information to set the connection classes by JedisProperties reids service configuration. For example annotation ip, port configuration services specific properties see Notes following code.

Configuration information about class annotated description:

@Component: the declaration of annotation is a Spring Bean
@ConfigurationProperties: @ConfigurationProperties annotation by the prefix in the prefix parameter defining the configuration of application.properties. SpringBoot may be accomplished by injecting the annotation information application.properties configured in the JedisProperties.

For example, the following configuration information application.properties:

= 127.0.0.1 spring.jedis.host
spring.jedis.port = 6380

SpringBoot will help us to inject value application.properties the host and port configurations to JedisProperties members in the host and port properties.

Specific code as follows:

@Component
@ConfigurationProperties(prefix="spring.jedis")
public class JedisProperties {

    / ** Redis server host * ip /
    Private String Host = "localhost";
    / ** Redis server login password * /
    Private String password;
    / ** Redis server port * /
    Private int Port = 6379;
    / ** connection timeout * /
    Private int timeout = 2000;
    / connection pool configuration ** * /
    Private pool pool the pool = new new ();
    // get and set methods omitted ...
}

public class pool {
    / ** The maximum number of connections, the default 8 * /
    Private maxTotal = int. 8;
    / ** The maximum number of idle connections, eight default * /
    Private maxIdle = int. 8;
    / ** minimum number of idle connections, default * 0 /
    Private minIdle int = 0;
    Private Long maxWaitMillis;
    / / get and set methods omitted ...    
    }

Redis configuration instructions

If you start a local redis service then you do not need any configuration can operate redis up.

If you are in redis installed in a remote server. Then you need the following configuration:

# Required configuration
spring.jedis.host = Redis server host ip
spring.jedis.port Redis server port =
# If you redis service is configured with a password that must be configured.
spring.jedis.password = Redis server login password
# non-mandatory configuration
spring.jedis.timeout = connection timeout
spring.jedis.pool.max-total = maximum number of connections, default 8
spring.jedis.pool.max-idle = the maximum number of idle connections, the default 8
spring.jedis.pool.min-idle = minimum number of idle connections, default 0
spring.jedis.pool.max-of millis = the wait-for maximum number of milliseconds to wait for a connection

2.3 customer defined Jedis end automatic configuration class
auto-configuration is one of the core functions of SpringBoot, in SpringBoot actual book is so described: Spring applications for many common application functionality, can automatically provide SpringBoot configuration. It is configured like a model. And SprignBoot automatic configuration is very flexible, we can also be done automatically configured by overlaying custom configuration.

About Auto Configuration Notes Introduction:

@Configuration + @Bean: javaConfig operating combinations and configurations in xml Bean is an effect.

@ConditionalOnClass: parameters of the corresponding annotation class must exist, or do not resolve the modified configuration class notes

@EnableConfigurationProperties: JedisProperties as a Bean will be introduced in JedisAutoConfiguration

@ConditionalOnMissingBean: The comment said that if it modifies Bean classes exist, do not need to create the Bean; can pass parameters such @ConditionOnMissingBean (name = "example") to the notes, this means that if the name is "example" the bean exists, the annotation which does not execute the modified code blocks.

End Annotations The following describes the basic role is to configure and generate JedisPool JedisPoolConfig object. Refer to the following code:

@Configuration
@ConditionalOnClass(JedisPool.class)
@EnableConfigurationProperties(JedisProperties.class)
public class JedisAutoConfiguration {
    private JedisProperties jedisProperties;
    
    public JedisAutoConfiguration(JedisProperties jedisProperties) {
        this.jedisProperties = jedisProperties;
    }
    
    @Bean
    @ConditionalOnMissingBean(JedisPool.class)
    public JedisPool jedisPool() {
        
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(jedisProperties.getPool().getMaxIdle());
        poolConfig.setMaxTotal(jedisProperties.getPool().getMaxTotal());
        poolConfig.setMaxWaitMillis (jedisProperties.getPool () getMaxWaitMillis () * 1000.);
        
        JedisPool jedisPool = new new JedisPool (poolConfig, jedisProperties.getHost (), jedisProperties.getPort (),
                jedisProperties.getTimeout () * 1000, jedisProperties.getPassword () , 0);
        return jedisPool;
    }
}

4. test
function is necessary for completion of the development developed functions tested,
SpringBoot need to introduce testing @RunWith () and @SpringBootTest annotation on the test class.
By @Autowired JedisPool injected into the test classes
and objects acquired by JedisPool Jedis can operate the redis.

Specific test code is as follows:

@RunWith (SpringRunner.class)
@SpringBootTest
public class JedisTest {
    
    @Autowired
    Private JedisPool jedisPool;
    @Test
    public void SET () {
        Jedis jedis jedisPool.getResource = ();
        jedis.set ( "ABC", "123");
    }
    
    @Test
    public void GET () {
        Jedis jedis jedisPool.getResource = ();
        String value = jedis.get ( "ABC");
        System.out.println (value);
    }
}

SpringBoot2.0 rapid integration Jedis client connection redis service specific code, please visit my github view.

github link: HTTPS: //github.com/zhuoqianmingyue/springbootexamples/tree/master/lesson17_redis
--------------------- 
Author: IT Utopia - moon table 
Source : CSDN 
original: https: //blog.csdn.net/ljk126wy/article/details/93303627 
copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/qq_36688928/article/details/93465928