简介: web
Redis是一个开源的使用ANSI C语言编写、遵照BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它一般被称为数据结构服务器,由于值(value)能够是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。redis
1.首先在虚拟机上的Centos上安装完成redis,而且完成redis.conf文件的配置(后台启动,密码,IP等等),启动redis-server redis.confspring
2. 查看是否启动:数据库
3.使用win下的RedisDesktopManager链接虚拟机上的redis服务器
4.建立项目:网络
spring-boot-starter-data-redis默认使用的Redis工具是Lettuce,我在这里使用的是Jedis,因此exclusion他
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency>
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
配置文件:application.properties,其中须要配置哪些看本身需求,前面几个是须要的数据结构
spring.redis.database=0 spring.redis.host=192.168.205.100 spring.redis.port=6379 spring.redis.password=123456
#spring.redis.lettuce.pool.max-active=
#spring.redis.lettuce.pool.max-idle=
#spring.redis.lettuce.pool.max-wait=
#spring.redis.lettuce.pool.min-idle=
#spring.redis.lettuce.shutdown-timeout=
#链接池最大链接数 spring.redis.jedis.pool.max-active=8 #链接池中的最大空闲链接 spring.redis.jedis.pool.max-idle=8 #链接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1ms #链接池中的最小空闲链接 spring.redis.jedis.pool.min-idle=0
注意:app
SpringBoot的自动配置类中提供了RedisAutoConfiguration进行Redis配置,源码以下,application.properties中的配置文件将被注入到RedisProperties中,若是开发者没有提供RedisTemplate和StringRedisTemplate这两个类,SpringBoot会默认提供这两个实例。spring-boot
@Configuration @ConditionalOnClass({RedisOperations.class}) @EnableConfigurationProperties({RedisProperties.class}) @Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class}) public class RedisAutoConfiguration { public RedisAutoConfiguration() { } @Bean @ConditionalOnMissingBean( name = {"redisTemplate"} ) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Object> template = new RedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; } }
Controller:工具
@RestController public class BookController { @Autowired RedisTemplate redisTemplate; @Autowired StringRedisTemplate stringRedisTemplate; @GetMapping("/test1") public void test1() { //使用stringRedisTemplate ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue(); ops1.set("name", "三国演义"); String name = ops1.get("name"); System.out.println(name); // 使用redisTemplate ValueOperations ops2 = redisTemplate.opsForValue(); Book b1 = new Book(); b1.setId(1); b1.setName("红楼梦"); b1.setAuthor("曹雪芹"); ops2.set("b1", b1); Book book = (Book) ops2.get("b1"); System.out.println(book); } }
启动,访问http://localhost:8080/test1
控制台表示,已经成功存储而且能够获取到redis中的数据了:
redis中: