安装文章https://blog.csdn.net/yeluo_vinager/article/details/103680059
问题:
F:\soft\Redis-x64-3.2.100>redis-server.exe --service-start --Service-name RedisServer1
[14752] 21 Jan 14:30:59.337 # HandleServiceCommands: system error caught. error code=1060, message = OpenService failed: unknown error
解决
没有启动
redis-server.exe --service-install redis.windows.conf --service-name redis1
redis-server.exe --service-start --Service-name redis;启动
springboot 使用
依赖
<!--引入redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.4.0</version>
</dependency>
Application.yml配置redis
spring:
redis:
host: 127.0.0.1 #配置redis的主机地址,需要修改成自己的
database: 50
port: 6379
password: #设置自己的密码
jedis:
pool:
max-active: 50 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool 的状态为exhausted(耗尽)
max-idle: 20 # 连接池中的最大空闲连接,默认值也是8。
min-idle: 5 # 连接池中的最小空闲连接,默认值也是0。
# max-wait: 5 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接 抛出JedisConnectionException
添加Api
package com.example.client.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class RedisService {
//redis提供了RedisTemplate 和 StringRedisTemplate模板
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private StringRedisTemplate RedisTemplate;
/*
新建一个 RedisService,注入 StringRedisTemplate,使用 stringRedisTemplate.opsForValue()
可以获取 ValueOperations<String, String> 对象,通过该对象即可读写 redis 数据库了。
*/
public void setRediskv(String key,String value){
ValueOperations<String, String> forValue = RedisTemplate.opsForValue();
forValue.set(key,value);
}
public String getRediskv(String key){
String s = stringRedisTemplate.opsForValue().get(key);
return s;
}
}
测试
package com.example.client.controller;
import com.example.client.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Api(tags = "测试controller") //接口描述
@RestController
@RequestMapping("Test")
public class TestController {
@Resource
private RedisService redisService;
@ApiOperation(value = "查询子节点" , notes="查询树叶子节点(文件夹)")
@GetMapping("/getMessage")
public String getMessage() {
return "hello, I am client.";
}
//测试对象类型
@GetMapping("setRedisObj")
public String setRedisObj(){
redisService.setRediskv("csdn","果vinegar");
return "success";
}
@GetMapping("getRedisObj")
public String getRedisObj(){
return redisService.getRediskv("csdn");
}
}
先调用http://localhost:8081/Test/setRedisObj
然后调用http://localhost:8081/Test/getRedisObj