redis組件,Redis-與SpringBoot的集成

 2023-10-15 阅读 37 评论 0

摘要:其實spring boot與redis集成特別簡單,只是有些小白java開發人員(無意冒犯)非要寫成配置類的形式,還用spring framework的思想去開發spring boot,讓我對國內的軟件環境以及java程序員的未來感到擔憂 在spring boot引用redis需要下面三步&#x

其實spring boot與redis集成特別簡單,只是有些小白java開發人員(無意冒犯)非要寫成配置類的形式,還用spring framework的思想去開發spring boot,讓我對國內的軟件環境以及java程序員的未來感到擔憂

在spring boot引用redis需要下面三步:
(1)首先,pom.xml文件如下

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

redis組件?(2)application.properties

spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=密碼

(3)

// 哪里用到redis,哪里就注入
@Autowired
private RedisTemplate<String, String> redisTemplate;public void aa(){BoundValueOperations<String, String> redis= redisTemplate.boundValueOps("綁定一個key");//取值redis.get();//賦值redis.set("value也就是內容", 7000, TimeUnit.SECONDS);
}

以上三步即可在spring boot中使用redis,然而在實際開發中,不了解spring boot的人有可能使用下面的方法注入redis,雖然這是可行的,但是我個人不推薦

redis的部署方式,先寫配置類

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;@Configuration
@EnableAutoConfiguration
public class RedisConfig {@Bean@ConfigurationProperties(prefix = "spring.redis")public JedisPoolConfig getRedisConfig() {JedisPoolConfig config = new JedisPoolConfig();return config;}@Bean@ConfigurationProperties(prefix = "spring.redis")public JedisConnectionFactory getConnectionFactory() {JedisConnectionFactory factory = new JedisConnectionFactory();factory.setHostName("域名或者IP");factory.setPort(6380);//注意這里,確認你的redis是否ssl鏈接,不是請falsefactory.setUseSsl(true);factory.setPassword("密碼");JedisPoolConfig config = getRedisConfig();factory.setPoolConfig(config);return factory;}@Beanpublic RedisTemplate<?, ?> getRedisTemplate() {RedisTemplate<?, ?> template = new StringRedisTemplate(getConnectionFactory());return template;}
}

最后說一下redis的序列化注意事項,取值的時候,要清楚的知道對方設置的時候采用的哪種序列化方式,比方下面的代碼就是我實際開發寫的,導致我不得不這樣寫的原因就是存儲值的那個開發小組,主庫存的是key-hash,其中key采用string序列化,而hash采用jackson序列化,并且,hash中的每個key又采用string序列化,hash中的value又采用jackson序列化(對于這種操作,顯然是存儲方對redis序列化不了解,但是沒有辦法,我又無法控制對方行為),只能寫出如下代碼

@Resource
private RedisTemplate<String, Object> redisTemplate;public void aa(){RedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class);RedisSerializer<String> stringSerializer = new StringRedisSerializer();redisTemplate.setKeySerializer(stringSerializer);redisTemplate.setValueSerializer(valueSerializer);redisTemplate.setHashKeySerializer(stringSerializer);redisTemplate.setHashValueSerializer(valueSerializer);BoundHashOperations<String, String, String> operations = redisTemplate.boundHashOps(tokenId);Map<String, String> value = operations.entries();String str = JSON.toJSONString(value);// 此處可以打印str
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/137130.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息