日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
SpringBoot如何快速集成Redis?

 Spring Boot 如何快速集成 Redis?沒錯,棧長本文教你,讓大家少走彎路!

汝城網站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網頁設計、網站建設、微信開發(fā)、APP開發(fā)、成都響應式網站建設等網站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)建站從2013年開始到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創(chuàng)新互聯(lián)建站。

添加依賴

使用像 Redis 這類的 NoSQL 數據庫就必須要依賴 spring-data-redis 這樣的能力包,開箱即用,Spring Boot 中都封裝好了:

引入spring-boot-starter-data-redis:

 
 
 
 
  1.  
  2.  org.springframework.boot 
  3.  spring-boot-starter-data-redis 

Spring Boot 基礎知識就不介紹了,不熟悉的可以關注公眾號Java技術棧,在后臺回復:boot,可以閱讀我寫的歷史實戰(zhàn)教程。

它主要包含了下面四個依賴:

  •  spring-boot-dependencies
  •  spring-boot-starter
  •  spring-data-redis
  •  lettuce-core

添加 Redis 連接配置

Redis 自動配置支持配置單機、集群、哨兵,來看下 RedisProperties 的參數類圖吧:

本文以單機為示例,我們在 application.yml 配置文件中添加 Redis 連接配置,:

 
 
 
 
  1. spring: 
  2.   redis: 
  3.     host: 192.168.8.88 
  4.     port: 6379 
  5.     password: redis2020 
  6.     database: 1

也可以將參數配置在 Spring Cloud Config Server 配置中心中。

Redis 自動配置

添加完依賴和連接配置參數之后,Redis 就能自動配置,參考 Redis 的自動配置類:

 
 
 
 
  1. org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

源碼:

 
 
 
 
  1. @Configuration(proxyBeanMethods = false) 
  2. @ConditionalOnClass(RedisOperations.class) 
  3. @EnableConfigurationProperties(RedisProperties.class) 
  4. @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) 
  5. public class RedisAutoConfiguration { 
  6.     ... 
  7. }

通過看源碼,Redis內置兩種客戶端的自動配置:

1)Lettuce(默認):

 
 
 
 
  1. org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration

2)Jedis:

 
 
 
 
  1. org.springframework.boot.autoconfigure.data.redis.JedisConnectionConfiguration

為什么默認Lettuce,其實文章之前的四個依賴也看出來了,請看默認依賴:

自動配置提供了兩種操作模板:

1)RedisTemplate

key-value 都為 Object 對象,并且默認用的 JDK 的序列化/反序列化器:

 
 
 
 
  1. org.springframework.data.redis.serializer.JdkSerializationRedisSerializer

使用這個序列化器,key 和 value 都需要實現 java.io.Serializable 接口。

2)StringRedisTemplate

key-value 都為 String 對象,默認用的 String UTF-8 格式化的序列化/反序列化器:

 
 
 
 
  1. org.springframework.data.redis.serializer.StringRedisSerializer

上面提到了兩種序列化器,另外還有兩種 JSON 的序列化器值得學習一下,下面配置會用到。

  •  Jackson2JsonRedisSerializer
  •  GenericJackson2JsonRedisSerializer

使用方式上,兩種都可以序列化、反序列化 JSON 數據,Jackson2JsonRedisSerializer 效率高,但 GenericJackson2JsonRedisSerializer 更為通用,不需要指定泛型類型。

核心配置

除了自動配置之外,下面是 Redis 的核心配置,主要是自定義了 RedisTemplate 使用 JSON 序列化器。

