In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how Java, Spring and Springboot integrate Redis database". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how Java, Spring and Springboot integrate Redis databases.
Java integrates Redis
1. Introduce dependency or import jar package redis.clients jedis 2.9.02, code implementation public class JedisTest {public static void main (String [] args) {/ / connect redis / / Jedis jedis=new Jedis ("192.168.65.128", 6379); / / use Jedis connection pool Jedis jedis=getJedis (); / / operate redis jedis.set ("name", "rookie"); jedis.set ("age", "19"); System.out.println ("Operation successful!") ; jedis.close ();} public static Jedis getJedis () {/ / create connection pool configuration object JedisPoolConfig config=new JedisPoolConfig (); config.setMaxIdle (10); config.setMinIdle (5); config.setMaxTotal (100); / / use the first creation method / / JedisPool jedisPool=new JedisPool (config, "192.168.65.128", 637910000, "root") when the service password of redis is required; JedisPool jedisPool=new JedisPool (config, "192.168.65.128", 6379line 10000) Return jedisPool.getResource ();}} Spring integrates Redis
1. Add dependency org.springframework.data spring-data-redis 2.1.8.RELEASE redis.clients jedis 2.9.02, redis configuration file 3, injection template object @ RunWith (SpringJUnit4ClassRunner.class) @ ContextConfiguration (locations = "classpath:application-redis.xml") public class RedisTest {@ Autowired private RedisTemplate redisTemplate; @ Test public void test () {/ / redisTemplate.opsForValue () .set ("name", "Blackie") Object name = redisTemplate.opsForValue () .get ("name"); System.out.println (name); System.out.println ("operation complete");}}
Note: after integrating redis with Spring (SpringBoot), the RedisTemplate object comes with the serialization capabilities of key and value. When operating on the client side of redis, the key obtained is the serialized key.
Think about it: why should Spring provide a serialization feature? Store objects in redis for the convenience of developers. Custom serialization types can be configured in xml.
Springboot integrates Redis
1. Add dependency org.springframework.boot spring-boot-starter-data-redis
2. Configure application.yml
3. Inject template object
@ RunWith (SpringRunner.class) @ SpringBootTestclass SpringbootRedisApplicationTests {@ Autowired private RedisTemplate redisTemplate; @ PostConstruct public void init () {/ / configure the key value serialization method of String type redisTemplate.setStringSerializer (new StringRedisSerializer (); redisTemplate.setValueSerializer (new StringRedisSerializer ());} @ Test void contextLoads () {redisTemplate.opsForValue (). Set ("age", 12); Object age = redisTemplate.opsForValue (). Get ("age"); System.out.println (age) System.out.println ("operation succeeded");} / / get objects of several data structures @ Test public void getRedisType () {/ / 1, operation string data type redisTemplate.opsForValue (); / 2, operation hash data type redisTemplate.opsForHash (); / / 3, operation List data type redisTemplate.opsForList (); / / 4, operation Set data type redisTemplate.opsForSet () / / 5. Data type redisTemplate.opsForZSet () of operation hSet; / / 6, data type of operation cardinality redisTemplate.opsForHyperLogLog ();}}
Note: custom serialization cannot be configured in the yml configuration file. You can trigger the execution method through the @ PostConstruct annotation in the springboot startup class or test class, thus achieving the effect of configuring custom serialization.
Add:
1.@PostConstruct description
The method modified by @ PostConstruct runs when the server loads Servlet and is called only once by the server, similar to the inti () method of Serclet. Methods modified by @ PostConstruct run after the constructor and before the init () method.
2.@PreDestroy description
The method modified by @ PreDestroy runs when the server uninstalls Servlet and is called only once by the server, similar to the destroy () method of Servlet. The method modified by @ PreDestroy runs after the destroy () method, before the Servlet is completely uninstalled.
Summary
1. When the project reports the following error: Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
The reason for the error: the redis service did not set the password, but the redis password was written in the project configuration file
Solution:
1) set the password password in the project configuration file to empty or not.
2) set the redis service password
-- you can directly modify the requirepass attribute in the redis.conf configuration file. If the modification does not take effect, you can modify it by command to enter the client of redis.
Redis 127.0.0.1 root 6379 > CONFIG SET requirepass "root" OKredis 127.0.0.1 OKredis 6379 > AUTH rootOk
Then restart the project to connect to the native redis service.
At this point, I believe you have a deeper understanding of "how Java, Spring and Springboot integrate Redis databases". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.