Spring 应用合并之路(二):峰回路转,柳暗花明 | 京东云技术团队 ...

不到断气不罢休  金牌会员 | 2024-3-21 04:55:34 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 890|帖子 890|积分 2670

书接上文,前面在 Spring 应用合并之路(一):摸石头过河 介绍了几种不成功的经验,下面继续折腾…
 
四、仓库合并,独立容器

在经历了上面的尝试,在同事为啥不搞两个独立的容器提醒下,决定抛开 Spring Boot 内置的父子容器方案,完全自己实现父子容器。
如何加载 web 项目?

现在的难题只有一个:如何加载 web 项目?加载完成后,如何持续持有 web 项目?经过思考后,可以创建一个 boot 项目的 Spring Bean,在该 Bean 中加载并持有 web 项目的容器。由于 Spring Bean 默认是单例的,并且会伴随 Spring 容器长期存活,就可以保证 web 容器持久存活。结合 Spring 扩展点概览及实践 中介绍的 Spring 扩展点,有两个地方可以利用:
 
1.可以利用 ApplicationContextAware 获取 boot 容器的 ApplicationContext 实例,这样就可以实现自己实现的父子容器;2.可以利用 ApplicationListener 获取 ContextRefreshedEvent 事件,该事件表示容器已经完成初始化,可以提供服务。在监听到该事件后,来进行 web 容器的加载。 
思路确定后,代码实现就很简单了:
 
  1. package com.diguage.demo.boot.config;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.ApplicationContextAware;
  7. import org.springframework.context.ApplicationEvent;
  8. import org.springframework.context.ApplicationListener;
  9. import org.springframework.context.event.ContextRefreshedEvent;
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;
  11. import org.springframework.stereotype.Component;
  12. /**
  13. * @author D瓜哥 · https://www.diguage.com
  14. */
  15. @Component
  16. public class WebLoaderListener implements ApplicationContextAware,
  17.         ApplicationListener<ApplicationEvent> {
  18.     private static final Logger logger = LoggerFactory.getLogger(WebLoaderListener.class);
  19.     /**
  20.      * 父容器,加载 boot 项目
  21.      */
  22.     private static ApplicationContext parentContext;
  23.     /**
  24.      * 子容器,加载 web 项目
  25.      */
  26.     private static ApplicationContext childContext;
  27.     @Override
  28.     public void setApplicationContext(ApplicationContext ctx) throws BeansException {
  29.         WebLoaderListener.parentContext = ctx;
  30.     }
  31.     @Override
  32.     public void onApplicationEvent(ApplicationEvent event) {
  33.         logger.info("receive application event: {}", event);
  34.         if (event instanceof ContextRefreshedEvent) {
  35.             WebLoaderListener.childContext = new ClassPathXmlApplicationContext(
  36.                     new String[]{"classpath:web/spring-cfg.xml"},
  37.                     WebLoaderListener.parentContext);
  38.         }
  39.     }
  40. }
复制代码
容器重复加载的问题

这次自己实现的父子容器,如同设想的那样,没有同名 Bean 的检查,省去了很多麻烦。但是,观察日志,会发现 com.diguage.demo.boot.config.WebLoaderListener#onApplicationEvent 方法被两次执行,也就是监听到了两次 ContextRefreshedEvent 事件,导致 web 容器会被加载两次。由于项目的 RPC 服务不能重复注册,第二次加载抛出异常,导致启动失败。
最初,怀疑是 web 容器,加载了 WebLoaderListener,但是跟踪代码,没有发现 childContext 容器中有 WebLoaderListener 的相关 Bean。
昨天做了个小实验,又调试了一下 Spring 的源代码,发现了其中的奥秘。直接贴代码吧:
SPRING/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
 
  1. /**
  2. * Publish the given event to all listeners.
  3. * <p>This is the internal delegate that all other {@code publishEvent}
  4. * methods refer to. It is not meant to be called directly but rather serves
  5. * as a propagation mechanism between application contexts in a hierarchy,
  6. * potentially overridden in subclasses for a custom propagation arrangement.
  7. * @param event the event to publish (may be an {@link ApplicationEvent}
  8. * or a payload object to be turned into a {@link PayloadApplicationEvent})
  9. * @param typeHint the resolved event type, if known.
  10. * The implementation of this method also tolerates a payload type hint for
  11. * a payload object to be turned into a {@link PayloadApplicationEvent}.
  12. * However, the recommended way is to construct an actual event object via
  13. * {@link PayloadApplicationEvent#PayloadApplicationEvent(Object, Object, ResolvableType)}
  14. * instead for such scenarios.
  15. * @since 4.2
  16. * @see ApplicationEventMulticaster#multicastEvent(ApplicationEvent, ResolvableType)
  17. */
  18. protected void publishEvent(Object event, @Nullable ResolvableType typeHint) {
  19.     Assert.notNull(event, "Event must not be null");
  20.     ResolvableType eventType = null;
  21.     // Decorate event as an ApplicationEvent if necessary
  22.     ApplicationEvent applicationEvent;
  23.     if (event instanceof ApplicationEvent applEvent) {
  24.         applicationEvent = applEvent;
  25.         eventType = typeHint;
  26.     }
  27.     else {
  28.         ResolvableType payloadType = null;
  29.         if (typeHint ApplicationEvent.class.isAssignableFrom(typeHint.toClass())) {
  30.             eventType = typeHint;
  31.         }
  32.         else {
  33.             payloadType = typeHint;
  34.         }
  35.         applicationEvent (this, event, payloadType);
  36.     }
  37.     // Determine event type only once (for multicast and parent publish)
  38.     if (eventType == null) {
  39.         eventType = ResolvableType.forInstance(applicationEvent);
  40.         if (typeHint == null) {
  41.             typeHint = eventType;
  42.         }
  43.     }
  44.     // Multicast right now if possible - or lazily once the multicaster is initialized
  45.     if (this.earlyApplicationEvents != null) {
  46.         this.earlyApplicationEvents.add(applicationEvent);
  47.     }
  48.     else if (this.applicationEventMulticaster != null) {
  49.         this.applicationEventMulticaster.multicastEvent(applicationEvent, eventType);
  50.     }
  51.     // Publish event via parent context as well...
  52.     // 如果有父容器,则也将事件发布给父容器。
  53.     if (this.parent != null) {
  54.         if (this.parent instanceof AbstractApplicationContext abstractApplicationContext) {
  55.             abstractApplicationContext.publishEvent(event, typeHint);
  56.         }
  57.         else {
  58.             this.parent.publishEvent(event);
  59.         }
  60.     }
  61. }
复制代码
在 publishEvent 方法的最后,如果父容器不为 null 的情况下,则也会向父容器广播容器的相关事件。
看到这里就清楚了,不是 web 容器持有了 WebLoaderListener 这个 Bean,而是 web 容器主动向父容器广播了 ContextRefreshedEvent 事件。
容器销毁

除了上述问题,还有一个问题需要思考:如何销毁 web 容器?如果不能销毁容器,会有一些意想不到的问题。比如,注册中心的 RPC 提供方不能及时销毁等等。
这里的解决方案也比较简单:同样基于事件监听,Spring 容器销毁会有 ContextClosedEvent 事件,在 WebLoaderListener 中监听该事件,然后调用 AbstractApplicationContext#close 方法就可以完成 Spring 容器的销毁工作。
父子容器加载及销毁

结合上面的所有论述,完整的代码如下:
 
  1. package com.diguage.demo.boot.config;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.BeansException;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.ApplicationContextAware;
  7. import org.springframework.context.ApplicationEvent;
  8. import org.springframework.context.ApplicationListener;
  9. import org.springframework.context.event.ContextClosedEvent;
  10. import org.springframework.context.event.ContextRefreshedEvent;
  11. import org.springframework.context.support.AbstractApplicationContext;
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;
  13. import org.springframework.stereotype.Component;
  14. import java.util.Objects;
  15. /**
  16. * 基于事件监听的 web 项目加载器
  17. *
  18. * @author D瓜哥 · https://www.diguage.com
  19. */
  20. @Component
  21. public class WebLoaderListener implements ApplicationContextAware,
  22.         ApplicationListener<ApplicationEvent> {
  23.     private static final Logger logger = LoggerFactory.getLogger(WebLoaderListener.class);
  24.     /**
  25.      * 父容器,加载 boot 项目
  26.      */
  27.     private static ApplicationContext parentContext;
  28.     /**
  29.      * 子容器,加载 web 项目
  30.      */
  31.     private static ClassPathXmlApplicationContext childContext;
  32.     @Override
  33.     public void setApplicationContext(ApplicationContext ctx) throws BeansException {
  34.         WebLoaderListener.parentContext = ctx;
  35.     }
  36.     /**
  37.      * 事件监听
  38.      *
  39.      * @author D瓜哥 · https://www.diguage.com
  40.      */
  41.     @Override
  42.     public void onApplicationEvent(ApplicationEvent event) {
  43.         logger.info("receive application event: {}", event);
  44.         if (event instanceof ContextRefreshedEvent refreshedEvent) {
  45.             ApplicationContext context = refreshedEvent.getApplicationContext();
  46.             if (Objects.equals(WebLoaderListener.parentContext, context)) {
  47.                 // 加载 web 容器
  48.                 WebLoaderListener.childContext = new ClassPathXmlApplicationContext(
  49.                         new String[]{"classpath:web/spring-cfg.xml"},
  50.                         WebLoaderListener.parentContext);
  51.             }
  52.         } else if (event instanceof ContextClosedEvent) {
  53.             // 处理容器销毁事件
  54.             if (Objects.nonNull(WebLoaderListener.childContext)) {
  55.                 synchronized (WebLoaderListener.class) {
  56.                     if (Objects.nonNull(WebLoaderListener.childContext)) {
  57.                         AbstractApplicationContext ctx = WebLoaderListener.childContext;
  58.                         WebLoaderListener.childContext = null;
  59.                         ctx.close();
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }
复制代码
 

五、参考资料

1.Spring 扩展点概览及实践 - "地瓜哥"博客网2.Context Hierarchy with the Spring Boot Fluent Builder API3.How to revert initial git commit?作者:京东科技 李君
来源:京东云开发者社区 转载请注明来源
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

不到断气不罢休

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表