Spring-boot-starter-redis Configuration Detailed

Source: Internet
Author: User
Tags connection pooling object object redis server

Spring-boot-starter-redis Configuration Detailed 

Spring-boot-starter-redis is mainly implemented by configuring the relevant parameters in the Redisconnectionfactory to connect Redis service. Redisconnectionfactory is an interface that has the following 4 specific implementation classes, which we typically use jedisconnectionfactory.

The basic configuration of Redis in the spring boot configuration file is as follows:

# Redis Server Address
# Redis Server Connection port
# Redis Server connection password (default is null, if the Redis service-side profile opens the Requirepass password, the corresponding configuration password should be filled in here)
# Connection time-out (milliseconds)

Top these 4 items are the basic configuration items in the Jedisconnectionfactory class, in fact, there are some such as connection pool, cluster, master-slave, Sentinel and other configuration, here first simple introduction of the next Connection pool (Jedispoolconfig), Need to know the other configuration can see the source code. Genericobjectpoolconfig is the parent class of Jedispoolconfig, which provides a total of three parameters for Maxtotal, Maxidle, and Maxidle, and also sets the default parameters.

# Connection Pool Maximum number of connections (use negative values to indicate no limit, corresponding to maxtotal)


# Maximum idle connections in the connection pool


# Minimum idle connections in the connection pool


Once the configuration file is configured, a Redis configuration class is also required to configure key and value serialization and to load the relevant parameters in the configuration file

If you only need to use the basic Redis configuration, then use the following configuration class, Spring boot will automatically scan the basic configuration of Redis, but one thing to note is that password, if you set password in the config file, Then you must manually inject jedisconnectionfactory in the configuration class, otherwise it will be reported Noauth authentication required during the startup process. :

  1. @Configuration
  2. @EnableCaching
  3. public class Redisconfig extends cachingconfigurersupport{
  4. @Bean
  5. Public keygenerator keygenerator() {
  6. return New Keygenerator () {
  7. Public Object generate(object target, Method method, Object ... params) {
  8. StringBuilder sb = new StringBuilder ();
  9. Sb.append (Target.getclass (). GetName ());
  10. Sb.append ("_"). Append (Method.getname ());
  11. For (Object obj:params) {
  12. Sb.append ("_"). Append (Obj.tostring ());
  13. }
  14. return sb.tostring ();
  15. }
  16. };
  17. }
  18. @SuppressWarnings ("Rawtypes")
  19. @Bean
  20. Public CacheManager cachemanager(redistemplate redistemplate) {
  21. Rediscachemanager RCM = new Rediscachemanager (redistemplate);
  22. //Set cache expiration Time
  23. //rcm.setdefaultexpiration (60);//sec
  24. return RCM;
  25. }
  26. @Bean
  27. Public redistemplate<string, string> redistemplate(Redisconnectionfactory factory) {
  28. Stringredistemplate template = new Stringredistemplate (factory);
  29. @SuppressWarnings ({ "rawtypes", "Unchecked"})
  30. Jackson2jsonredisserializer Jackson2jsonredisserializer = new Jackson2jsonredisserializer (Object.class);
  31. Objectmapper om = new Objectmapper ();
  32. Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);
  33. Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);
  34. Jackson2jsonredisserializer.setobjectmapper (OM);
  35. Template.setvalueserializer (Jackson2jsonredisserializer);
  36. Template.afterpropertiesset ();
  37. Jedisconnectionfactory JC = (jedisconnectionfactory) factory;
  38. System.out.println (Jc.gethostname ());
  39. return template;
  40. }
  41. }
If you have also configured parameters such as connection pooling, add the following in the configuration class above:

    1. @Bean
    2. Public jedisconnectionfactory redisconnectionfactory() {
    3. Jedisconnectionfactory factory = new Jedisconnectionfactory ();
    4. Factory.sethostname (host);
    5. Factory.setport (port);
    6. Factory.setpassword (password);
    7. Factory.settimeout (timeout); //Set connection time-out
    8. return factory;
    9. }
One thing to explain is that there are a number of properties in the configuration class that are injected into the configuration file, as you can see from the blog:

Click to open link

    1. Note that if the parameter type is long then the string type conversion exception is not possible.
    2. 3, redistemplate the default use of the serialization mode is Jdkserializationredisserializer, it does not have the top problem. Because its serialization method is serialize (object object)

Spring-boot-starter-redis Configuration Detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.