如何进行高效的SpringBoot源码阅读

摘要

日常开发中,需要用到各种各样的框架来实现API、系统的构建。作为程序员,除了会使用框架还必须要了解框架工作的原理。这样可以便于我们排查问题,和自定义的扩展。那么如何去学习框架呢。通常我们通过阅读文档、查看源码,然后又很快忘记。始终不能融汇贯通。本文主要基于Spring
Cache扩展为例,介绍如何进行高效的源码阅读。

SpringCache的介绍

为什么以Spring Cache为例呢,原因有两个

  1. Spring框架是web开发最常用的框架,值得开发者去阅读代码,吸收思想

  2. 缓存是企业级应用开发必不可少的,而随着系统的迭代,我们可能会需要用到内存缓存、分布式缓存。那么Spring Cache作为胶水层,能够屏蔽掉我们底层的缓存实现。

一句话解释Spring Cache: 通过注解的方式,利用AOP的思想来解放缓存的管理。

step1 查看文档

首先通过查看官方文档,概括了解Spring Cache https://docs.spring.io/spring-
boot/docs/current/reference/html/boot-features-caching.html

重点两点

  1. 两个接口抽象 Cache, CacheManager,具体的实现都是基于这两个抽象实现。典型的SPI机制,和eat your dog food。当需要提供接口给外部调用,首先自己内部的实现也必须基于同样一套抽象机制

The cache abstraction does not provide an actual store and relies on
abstraction materialized by the org.springframework.cache.Cache and
org.springframework.cache.CacheManager interfaces.

  1. Spring Cache提供了这些缓存的实现,如果没有一种 CacheManage,或者 CacheResolver,会按照指定的顺序去实现

If you have not defined a bean of type CacheManager or a CacheResolver named
cacheResolver (see CachingConfigurer), Spring Boot tries to detect the
following providers (in the indicated order): 1.Generic 2.JCache (JSR-107)
(EhCache 3, Hazelcast, Infinispan, and others) 3.EhCache 2.x 4.Hazelcast
5.Infinispan 6.Couchbase 7.Redis 8.Caffeine 9.Simple

step2 run demo

对Spring Cache有了一个大概的了解后,我们首先使用起来,跑个demo。

定义一个用户查询方法

      1. @Component

  2. public class CacheSample {

  3.     @Cacheable(cacheNames = "users")

  4.     public Map getUser(final Collection userIds) {

  5.         System.out.println("not cache");

  6.         final Map mapUser = new HashMap<>();

  7.         userIds.forEach(userId -> {

  8.             mapUser.put(userId, User.builder().userId(userId).name("name").build());

  9.         });

  10.         return mapUser;

  11.     }

配置一个CacheManager

      1. @Configuration

  2. public class CacheConfig {

  3.     @Primary

  4.     @Bean(name = { "cacheManager" })

  5.     public CacheManager getCache() {

  6.       return new ConcurrentMapCacheManager("users");

  7.     }

API调用

      1. @RestController

  2. @RequestMapping("/api/cache")

  3. public class CacheController {

  4.     @Autowired

  5.     private CacheSample cacheSample;

  6.     @GetMapping("/user/v1/1")

  7.     public List getUser() {

  8.         return cacheSample.getUser(Arrays.asList(1L,2L)).values().stream().collect(Collectors.toList());

  9.     }

  10.     }

step3 debug 查看实现

demo跑起来后,就是debug看看代码如何实现的了。因为直接看源代码的,没有调用关系,看起来会一头雾水。通过debug能够使你更快了解一个实现。

如何进行高效的SpringBoot源码阅读_第1张图片通过debug我们会发现主要控制逻辑是在切面CacheAspectSupport
会先根据cache key找缓存数据,没有的话put进去。

      1. // Check if we have a cached item matching the conditions

  2. Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));

  3.   4. // Collect puts from any @Cacheable miss, if no cached item is found

  5. List cachePutRequests = new LinkedList<>();

  6. if (cacheHit == null) {

  7.    collectPutRequests(contexts.get(CacheableOperation.class),

  8.          CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);

  9. }

step4 实现扩展

知道如何使用Spring Cache后,我们需要进一步思考,就是如何扩展。那么带着问题出发。比如Spring
Cache不支持批量key的缓存,像上文我们举的例子,我们希望缓存的key是userId,而不是CollectionuserIds。以userId为key,这样的缓存命中率更高,存储的成本更小。

      1.   @Cacheable(cacheNames = "users")

  2.     public Map getUser(final Collection userIds) {

所以我们要实现对Spring Cache进行扩展。step3中我们已经大致了解了Spring
Cache的实现。那么实现这个扩展的功能就是拆分CollectionuserIds,缓存命中的从缓存中获取,没有命中的,调用源方法。

      1. @Aspect

  2. @Component

  3. public class CacheExtenionAspect {

  4.   5.     @Autowired

  6.     private CacheExtensionManage cacheExtensionManage;

  7.   8.     /**

  9.      * 返回的结果中缓存命中的从缓存中获取,没有命中的调用原来的方法获取

  10.      * @param joinPoint

  11.      * @return

  12.      */

  13.     @Around("@annotation(org.springframework.cache.annotation.Cacheable)")

  14.     @SuppressWarnings("unchecked")

  15.     public Object aroundCache(final ProceedingJoinPoint joinPoint) {

  16.   17.         // 修改掉Collection值,cacheResult需要重新构造一个

  18.         args[0] = cacheResult.getMiss();

  19.         try {

  20.             final Map notHit = CollectionUtils.isEmpty(cacheResult.getMiss()) ? null

  21.                     : (Map) (method.invoke(target, args));

  22.             final Map hits = cacheResult.getHit();

  23.             if (Objects.isNull(notHit)) {

  24.                 return hits;

  25.             }

  26.             // 设置缓存

  27.             cacheResult.getCache().putAll(notHit);

  28.             hits.putAll(notHit);

  29.             return hits;

  30.     }

  31. }

然后扩展 Cache, CacheManage重写Cache的查找缓存方法,返回新的CacheResult

      1.   public static Object lookup(final CacheExtension cache, final Object key) {

  2.         if (key instanceof Collection) {

  3.             final Collection originalKeys = ((Collection) key);

  4.             if (originalKeys == null || originalKeys.isEmpty()) {

  5.                 return CacheResult.builder().cache(cache).miss(

  6.                         Collections.emptySet())

  7.                         .build();

  8.             }

  9.             final List keys = originalKeys.stream()

  10.                     .filter(Objects::nonNull).collect(Collectors.toList());

  11.             final Map hits = cache.getAll(keys);

  12.             final Set miss = new HashSet(keys);

  13.             miss.removeAll(hits.keySet());

  14.             return CacheResult.builder().cache(cache).hit(hits).miss(miss).build();

  15.         }

  16.         return null;

  17.     }

CacheResult就是新的缓存结果格式

      1.     @Builder

  2.     @Setter

  3.     @Getter

  4.     static class CacheResult {

  5.         final CacheExtension cache;

  6.         // 命中的缓存结果

  7.         final Map hit;

  8.         // 需要重新调用源方法的keys

  9.         private Set miss;

  10.     }

然后扩展CacheManager,没什么重写,就是自定义一种manager类型 为缓存指定新的CacheManager

      1.   @Primary

  2.   @Bean

  3.   public CacheManager getExtensionCache() {

  4.         return new CacheExtensionManage("users2");

  5.   }

总结

本文主要介绍一种源码学习方法,纯属抛砖引玉,如果你有好的方法,欢迎分享。

你可能感兴趣的:(如何进行高效的SpringBoot源码阅读)