集成方式
集成方式使用redisson-spring-boot-starter
,下面开始配置步骤
配置过程
- 依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-</artifactId>
<version>3.16.4</version>
</dependency>
- application.yml配置
spring:
redis:
host: 127.0.0.1
port: 6379
password:
- 配置类
- 方式一: 使用RedisAutoConfiguration配置
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Configuration;
/**
* @Auther: hongdf
*/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedissonConfig {
}
- 方式二: 手动配置Bean
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
/**
* @Auther: hongdf
* @Description: Redisson实例配置
*/
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
@Bean
public Redisson getRedisson(){
Config config = new Config();
String address = String.format("redis://%s:%s", host, port);
SingleServerConfig singleServerConfig = config.useSingleServer().
setAddress(address);
if (StringUtils.hasText(password)){
singleServerConfig.setPassword(password);
}
// return Redisson.create(config);
// 强转为Redisson返回
return (Redisson)Redisson.create(config);
}
}
- 使用
@Autowired
private Redisson redisson;