作者:宝一一0702 | 来源:互联网 | 2023-10-12 20:51
1,安装redis
在centos系统上使用yum install redis
显示以上信息即安装完成
这个时候我们是可以启动redis的,但是其他的服务器是无法访问redis的,目前比较新版本的redis默认都会打开保护模式并且绑定本机ip地址,所以这个时候我们需要修改redis.conf配置文件,将保护模式去除添加自己需要绑定的服务器或者直接全部去除。
不知道redis.conf文件在哪里的可以命令:find / -name redis.conf
将protected-mode yes改为no
完成后我们就可以启动redis了,使用命令:service redis start
显示OK表明已经启动成功了
以上就是我们的准备工作,接下来就是具体的代码实现对redis的访问了
2,引入springboot帮我们自动配置redis的jar包
3,添加redis配置文件
@Configuration
public class RedisConfig {
//配置redis模板
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate redisTemplate = new RedisTemplate<>();
//采用jackson2JsonRedisSerializer包进行序列化
Jackson2JsonRedisSerializer jackson2JsOnRedisSerializer= new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
配置文件如下:
server.port=80
spring.redis.database=1
spring.redis.host=172.16.1.165
spring.redis.port=6379
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.lettuce.pool.max-active=8
这里需要注意的是我们采用的是Jackson2JsonRedisSerializer来序列化我们的javabean,实际上spring自动化帮我们默认配置了一个JdkSerializationRedisSerializer,这两个是有区别的,Jackson2JsonRedisSerializer相比于JdkSerializationRedisSerializer结构更加清晰,存储的字节少,速度快,并且不需要使用Serializable实例化实体类。
4,配置service类和我们需要测试的实体类
public class Student implements Serializable {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class User implements Serializable {
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
@Service
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Resource(name = "redisTemplate")
ValueOperations valueOperations;
@Resource(name = "stringRedisTemplate")
ValueOperations valueStringOperations;
private HashSet students = new HashSet();
private HashSet users = new HashSet();
public void addStudent(Student student){
valueOperations.set(student.getName(),student);
}
public Object getStudent(String name){
Object reslut = valueOperations.get(name);
return reslut;
}
public void addUser(User user){
valueStringOperations.set(user.getUsername(),user.getPassword());
}
public Object getUser(String username){
String reslut = valueStringOperations.get(username);
return reslut;
}
}
5,配置controller类
@RestController
public class TestController {
@Autowired
private RedisService redisService;
@RequestMapping("/putstudent")
public String putStudent(Student student){
redisService.addStudent(student);
return "放入Student成功!!!";
}
@RequestMapping("/getstudentvalue")
public Student getStudentValue(String name){
Object result = redisService.getStudent(name);
return (Student)result;
}
@RequestMapping("/putUser")
public String putUser(User user){
redisService.addUser(user);
return "放入User成功!!!";
}
@RequestMapping("/getuservalue")
public Student getUserValue(String username){
Object result = redisService.getStudent(username);
return (Student)result;
}
}
我们输入http://localhost/putstudent?name=小明&&sex=男 然后再输入http://localhost/putstudent?name=小明
结果如下
我们输入http://localhost/putUser?username=Elvis&&password=123456 然后再输入http://localhost/putstudent?name=小明
结果如下