sofaboot的启动原理解析

SpringBoot的原理

我们先找到这张springboot的原理图
在这里插入图片描述
详细可以参见

类隔离能力

https://zhuanlan.zhihu.com/p/37665305
本质上还是OSGi。使用更大的Ark容器,将含有spring-boot的模块作为一个ark-biz。,

具体的启动流程

入口类和spring一摸一样

@SpringBootApplication(exclude = {
    
    DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class SOFABootApplication {
    
    

    private static final Logger LOGGER = LoggerFactory.getLogger(SOFABootApplication.class);

    public static void main(String[] args) throws Throwable {
    
    
        try {
    
    
            SpringApplication.run(SOFABootApplication.class, args);
            LOGGER.info("SOFABoot App Start!!!");
        }catch (Throwable e){
    
    
            LOGGER.error("SOFABoot App Start Fail!!! More logs can be found on 1) logs/sofa-runtime/common-error.log"
                   + " 2) logs/spring/spring.log 3) logs/mvc/common-error.log 4) logs/health-check/common-error.log", e);
            throw e;
        }
    }

}

需要引入下面的starter

        <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>isle-alipay-sofa-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>rpc-alipay-sofa-boot-starter</artifactId>
        </dependency>
                <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>log4j2-alipay-sofa-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alipay.sofa</groupId>
            <artifactId>runtime-alipay-sofa-boot-starter</artifactId>
        </dependency>

一样会传递依赖自己的autoconfigure

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alipay.sofa.boot.actuator.autoconfigure.version.VersionEndpointAutoConfiguration,\
com.alipay.sofa.boot.actuator.autoconfigure.health.SofaBootHealthCheckAutoConfiguration

我们随便找一个

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.alipay.sofa.boot.actuator.autoconfigure.health;

import com.alipay.sofa.boot.actuator.health.ComponentHealthIndicator;
import com.alipay.sofa.boot.actuator.health.MultiApplicationHealthIndicator;
import com.alipay.sofa.boot.actuator.health.ReadinessEndpointWebExtension;
import com.alipay.sofa.boot.actuator.health.SofaBootHealthIndicator;
import com.alipay.sofa.boot.actuator.health.SofaBootReadinessEndpoint;
import com.alipay.sofa.boot.actuator.health.SofaModuleHealthIndicator;
import com.alipay.sofa.healthcheck.AfterReadinessCheckCallbackProcessor;
import com.alipay.sofa.healthcheck.HealthCheckerProcessor;
import com.alipay.sofa.healthcheck.HealthIndicatorProcessor;
import com.alipay.sofa.healthcheck.ReadinessCheckListener;
import com.alipay.sofa.healthcheck.core.HealthChecker;
import com.alipay.sofa.healthcheck.impl.ComponentHealthChecker;
import com.alipay.sofa.healthcheck.impl.ModuleHealthChecker;
import com.alipay.sofa.healthcheck.impl.SofaRuntimeHealthChecker;
import com.alipay.sofa.isle.stage.ModelCreatingStage;
import com.alipay.sofa.runtime.spi.component.SofaRuntimeContext;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorProperties;
import org.springframework.boot.actuate.health.HealthStatusHttpMapper;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass({
    
    HealthChecker.class})
public class SofaBootHealthCheckAutoConfiguration {
    
    
    public SofaBootHealthCheckAutoConfiguration() {
    
    
    }

    @Bean
    public ReadinessCheckListener readinessCheckListener() {
    
    
        return new ReadinessCheckListener();
    }

    @Bean
    public HealthCheckerProcessor healthCheckerProcessor() {
    
    
        return new HealthCheckerProcessor();
    }

    @Bean
    public HealthIndicatorProcessor healthIndicatorProcessor() {
    
    
        return new HealthIndicatorProcessor();
    }

    @Bean
    public AfterReadinessCheckCallbackProcessor afterReadinessCheckCallbackProcessor() {
    
    
        return new AfterReadinessCheckCallbackProcessor();
    }

    @Bean
    public SofaBootHealthIndicator sofaBootHealthIndicator() {
    
    
        return new SofaBootHealthIndicator();
    }

    @Bean
    public SofaRuntimeHealthChecker defaultRuntimeHealthChecker(SofaRuntimeContext sofaRuntimeContext) {
    
    
        return new SofaRuntimeHealthChecker(sofaRuntimeContext);
    }

    @Bean
    @ConditionalOnClass(
        name = {
    
    "com.alipay.sofa.ark.spi.model.Biz"}
    )
    public MultiApplicationHealthIndicator multiApplicationHealthIndicator() {
    
    
        return new MultiApplicationHealthIndicator();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint(
        endpoint = SofaBootReadinessEndpoint.class
    )
    public SofaBootReadinessEndpoint sofaBootReadinessCheckEndpoint() {
    
    
        return new SofaBootReadinessEndpoint();
    }

    @Bean
    public ComponentHealthIndicator sofaComponentHealthIndicator() {
    
    
        return new ComponentHealthIndicator();
    }

    @Bean
    public ComponentHealthChecker sofaComponentHealthChecker(SofaRuntimeContext sofaRuntimeContext) {
    
    
        return new ComponentHealthChecker(sofaRuntimeContext);
    }

    @Configuration
    @AutoConfigureBefore({
    
    HealthEndpointAutoConfiguration.class})
    @ConditionalOnClass({
    
    HealthChecker.class})
    public static class ReadinessCheckExtensionConfiguration {
    
    
        public ReadinessCheckExtensionConfiguration() {
    
    
        }

        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnEnabledEndpoint(
            endpoint = ReadinessEndpointWebExtension.class
        )
        public ReadinessEndpointWebExtension readinessEndpointWebExtension() {
    
    
            return new ReadinessEndpointWebExtension();
        }

        @Bean
        @ConditionalOnMissingBean
        public HealthStatusHttpMapper createHealthStatusHttpMapper(HealthIndicatorProperties healthIndicatorProperties) {
    
    
            HealthStatusHttpMapper statusHttpMapper = new HealthStatusHttpMapper();
            if (healthIndicatorProperties.getHttpMapping() != null) {
    
    
                statusHttpMapper.addStatusMapping(healthIndicatorProperties.getHttpMapping());
            }

            statusHttpMapper.addStatusMapping(Status.UNKNOWN, 500);
            return statusHttpMapper;
        }
    }

    @Configuration
    @ConditionalOnClass({
    
    HealthChecker.class, ModelCreatingStage.class})
    public static class SofaModuleHealthIndicatorConfiguration {
    
    
        public SofaModuleHealthIndicatorConfiguration() {
    
    
        }

        @Bean
        public SofaModuleHealthIndicator sofaModuleHealthIndicator() {
    
    
            return new SofaModuleHealthIndicator();
        }

        @Bean
        public ModuleHealthChecker sofaModuleHealthChecker() {
    
    
            return new ModuleHealthChecker();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/define_us/article/details/111355876