此次教程演示安装的是Window版的Redis,
Linux安装Redis可以参考我的这篇博文:Redis的安装和客户端使用注意事项
关于Java连接Redis操作方面可以参考我的这篇博文:Java连接Redis之redis的增删改查
window安装Redis非常简单,就是下载+解压,启动服务端和客户端即可。
我是参考菜鸟教程的:http://www.runoob.com/redis/redis-install.html
包括,大家想熟悉和练练手,大家可以参考菜鸟教程,非常基础,也十分有利于提高学习信心和积极性的。
至于为什么使用Redis?
简单的说,如果我有上亿的数据,这些数据基本变动不大,如果我通过数据库加索引查询,是需要一定的时间的,如果我通过Redis将其缓存,那么通常是先缓存再数据库,意思是,如果缓存有,就不必查数据库了,要知道将数据缓存到内存中,通常cpu是直接跟内存进行交互,cpu+内存,这样会很大提升查询效率和速度的。而磁盘的话,就相对于慢的非常多。
这也是Redis一个应用场景之一。
就不多说基础方面的了,来一波示例讲解:
一、导入依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> modelVersion>4.0.0</> groupId>org.springframeworkartifactId>gs-messaging-redisversion>0.1.0parent> >org.springframework.boot>spring-boot-starter-parent>1.5.8.RELEASEpropertiesjava.version>1.8java.versiondependenciesdependency> >spring-boot-starter>spring-boot-starter-data-redis> buildpluginsplugin> >spring-boot-maven-pluginrepositoriesrepositoryid>spring-releasesname>Spring Releasesurl>https://repo.spring.io/libs-releasepluginRepositoriespluginRepository> project>
二、编写消息接收类
package hello; import java.util.concurrent.CountDownLatch; org.slf4j.Logger; org.slf4j.LoggerFactory; org.springframework.beans.factory.annotation.Autowired; public class Receiver { private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.); private CountDownLatch latch; @Autowired public Receiver(CountDownLatch latch) { this.latch = latch; } void receiveMessage(String message) { LOGGER.info("Received <" + message + ">"); latch.countDown(); } }
三、编写配置文件
# Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u4E3A0\uFF09 spring.redis.database=0 # Redis\u670D\u52A1\u5668\u5730\u5740 spring.redis.host=localhost # Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3 spring.redis.port=6379 # Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09 spring.redis.password= # \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 spring.redis.pool.max-active=8 # \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09 spring.redis.pool.max-wait=-1 # \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5 spring.redis.pool.max-idle=8 # \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5 spring.redis.pool.min-idle=0 # \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09 spring.redis.timeout=0
四、编写启动类
org.springframework.boot.SpringApplication; org.springframework.boot.autoconfigure.SpringBootApplication; org.springframework.context.ApplicationContext; org.springframework.context.annotation.Bean; org.springframework.data.redis.connection.RedisConnectionFactory; org.springframework.data.redis.core.StringRedisTemplate; org.springframework.data.redis.listener.PatternTopic; org.springframework.data.redis.listener.RedisMessageListenerContainer; org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @SpringBootApplication Application { final Logger LOGGER = LoggerFactory.getLogger(Application.); @Bean RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); container.addMessageListener(listenerAdapter,new PatternTopic("chat")); return container; } @Bean MessageListenerAdapter listenerAdapter(Receiver receiver) { return new MessageListenerAdapter(receiver,"receiveMessage"); } @Bean Receiver receiver(CountDownLatch latch) { Receiver(latch); } @Bean CountDownLatch latch() { new CountDownLatch(1); } @Bean StringRedisTemplate template(RedisConnectionFactory connectionFactory) { StringRedisTemplate(connectionFactory); } void main(String[] args) throws InterruptedException { ApplicationContext ctx = SpringApplication.run(Application.,args); StringRedisTemplate template = ctx.getBean(StringRedisTemplate.); CountDownLatch latch = ctx.getBean(CountDownLatch.); LOGGER.info("Sending message..."); template.convertAndSend("chat","Hello from Redis!"); latch.await(); System.exit(0); } }
成功标志:
@H_44_301@