springboot集成redis 有更新!

  ,
评论 • 156 浏览

    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);
    		//在这里添加处理数据的逻辑
    }
    
    版权声明:本文为博主原创文章,欢迎大家转载,但需保留文章链接。个人博客地址:http://www.wenboblogs.cn
    评论
    validate