1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、设置 redis url
spring:
redis:
host: 192.168.1.211
port: 6379
3、添加 service
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@Service
public class RedisMessageSubscriber implements MessageListener {
private final Logger log = LoggerFactory.getLogger(RedisMessageSubscriber.class);
@Autowired
private SimpMessagingTemplate messagingTemplate;
@Override
public void onMessage(@NotNull Message message, byte[] bytes) {
log.debug("redis is get message -> {},bytes -> {}", message, new String(bytes));
JSONObject jsonObject = JSON.parseObject(new String(message.getBody()));
log.debug("constants indexes are {}", Constants.CHANNELS);
//在这里添加处理数据的逻辑
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于