Redisson应用
Redisson
依赖
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> </dependency>
schema.sql
drop table users if exists; create table users ( id bigint auto_increment, name varchar(255), age int, create_time timestamp, primary key (id) ); insert into users (name, create_time,age) values ('Lili', now(),29); insert into users (name, create_time,age) values ('Fiona', now(),30); insert into users (name, create_time,age) values ('xyz', now(),30); insert into users (name, create_time,age) values ('zbc', now(),16); insert into users (name, create_time,age) values ('Nana', now(),18);
实体类Users.java
package com.example.demo.model; import lombok.*; import org.hibernate.annotations.CreationTimestamp; import javax.persistence.*; import java.util.Date; @Entity @Table(name = "users") @Data @Builder @ToString(callSuper = true) @NoArgsConstructor @AllArgsConstructor public class Users { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private int age; @Column(updatable = false) @CreationTimestamp private Date createTime; }
数据操作接口UsersRepository.java
package com.example.demo.repository; import com.example.demo.model.Users; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository; import java.util.List; @RepositoryRestResource(collectionResourceRel = "user", path = "user") public interface UsersRepository extends JpaRepository<Users,Integer> { List<Users> findByName(@Param("name") String name); List<Users> findByNameContaining(@Param("name") String name); }
配置类
package com.example.demo.configure;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class RedissonConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public RedissonClient getRedisson() {
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port);
return Redisson.create(config);
}
}
控制器
package com.example.demo.controller;
import com.example.demo.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class DemoController {
@Autowired
UsersRepository usersRepository;
@Autowired
private RedissonClient redissonClient;
@RequestMapping("/redis")
public String redis() {
RBucket<String> k1= redissonClient.getBucket("k1");
k1.set("123456");
log.info("k1=",k1.get());
RMap<String,String> map = redissonClient.getMap("users");
usersRepository.findAll().forEach(c -> {
map.put(c.getName(),Integer.toString(c.getAge()));
});
String age = map.get("Fiona");
log.info("Fiona age:"+age);
return "success";
}
}
分布式锁的应用
@RequestMapping("/lock")
public String lock() {
RLock helloLock = redissonClient.getLock("hello");
//加锁
helloLock.lock();
try {
log.info("locked");
Thread.sleep(1000 * 200);
} catch (InterruptedException e){
log.error("error",e.getMessage());
} finally {
//释放锁
helloLock.unlock();
}
log.info("finished");
return "success";
}