1.11微服务--SpringBoot整合Redis
一、Redis简介
Redis 是一个开源的、先进的key-value 存储系统,可用于构建高性能的存储系统。Redis支持数据结构有字符串、哈希、列表、集合、排序集合、位图、超文本等。
NoSQL (Not Only SQL ) 泛指非关系型的数据库。Redis 是一种NoSQL,Redis 具有很多的优点:例如读写非常快速,支持丰富的数据类型,所有的操作都是原子的。
Redis安装:
1)Mac 下安装
通过brew 命令安装,安装后启动Redis 服务器和客户端,命令如下:
安装:brew install redis
启动服务器:redis-server
启动客户端:redis-cli
2)Windows 下安装
Redis 官方没有提供Windows 版本,不过微软维护了一个版本,下载地址为https://github.com/MSOpenTech/redis/releases
下载.msi 版本, 一直单击“下一步”完成安装即可。
二、在Spring Boot 中使用Redis
新建一个Spring Boot 工程,在工程的POM文件中加入Redis 的起步依赖、spring-boot-starter-data-redis ,代码如下:POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
在工程的配置文件application.properties 中加上Redis的数据源配置,例如host、post、数据库配置信息等。
如果Redis 设置了密码,则要提供密码,选择序号为1的数据库,配置Pool的相关配置,配置代码同如下:application.properties
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0
数据操作层的RedisDao类通过@Repository 注解来注入Spring IoC 容器中,该类是通过RedisTemplate 来访问Redis的。
通过注入StringRedisTemplate的Bean来对Redis数据库中的字符串类型的数据进行操作,写了两个方法:包括向Redis中设置String类型的数据和从Redis中读取String类型的数据,代码如下:
src/main/com/example/dao/RedisDao.java
package com.example.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
import java.util.concurrent.TimeUnit;
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate template;
public void setKey(String key,String value){
ValueOperations<String, String> ops = template.opsForValue();
ops.set(key,value,1, TimeUnit.MINUTES);//1分钟过期
}
public String getValue(String key){
ValueOperations<String, String> ops = this.template.opsForValue();
return ops.get(key);
}
}
在SpringBootTest的测试类中注入RedisDao,首先通过RedisDao向Redis设置两组字符串值, 即name为example, age为18的两组字符串,然后分别通过RedisDao 从Redis中读取这两个值,并打印出来,代码如下:
src/test/com/example/SpringbootRedisApplicationTests.java
package com.example;
import com.example.dao.RedisDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootRedisApplicationTests {
public static Logger logger= LoggerFactory.getLogger(SpringbootRedisApplicationTests.class);
@Test
public void contextLoads() {
}
@Autowired
RedisDao redisDao;
@Test
public void testRedis(){
redisDao.setKey("name","example");
redisDao.setKey("age","18");
logger.info(redisDao.getValue("name"));
logger.info(redisDao.getValue("age"));
}
}
启动单元测试,控制台打印了example和18的两个字符串值。
可见,通过RedisDao 首先向Redis 数据库中写入了两个数据,然后又读取了这两个数据。
————————————————————————————————————————————