前面我們已經(jīng)在Linux安裝好了redis,接下來我們就可以在項目里配置redis了
首先我們先進入linux里redis安裝目錄,編輯redis.conf文件,找到bind 127.0.0.1并注釋掉(該步驟如果不需要可省略)

image.png
若注釋掉那么redis的ip就是你服務器的ip

image.png
然后將daemonize屬性改為yes(這前面安裝的時候就已經(jīng)改了)
我們然后去配置一下redis的密碼(也可以不配置,一開始我也沒配置導致報錯,查找問題給出四個解決方法,其中一個就是讓我們去配置密碼)
找到requirepass foobared注釋,在下面配置你自己需要設置的密碼:requirepass 你的密碼,改完重啟下redis

image.png
這樣linux里redis的配置差不多結束了,接下來去項目里配置redis
首先添加redis依賴
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
接著去配置application.properties
#redis配置
spring.redis.database=0
spring.redis.password=123456
spring.redis.port=6379
spring.redis.host=redis的ip
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=10000ms
spring.redis.lettuce.shutdown-timeout=100ms
接下來我們寫程序來測試一下
@RestController
@EnableCaching
public class TextRedisController {
@Autowired
StringRedisTemplate redisTemplate;
@RequestMapping("/addRedis")
public String addRedis(){
redisTemplate.opsForValue().set("redis","123");
return "OK";
}
@RequestMapping("/getRedis")
public String getRedis(){
String text = redisTemplate.opsForValue().get("redis");
return text;
}
}
啟動去postman測試一下

image.png
OK,然后調用下getRedis方法看下能不能獲取參數(shù)

image.png
OK!獲取到了我們存進去的參數(shù),這樣redis就算配置完成了