一.創建spring boot項目
org.springframework.bootspring-boot-starter-data-redisorg.springframework.bootspring-boot-starter-webcom.alibabafastjson1.2.41
二.編輯yml配置文件
server: port: 7888 # 日誌配置 logging: config: classpath:log/logback.xml level: cn.com.dhcc: info org.springframework: info org.springframework.web: info com.alibaba.nacos.client.naming: error spring: redis: host: localhost port: 6379 password: ********* database: 1 jedis: pool: max-idle: 8 max-active: 8 max-wait: -1 min-idle: 0 timeout: 5000
三.配置Redis
@Configuration public class RedisConfiguration { /** * 實例化 RedisTemplate 對象 * * @return */ @Bean("RedisTemplateS") public RedisTemplate
functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplateredisTemplate = new RedisTemplate<>(); initDomainRedisTemplate(redisTemplate, redisConnectionFactory); return redisTemplate; } /** * 設置數據存入 redis 的序列化方式,並開啟事務 * * @param redisTemplate * @param factory */ private void initDomainRedisTemplate(@Qualifier("RedisTemplateS") RedisTemplateredisTemplate, RedisConnectionFactory factory) { // 如果不配置Serializer,那麼存儲的時候缺省使用String,如果用User類型存儲,那麼會提示錯誤User can't cast to // String! redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); FastJsonRedisSerializer
[wooen
]
Spring boot+redis實現消息發佈與訂閱的代碼已經有305次圍觀
http://coctec.com/docs/java/show-post-232701.html