站长网_站长创业_站长主页_站长之家_易采站长站

会员投稿 投稿指南 站长资讯通告: Spring boot+redis实现消息发布与订阅的代码
搜索:
您的位置: 主页 > 教程 > 数据库 > Redis > » 正文

Spring boot+redis实现消息发布与订阅的代码

来源: 易采站长站

一.创建spring boot项目

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.41</version>
  </dependency>

二.编辑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<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
  RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
  initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
  return redisTemplate;
 }

 /**
  * 设置数据存入 redis 的序列化方式,并开启事务
  * 
  * @param redisTemplate
  * @param factory
  */
 private void initDomainRedisTemplate(@Qualifier("RedisTemplateS") RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
  // 如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to
  // String!
  redisTemplate.setKeySerializer(new StringRedisSerializer());
  redisTemplate.setHashKeySerializer(new StringRedisSerializer());

  FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<Object>(Object.class);
  redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
  redisTemplate.setValueSerializer(fastJsonRedisSerializer);
  //redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
  //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
  // 开启事务
  redisTemplate.setEnableTransactionSupport(true);
  redisTemplate.setConnectionFactory(factory);
 }

 /**
  * 注入封装RedisTemplate @Title: redisUtil @return RedisUtil @date
  * 
  */
 @Bean(name = "redisUtils")
 public RedisUtils redisUtil(@Qualifier("RedisTemplateS") RedisTemplate<String, Object> redisTemplate) {
  RedisUtils redisUtil = new RedisUtils();
  redisUtil.setRedisTemplate(redisTemplate);
  return redisUtil;
 }

四.编写RedisUtil消息发布方法

public class RedisUtils {
 private static final Logger log = LoggerFactory.getLogger(RedisUtils.class);

 private RedisTemplate<String, Object> redisTemplate;

 public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
  this.redisTemplate = redisTemplate;
 }

 public void publish(String channal ,Object obj) {
  redisTemplate.convertAndSend(channal,obj );
 }
}
            
最新图文资讯
1 2 3 4 5 6
易采站长站 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助 -