1.引入spring-boot-starter-data-redisjar
包,注意spring boot 2.1 沒有對應的spring-boot-starter-redis
版本,更名爲spring-boot-starter-data-redis
。java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.在application中添加redis配置redis
spring: redis: host: 192.168.187.11 port: 6379
3.測試spring
redis客戶端查看,沒有任何keyshell
192.168.187.11:6379> keys * (empty list or set)
@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); } }
使用postman送請求: localhost:8080/string/put?key=hello&value=world
json
localhost:8080/string/get?key=hello
app
在上述使用中,是沒法存儲對象的,存儲對象的話須要使用RedisTemplate而且要使用響應的序列化機制,下面咱們就來測試下:maven
1.引入序列化的jar包,這裏咱們是使用jacksonspring-boot
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency>
2.添加redis配置類post
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); //使用StringRedisSerializer來序列化和反序列化redis的ke redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //開啓事務 redisTemplate.setEnableTransactionSupport(true); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } }
3.添加測試controller測試
@RestController @RequestMapping(value = "/object") public class RedisObjectController { @Autowired private RedisTemplate<String, Object> redisTemplate; @GetMapping("/get/{username}") public Object get(@PathVariable String username) { return redisTemplate.opsForValue().get(username); } @PutMapping("/put") public void put(String username, String nickname) { User user = new User(username, nickname); redisTemplate.opsForValue().set(username, user); } }
4.使用postman測試
使用redis客戶端查看
192.168.187.11:6379> get qianjiangtao "{\"@class\":\"com.lvmama.tony.model.User\",\"username\":\"qianjiangtao\",\"nickname\":\"Tony-J\"}"
咱們都知道spring boot自動化配置中的配置都是經過spring-configuration-metadata.json來約束的,同理redis也是這樣的,咱們配置了spring.redis.host,不妨來找下這個配置項
{ "name": "spring.redis.host", "type": "java.lang.String", "description": "Redis server host.", "sourceType": "org.springframework.boot.autoconfigure.data.redis.RedisProperties", "defaultValue": "localhost" }
從這能看出來redis的配置都是經過RedisProperties這個類來配置的,在這裏面咱們能看到衆多的redis配置及默認配置,咱們能夠從一個入口切入,就拿port切入,來看下port在哪設置的
org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration#getStandaloneConfig
看出在RedisConnectionConfiguration中的getStandaloneConfig中賦值的,那這個方法又是誰調用的呢?繼續找?
從圖中能看出來有兩個地方可能會調用,從類的名字能看出來,spring boot是支持Jedis和Lettuce兩種客戶端來操做redis,那究竟是用哪一個呢? 都看看唄
從圖中截取的源碼中能看出來,我是使用了LettuceConnectionConfiguration
,看註解是我引入了RedisClient
,我何時引入的?因而我就看看maven的依賴
從maven依賴中能看出一些重要的信息:
如何驗證呢?不能瞎說
要想知道很簡單的,在咱們本身寫的RedisConfig
中打下斷點,看看用的RedisConnectionFactory
究竟是不是LettuceConnectionFactory
就能證實了
果真如此!
簡單的流程就是:
1.spring boot經過application配置加載redis配置
2.解析封裝成RedisProperties
3.根據@ConditionalOnClass
判斷使用哪一個Redis客戶端,封裝成LettuceClientConfiguration
並建立LettuceConnectionFactory
4.經過@Bean建立咱們本身的配置類在LettuceConnectionFactory
基礎上添加咱們本身自定義的配置