Redis是什么,我這里就不多說了火邓。
先在pom.xml添加依賴包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
我們先看看 這些包都依賴那些內(nèi)容
我們需要做一個(gè)自動(dòng)序列化的Bean
/**
* Redis 初始化配置
*/
@Configuration
@EnableCaching // 開啟注解
public class RedisConfig {
@Bean
public RedisTemplate<String, Serializable>
redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
//使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//開啟事務(wù)
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
SpringBoot 開發(fā)效率其實(shí)和PHP已經(jīng)差不多了
我們暫時(shí)只用 opsForValue
堆巧,這是Redis常用的類型勤揩。我們可以理距為字符串類型吧寞奸,如果你要用Redis 的list類型的話 就是 opsForList
@RestController
@RequestMapping(value = "/string")
public class RedisStringController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@PutMapping(value = "/put")
public void put(String key, @RequestParam(required = false, defaultValue = "default") String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@GetMapping(value = "/get")
public Object get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
}
spring boot整合redis自動(dòng)化配置原理分析
我們都知道spring boot自動(dòng)化配置中的配置都是通過spring-configuration-metadata.json來約束的,同理redis也是這樣的溶诞,我們配置了spring.redis.host,不妨來找下這個(gè)配置項(xiàng)
{
"name": "spring.redis.host",
"type": "java.lang.String",
"description": "Redis server host.",
"sourceType": "org.springframework.boot.autoconfigure.data.redis.RedisProperties",
"defaultValue": "localhost"
}
從這能看出來redis的配置都是通過RedisProperties這個(gè)類來配置的,在這里面我們能看到眾多的redis配置及默認(rèn)配置决侈,我們可以從一個(gè)入口切入螺垢,就拿port切入,來看下port在哪設(shè)置的
org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration#getStandaloneConfig
看出在RedisConnectionConfiguration中的getStandaloneConfig中賦值的,那這個(gè)方法又是誰調(diào)用的呢?繼續(xù)找?
從圖中能看出來有兩個(gè)地方可能會(huì)調(diào)用,從類的名字能看出來赖歌,spring boot是支持Jedis和Lettuce兩種客戶端來操作redis枉圃,那到底是用哪個(gè)呢? 都看看唄
從圖中截取的源碼中能看出來,我是使用了LettuceConnectionConfiguration
庐冯,看注解是我引入了RedisClient
孽亲,我什么時(shí)候引入的?于是我就看看maven的依賴
從maven依賴中能看出一些重要的信息:
- 1.spring-boot-starter-data-redis中其實(shí)用的是spring-data-redis展父,其實(shí)是包裝了下
- 2.依賴了lettuce-core返劲,原來是從這里引入的,怪不得
如何驗(yàn)證呢犯祠?不能瞎說
? 要想知道很簡(jiǎn)單的旭等,在我們自己寫的RedisConfig
中打下斷點(diǎn),看看用的RedisConnectionFactory
到底是不是LettuceConnectionFactory
就能證明了
果然如此衡载!
簡(jiǎn)單的流程就是:
1.spring boot通過application配置加載redis配置
2.解析封裝成RedisProperties
3.根據(jù)@ConditionalOnClass
判斷使用哪個(gè)Redis客戶端搔耕,封裝成LettuceClientConfiguration
并創(chuàng)建LettuceConnectionFactory
4.通過@Bean創(chuàng)建我們自己的配置類在LettuceConnectionFactory
基礎(chǔ)上添加我們自己自定義的配置