另外就是,把幾個數據類型的操作類進行了 Bean 池化處理。

 
 
 
 
  1. @Configuration 
  2. public class RedisConfig { 
  3.     @Bean 
  4.     public RedisTemplate redisTemplate(RedisConnectionFactory factory) { 
  5.         RedisTemplate template = new RedisTemplate<>(); 
  6.         template.setConnectionFactory(factory);
  7.         StringRedisSerializer stringSerializer = new StringRedisSerializer(); 
  8.         RedisSerializer jacksonSerializer = getJacksonSerializer(); 
  9.         template.setKeySerializer(stringSerializer); 
  10.         template.setValueSerializer(jacksonSerializer); 
  11.         template.setHashKeySerializer(stringSerializer); 
  12.         template.setHashValueSerializer(jacksonSerializer); 
  13.         template.setEnableTransactionSupport(true); 
  14.         template.afterPropertiesSet();
  15.         return template; 
  16.     } 
  17.     private RedisSerializer getJacksonSerializer() { 
  18.         ObjectMapper om = new ObjectMapper(); 
  19.         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 
  20.         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 
  21.         return new GenericJackson2JsonRedisSerializer(om); 
  22.     } 
  23.     @Bean 
  24.     public HashOperations hashOperations(RedisTemplate redisTemplate) { 
  25.         return redisTemplate.opsForHash(); 
  26.     } 
  27.     @Bean 
  28.     public ValueOperations valueOperations(RedisTemplate redisTemplate) { 
  29.         return redisTemplate.opsForValue(); 
  30.     } 
  31.     @Bean 
  32.     public ListOperations listOperations(RedisTemplate redisTemplate) { 
  33.         return redisTemplate.opsForList(); 
  34.     } 
  35.     @Bean 
  36.     public SetOperations setOperations(RedisTemplate redisTemplate) { 
  37.         return redisTemplate.opsForSet(); 
  38.     } 
  39.     @Bean 
  40.     public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { 
  41.         return redisTemplate.opsForZSet(); 
  42.     } 
  43. }

如果你只想用默認的 JDK 序列化器,那 RedisTemplate 相關配置就不是必須的。

緩存實戰(zhàn)

下面寫了一個示例,用來緩存并讀取緩存中一個類對象。

 
 
 
 
  1. @GetMapping("/redis/set") 
  2. public String set(@RequestParam("name") String name) { 
  3.     User user = new User(); 
  4.     user.setId(RandomUtils.nextInt()); 
  5.     user.setName(name); 
  6.     user.setBirthday(new Date()); 
  7.     List list = new ArrayList<>(); 
  8.     list.add("sing"); 
  9.     list.add("run"); 
  10.     user.setInteresting(list); 
  11.     Map map = new HashMap<>(); 
  12.     map.put("hasHouse", "yes"); 
  13.     map.put("hasCar", "no"); 
  14.     map.put("hasKid", "no"); 
  15.     user.setOthers(map); 
  16.     redisOptService.set(name, user, 30000); 
  17.     User userValue = (User) redisOptService.get(name); 
  18.     return userValue.toString(); 
  19. }

測試:

http://localhost:8080/redis/set?name=zhangsan

返回:

 
 
 
 
  1. User(id=62386235, name=zhangsan, birthday=Tue Jun 23 18:04:55 CST 2020, interesting=[sing, run], others={hasHouse=yes, hasKid=no, hasCar=no})

Redis中的值:

 
 
 
 
  1. 192.168.8.88:6379> get zhangsan "["cn.javastack.springboot.redis.pojo.User",{"id":62386235,"name":"zhangsan","birthday":["java.util.Date",1592906695750],"interesting":["java.util.ArrayList",["sing","run"]],"others":["java.util.HashMap",{"hasHouse":"yes","hasKid":"no","hasCar":"no"}]}]"

好啦,Spring Boot 快速集成 Redis 就到這了,下篇帶來 Spring Boot 如何快速集成 Redis 分布式鎖,關注公眾號Java技術棧,第一時間推送,敬請期待……

本文完整源代碼也將和下篇一起上傳到Github,歡迎大家 Star 關注學習。

https://github.com/javastacks/spring-boot-best-practice


當前文章:SpringBoot如何快速集成Redis?
文章路徑:http://m.5511xx.com/article/dhhocgi.html