1.Redis在Unbuntu14开启,

进入安装的src目录:

1.修改redis.conf,因为redis默认是受保护模式。

protected-mode yes   (改为no)

bind 127.0.0.1    (注释掉,因为默认只有本机能访问)

requiredpass "password"  (新增一行,作为权限验证密码)

接下来开启redis,(这里一定要指定conf文件,默认不会使用你修改的redis.conf文件,导致修改内容无效)

nohup ./redis-server ../redis.conf  &

2.用./redis-cli 打开cli

127.0.0.1:6379>auth password

OK

127.0.0.1:6379>ping

PONG

验证通过

2.搭建SpringBoot-Redis环境

1.新建项目,pom.xml文件:

   <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

  2.application.properties配置

 核心只需配:

spring.redis.port=6379
spring.redis.host=xxx
spring.redis.password=password 编写controller方法
 @Autowired
private StringRedisTemplate stringRedisTemplate; @GetMapping("set")
public void setString() {
stringRedisTemplate.opsForValue().set("lisi", "美女");
} @GetMapping("show")
public String getString() {
return stringRedisTemplate.opsForValue().get("lisi");
}
启动
@SpringBootApplication
public class SpringDataRedis01Application { public static void main(String[] args) {
SpringApplication.run(SpringDataRedis01Application.class, args);
}
}

 

StringRedisTemplate是继承自RedisTemplate,提供的一种对Key,Value为String类型的一种便利的操作对象
官方文档(Since it’s quite common for the keys and values stored in Redis to be java.lang.String, the Redis modules provides two extensions to RedisConnection
 and RedisTemplate, respectively the StringRedisConnection (and its DefaultStringRedisConnection implementation) and StringRedisTemplate as a convenient one-stop
solution for intensive String operations. In addition to being bound to String keys, the template and the connection use the StringRedisSerializerunderneath
which means the stored keys and values are human readable (assuming the same encoding is used both in Redis and your code))
public class StringRedisTemplate extends RedisTemplate<String, String> {

    /**
* Constructs a new <code>StringRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
* and {@link #afterPropertiesSet()} still need to be called.
*/
public StringRedisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
setKeySerializer(stringSerializer);
setValueSerializer(stringSerializer);
setHashKeySerializer(stringSerializer);
setHashValueSerializer(stringSerializer);
}
。。。。。。
}

RedisProperties.java官方文档

 # REDIS (RedisProperties)
spring.redis.cluster.max-redirects= # Maximum number of redirects to follow when executing commands across the cluster.
spring.redis.cluster.nodes= # Comma-separated list of "host:port" pairs to bootstrap from.
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:[email protected]:6379
spring.redis.host=localhost # Redis server host.
spring.redis.jedis.pool.max-active=8 # Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.jedis.pool.max-idle=8 # Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.jedis.pool.max-wait=-1ms # Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.jedis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.lettuce.pool.max-active=8 # Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.lettuce.pool.max-idle=8 # Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.lettuce.pool.max-wait=-1ms # Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.lettuce.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.lettuce.shutdown-timeout=100ms # Shutdown timeout.
spring.redis.password= # Login password of the redis server.
spring.redis.port=6379 # Redis server port.
spring.redis.sentinel.master= # Name of the Redis server.
spring.redis.sentinel.nodes= # Comma-separated list of "host:port" pairs.
spring.redis.ssl=false # Whether to enable SSL support.
spring.redis.timeout= # Connection timeout.
 

参考:https://docs.spring.io/spring-data/redis/docs/1.8.10.RELEASE/reference/html/#redis:connectors:jedis

05-29 01:06