GuoTiantian_1207
EliasGuo
采纳率0%
2021-03-05 17:25

监听redis key失效事件,一直无法连接到redis

报错信息 Connection refused: no further information: localhost/127.0.0.1:6379

redis版本5.07

yml配置的是远程端口,发生问题时,redis是启动的,也可以远程访问;

以下是报错类的代码  麻烦各位帮忙看下是什么原因 比较急

package com.hem.xhlapi.redis.client;

import com.alibaba.fastjson.JSON;
import com.hem.xhlapi.enums.RedisPrefixEnum;
import com.hem.xhlapi.model.vo.BroadUserVO;
import com.hem.xhlapi.netease.model.BroadCastNoticeRequest;
import com.hem.xhlapi.service.facade.NeteaseFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;


/**
 * @ClassName RedisKeyExpirationListener
 * @Deacription redis键失效处置
 * @Author Leslie.Guo
 * @Date 2021/3/5 13:29
 * @Version 1.0
 **/
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    @Autowired
    private NeteaseFacade neteaseFacade;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }
    /**
     * 针对 redis 数据失效事件,进行数据处理
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {

        // 获取到失效的 key,进行取消订单业务处理
        String expiredKey = message.toString();
        System.out.println(expiredKey);
        String headlineKey = RedisPrefixEnum.ISSUE_HEADLINE.getKey();
        String issueBroadcastKey = RedisPrefixEnum.ISSUE_BROADCAST.getKey();
        BroadUserVO vo =new BroadUserVO();
        if(expiredKey.equals(headlineKey)){
            //封装头条信息
            vo.setBroadcastType("1");
            String s = JSON.toJSONString(vo);
            //推送云信数据
            BroadCastNoticeRequest sendRequest = new BroadCastNoticeRequest.Builder()
                    .withBody(s)
                    .build();
            // 发送广播消息
            neteaseFacade.asyncBroadcastMsg(sendRequest);
        }else if(expiredKey.equals(issueBroadcastKey)){
          vo.setBroadcastType("2");
            String s = JSON.toJSONString(vo);
            //推送云信数据
            BroadCastNoticeRequest sendRequest = new BroadCastNoticeRequest.Builder()
                    .withBody(s)
                    .build();
            // 发送广播消息
            neteaseFacade.asyncBroadcastMsg(sendRequest);
        }else{
            System.out.println(expiredKey);
        }
    }

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • technologist_06 CSDN技术专家团—月崖 1月前

    本地的redis服务是否启动了、配置文件的信息是否正确

    点赞 2 评论 复制链接分享

为你推荐