sofa-runtime插件

简介

sofa-runtime插件主要解决的是Biz模块之间的通信问题
sofa-runtime整体框图

流程

SofaRuntimeActivator插件启动器

Biz模块事件监听处理

SofaEventHandler对应的代码如下:

public class SofaEventHandler implements EventHandler {
    @Override
    public void handleEvent(ArkEvent event) {
        if (Constants.BIZ_EVENT_TOPIC_UNINSTALL.equals(event.getTopic())) {
            doUninstallBiz((BizEvent) event);
        } else if (Constants.BIZ_EVENT_TOPIC_HEALTH_CHECK.equals(event.getTopic())) {
            doHealthCheck((BizEvent) event);
        }
    }

    private void doUninstallBiz(BizEvent event) {
        SofaRuntimeProperties.unRegisterProperties(event.getBiz().getBizClassLoader());
        SofaRuntimeManager sofaRuntimeManager = getSofaRuntimeManager(event.getBiz());
        SofaFramework.unRegisterSofaRuntimeManager(sofaRuntimeManager);
        sofaRuntimeManager.shutdown();
    }

    private void doHealthCheck(BizEvent event) {
        SofaRuntimeManager sofaRuntimeManager = getSofaRuntimeManager(event.getBiz());
        if (!sofaRuntimeManager.isHealthCheckPassed()) {
            throw new RuntimeException("Health check failed.");
        }
    }

    private SofaRuntimeManager getSofaRuntimeManager(Biz biz) {
        for (SofaRuntimeManager sofaRuntimeManager : SofaFramework.getRuntimeSet()) {
            if (sofaRuntimeManager.getAppClassLoader().equals(biz.getBizClassLoader())) {
                return sofaRuntimeManager;
            }
        }
        throw new RuntimeException("No SofaRuntimeManager Found!");
    }

    @Override
    public int getPriority() {
        return DEFAULT_PRECEDENCE;
    }
}

doUninstallBiz方法调用 sofaRuntimeManager.shutdown()方法进行应用程序关闭,实际调用的是StandardSofaRuntimeManager.shutdown方法,代码如下:

public void shutdown() throws ServiceRuntimeException {
        try {
            for (ApplicationShutdownCallback callback : applicationShutdownCallbacks) {
                callback.shutdown();
            }
            if (componentManager != null) {
                componentManager.shutdown();
            }
            clear();
        } catch (Throwable throwable) {
            throw new ServiceRuntimeException(throwable);
        }
    }

在applicationShutdownCallbacks中注册了CloseApplicationContextCallBack这个关闭回调,这个CloseApplicationContextCallBack代码如下:

public class CloseApplicationContextCallBack implements ApplicationShutdownCallback,
                                            ApplicationContextAware {

    private ApplicationContext cxt;

    @Override
    public void shutdown() {
        if (cxt instanceof AbstractApplicationContext) {
           // 调用spring application context进行关闭
            ((AbstractApplicationContext) cxt).close();
        } else {
            throw new RuntimeException(String.format("%s is not instanceof %s, can not be closed.",
                cxt.getClass(), AbstractApplicationContext.class));
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        cxt = applicationContext;
    }
}

JVM service发布应用

类DynamicJvmServiceProxyFinder实现了JVM内部之间的服务发布和引用,通过服务组件ServiceComponent来进行服务的发布和引用

参考

  1. http://www.sofastack.tech/sofa-boot/docs/Module-Service

你可能感兴趣的:(sofa-runtime插件)