一、java连接redis
1.创建一个简单的web工程,在pom里边引入jar包,引入redis依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
2.虚拟机linux要启动redis(先安装,最开始的文章有介绍)
启动:
redis-server redis.conf
3.在Java中新建Redis并书写以下代码:
代码如下:
public class Redis {
public static void main(String[] args) {
Jedis jedis=new Jedis("192.168.253.3",6379);
jedis.set("k1","1");
jedis.setnx("k1","4");
jedis.setnx("k2","2");
jedis.mset("k3","3","k4","4");
List<String> mget = jedis.mget("k1", "k2", "k3", "k4");
System.out.println(mget);
jedis.flushAll();
}
}
二、springboot整合redis
目录结构:
1.在idea里边建立简单的spring Initializr
2.勾选以下内容
3.在pom里边引入jar包
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
4.建立一个简单的entity实体,
代码如下:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private Integer age;
private String sex;
}
5.测试
(1)map测试
配置application.properties
代码:
spring.redis.host=192.168.253.3
设置SpringbootRedisApplicationTests文件
代码如下:
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void contextLoads() {
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
ops.set("k1", "v1");
System.out.println(ops.get("k1"));
Boolean b = ops.setIfAbsent("k5", "123");
System.out.println(b);
Map<String, String> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("name", "张三");
objectObjectHashMap.put("age", "20");
objectObjectHashMap.put("name", "李四");
ops.multiSet(objectObjectHashMap);
System.out.println(stringRedisTemplate.keys("*"));
}
}
测试结果:
(2)Hash测试
设置SpringbootRedisApplicationTests文件
代码如下:
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void contextLoads() {
HashOperations<String, Object, Object> hashs = stringRedisTemplate.opsForHash();
hashs.put("k1","name","张三");
hashs.put("k1","age","20");
Object o=hashs.get("k1","name");
System.out.println(hashs.keys("k1"));
System.out.println(hashs.values("k1"));
System.out.println(o);
System.out.println("k1");
}
}
运行结果:
(3)序列化测试
设置SpringbootRedisApplicationTests2文件
代码如下:
@SpringBootTest
class SpringbootRedisApplicationTests2 {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
ValueOperations valueOperations= redisTemplate.opsForValue();
valueOperations.set("k1",new User("王五",25,"男"));
Object k4=valueOperations.get("k1");
System.out.println(k4);
}
}
(4)hash序列化
设置SpringbootRedisApplicationTests2文件
代码如下:
@SpringBootTest
class SpringbootRedisApplicationTests2 {
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoadsHash() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
HashOperations hash=redisTemplate.opsForHash();
hash.put("k1","name","张三");
hash.put("k2","age","20");
Object k3= hash.values("k1");
Object k4=hash.values("k2");
System.out.println(k3);
System.out.println(k4);
}
}
设置哨兵
1.在linux虚拟机里边开启哨兵和不同的端口
开启命令:
redis-sentinel sentinel.conf
2.利用vim sentinel.conf配置哨兵
3.配置ip、端口号(79:主,80:从,81:从)以及哨兵个数
4.启动不同的端口号(以80为例,其他一样)
代码:
redis-server redis 6380
5.设值主从关系
// An highlighted block
salveof 主ip
80(从)
81(从)
79(主)
6.配置及application.properties文件
7.设置SpringbootRedisApplicationTests3文件
代码如下:
@SpringBootTest
class SpringbootRedisApplicationTests3 {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoadsHash() {
redisTemplate.opsForValue().set("k1","8080");
System.out.println(redisTemplate.opsForValue().get("k1"));
}
}
四、springboot使用redis作为缓存。
目录结构
1.启动哨兵,上边有命令这里不再说
2.所需jar包
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
3.连接哨兵和数据库
在application.properties里写如下代码
spring.datasource.username=数据库名
spring.datasource.password=数据库密码
spring.datasource.url=jdbc:mysql://localhost:3306/demo1?serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.253.3:26379
logging.level.com.zll.springbootcache.mapper=debug
4.写entity里的Student
这里必须有相对应的数据库
@Data
public class Student {
@TableId(type = IdType.AUTO)
private Integer sid;
private String sname;
private String sex;
}
5.mapper->StudentMapper
public interface StudentMapper extends BaseMapper<Student> {
}
6.service->StudentService
@Service
public class StudentService {
@Resource
private StudentMapper studentMapper;
@Autowired
private RedisTemplate redisTemplate;
public Student selectId(Integer id){
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
ValueOperations valueOperations=redisTemplate.opsForValue();
Object o = valueOperations.get("name::selectid::" + id);
if(o!=null){
return (Student) o;
}
Student student=studentMapper.selectById(id);
if(student!=null){
valueOperations.set("name::selectid::"+id,student);
}
return student;
}
}
7.controller->StudentController
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("selectid/{id}")
public Student selectid(@PathVariable Integer id){
return studentService.selectId(id);
}
}
8.SpringbootcacheApplication
@SpringBootApplication
@MapperScan(basePackages = {"com.zll.springbootcache.mapper"})
public class SpringbootcacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootcacheApplication.class, args);
}
}
运行结果:
浏览器:
第一次后台: