Redis and Springboot Connection integration under CENTOS7 environment

Source: Internet
Author: User
Tags redis desktop manager redis server

1. Official download installation file redis-4.0.2 version, unzip, compile (GCC, TCL), make Test no problem, compile successfully

2. Switch to Redis directory, execute src/redis-server redis.conf &

redis.conf Content Note 2 point a) note bind 127.0.0.1 B) Turn off protected mode and allow other IP remote connections

3. Execution Src/redis-cli-h 192.168.xx.xx-p 6379

If set key value succeeds, then it is possible to connect remotely ( pay special attention to whether the firewall on the server is down ).

4 Recommended local Redis Visualizer: Redis Desktop Manager

5.springboot Connection Integration operation

@Configuration @enablecaching Public classRedisconfigextendsCachingconfigurersupport {@Bean Publickeygenerator Keygenerator () {return NewKeygenerator () {@Override Publicobject Generate (Object target, Method method, Object ... params) {StringBuilder sb=NewStringBuilder ();                Sb.append (Target.getclass (). GetName ());                Sb.append (Method.getname ());  for(Object obj:params) {sb.append (obj.tostring ()); }                returnsb.tostring ();    }        }; } @SuppressWarnings ("Rawtypes") @Bean PublicCacheManager CacheManager (redistemplate redistemplate) {Rediscachemanager RCM=NewRediscachemanager (redistemplate); returnRCM; }     PublicRedistemplate<string, string>redistemplate (redisconnectionfactory redisconnectionfactory) {stringredistemplate template=Newstringredistemplate (redisconnectionfactory); Jackson2jsonredisserializer Jackson2jsonredisserializer=NewJackson2jsonredisserializer (Object.class); Objectmapper om=NewObjectmapper ();        Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);        Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);        Jackson2jsonredisserializer.setobjectmapper (OM);        Template.setvalueserializer (Jackson2jsonredisserializer);        Template.afterpropertiesset (); returntemplate; }}

Properties File Configuration

#redis配置 # Redis Database index (default 0) spring.redis.database=0  # Redis server address Spring.redis.host= 192.168.198.xxx# Redis Server connection Port spring.redis.port=6379  # Redis Server connection password (default is empty) Spring.redis.password =# Connection pool Maximum number of connections (using negative values to indicate no limit) Spring.redis.pool.max-active=8  # Connection pool Maximum blocking wait time (with negative values for No limit) Spring.redis.pool.max-wait=-1  # maximum idle connections in the connection pool Spring.redis.pool.max-idle =8  # Minimum idle connection in connection pool spring.redis.pool.min-idle=0  # Connection time-out (ms) Spring.redis.timeout=0

Test class

@RunWith (Springjunit4classrunner.class) @SpringBootTest (Classes= Demodruidtestapplication.class) @WebAppConfiguration Public classTestredis {@AutowiredPrivatestringredistemplate stringredistemplate; @AutowiredPrivateredistemplate redistemplate; @Test Public voidTest () {Stringredistemplate.opsforvalue (). Set ("Test1", "111"); Assert.assertequals ("111", Stringredistemplate.opsforvalue (). Get ("Test1")); }}

Redis and Springboot Connection integration under CENTOS7 environment

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.