[开源]SpringBoot增强框架,提供健康检查、日志空间、类隔离能力

开源系统流程管理
2026-06-28 02:40 0 次浏览 • 0 次下载 0KB
[开源]SpringBoot增强框架,提供健康检查、日志空间、类隔离能力

一、开源项目简介

SOFABoot 是蚂蚁集团开源的基于 Spring Boot 的研发框架,它在 Spring Boot 的基础上,提供了诸如 Readiness Check,上下文隔离,类隔离,日志空间隔离等等能力。在增强了 Spring Boot 的同时,SOFABoot 提供了让用户可以在 Spring Boot 中非常方便地使用 SOFA 中间件的能力。

二、开源协议

使用Apache-2.0开源协议

三、界面展示

四、功能概述

1、背景

Spring Boot 是一个非常优秀的开源框架,可以非常方便地就构建出一个基于 Spring 的应用程序,但是在使用过程中,还是会遇到一些问题:

  • Spring Boot 提供了一个基础的健康检查的能力,中间件和应用都可以扩展来实现自己的健康检查逻辑。但是 Spring Boot 的健康检查只有 Liveness Check 的能力,缺少 Readiness Check 的能力,这样会有比较致命的问题。当一个微服务应用启动的时候,必须要先保证启动后应用是健康的,才可以将上游的流量放进来(来自于 RPC,网关,定时任务等等流量),否则就可能会导致一定时间内大量的错误发生。
  • Spring Boot 虽然通过依赖管理(Dependency Management)的方式最大程度的保证了 Spring Boot 管理的 JAR 包之间的兼容性,但是不可避免的,当引入一些其他的 JAR 包的时候,还是可能会遇到冲突,而且很多时候这种冲突解决起来并不是这么容易,一个例子是当冲突的包是序列化相关的类库时,比如说 Hessian,如果应用中的一个组件需要使用 Hessian 3,而另一个则必须要使用 Hessian 4,由于 Hessian 3 和 Hessian 4 之间的不兼容性,并且序列化还涉及到微服务中的上下游服务,要把 Hessian 统一到一个版本绝非易事。
  • 在超大规模微服务运维的场景下,运维能力的平台化是一定要解决的问题,而监控又是其中非常主要的一个点,针对于日志监控这种情况,Spring Boot 并没有提供任何解决方案。大部分的开源组件,具体要打印哪些日志,打印到什么路径,什么文件下面,都是由应用的使用者来决定,这样会导致每一个应用的日志配置都各式各样,每一个应用都需要去监控系统中配置自己应用的日志监控,导致关键的监控的实施成本特别高。
  • 在企业级应用场景,模块化开发是解决多团队沟通成本的有效解决方案,每个业务团队专注于开发自己的应用模块,每个模块自包含,便于开发及自测,减少团队间的沟通成本。但是 Spring Boot 默认不支持模块化开发,所有 Bean 共用一个 Spring 上下文,在多团队开发时,如果不同团队定义了相同 BeanId,运行时将出现 BeanId 冲突错误。

为了解决以上的问题,又因为 SOFA 中间件中的各个组件本身就需要集成 Spring Boot,所以蚂蚁集团基于 Spring Boot 开发并开源了 SOFABoot,来解决以上的问题,也方便使用者在 Spring Boot 中方便地去使用 SOFA 中间件。

2、功能简介

为了解决 Spring Boot 在实施大规模微服务架构时候的问题,SOFABoot 提供了以下的能力:

2.1 增强 Spring Boot 的健康检查能力

针对 Spring Boot 缺少 Readiness Check 能力的情况,SOFABoot 增加了 Spring Boot 现有的健康检查的能力,提供了 Readiness Check 的能力。利用 Readiness Check 的能力,SOFA 中间件中的各个组件只有在 Readiness Check 通过之后,才将流量引入到应用的实例中,比如 RPC,只有在 Readiness Check 通过之后,才会向服务注册中心注册,后面来自上游应用的流量才会进入。

除了中间件可以利用 Readiness Check 的事件来控制流量的进入之外,PAAS 系统也可以通过访问 http://localhost:8080/health/readiness 来获取应用的 Readiness Check 的状况,用来控制例如负载均衡设备等等流量的进入。

2.2 提供类隔离的能力

为了解决 Spring Boot 下的类依赖冲突的问题,SOFABoot 基于 SOFAArk 提供了 Spring Boot 上的类隔离的能力,在一个 SOFABoot 的系统中,只要引入 SOFAArk 相关的依赖,就可以将 SOFA 中间件相关的类和应用相关的类的 ClassLoader 进行隔离,防止出现类冲突。当然,用户也可以基于 SOFAArk,将其他的中间件、第三方的依赖和应用的类进行隔离。

2.3 日志空间隔离能力

为了统一大规模微服务场景下的中间件日志的打印,SOFABoot 提供了日志空间隔离的能力给 SOFA 中间件,SOFA 中间件中的各个组件采用日志空间隔离的能力之后,自动就会将本身的日志和应用的普通日志隔离开来,并且打印的日志的路径也是相对固定,非常方便进行统一地监控。

2.4 SOFA 中间件的集成管理

基于 Spring Boot 的自动配置能力,SOFABoot 提供了 SOFA 中间件统一易用的编程接口以及 Spring Boot 的 Starter,方便在 Spring Boot 环境下使用 SOFA 中间件,SOFA 中间件中的各个组件都是独立可插拔的,节约开发时间,和后期维护的成本。

2.5 模块化开发

SOFABoot 从 2.4.0 版本开始支持基于 Spring 上下文隔离的模块化开发能力,每个 SOFABoot 模块使用独立的 Spring 上下文,避免不同 SOFABoot 模块间的 BeanId 冲突,有效降低企业级多模块开发时团队间的沟通成本。

五、技术选型

示例

SOFABoot 的示例工程 sofaboot-samples 包含以下 demo 项目:

  • SOFABoot 示例工程
  • SOFABoot 示例工程(包含类隔离能力)
  • SOFABoot 示例工程(包含模块化开发能力)
  • SOFABoot 示例工程(使用 SOFARPC)
  • SOFABoot 示例工程(使用定时任务)

在本文档中,将创建一个 Spring Boot 的工程,引入 SOFABoot 基础依赖,并且引入 SOFABoot 的健康检查扩展能力,演示如何快速上手 SOFABoot。

环境准备

要使用 SOFABoot,需要先准备好基础环境,SOFABoot 依赖以下环境: - JDK7 或 JDK8 - 需要采用 Apache Maven 3.2.5 或者以上的版本来编译

创建工程

SOFABoot 是直接构建在 Spring Boot 之上,因此可以使用 Spring Boot 的工程生成工具 来生成,在本文档中,我们需要添加一个 Web 的依赖,以便最后在浏览器中查看效果。

引入 SOFABoot

在创建好一个 Spring Boot 的工程之后,接下来就需要引入 SOFABoot 的依赖,首先,需要将上文中生成的 Spring Boot 工程的 zip 包解压后,修改 maven 项目的配置文件 pom.xml,将

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>${spring.boot.version}</version>
    <relativePath/> 
</parent>

替换为:

<parent>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofaboot-dependencies</artifactId>
    <version>${sofa.boot.version}</version>
</parent>

这里的 ${sofa.boot.version} 指定具体的 SOFABoot 版本,参考发布历史。 然后,添加 SOFABoot 健康检查扩展能力的依赖及 Web 依赖(方便查看健康检查结果):

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>healthcheck-sofa-boot-starter</artifactId>
</dependency>

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

最后,在工程的 application.properties 文件下添加 SOFABoot 工程常用的参数配置,其中 spring.application.name 是必需的参数,用于标示当前应用的名称;logging path 用于指定日志的输出目录。

# Application Name
spring.application.name=SOFABoot Demo
# logging path
logging.path=./logs

运行

可以将工程导入到 IDE 中运行生成的工程里面中的 main 方法(一般上在 XXXApplication 这个类中)启动应用,也可以直接在该工程的根目录下运行 mvn spring-boot:run,将会在控制台中看到启动打印的日志:

2018-04-05 21:36:26.572  INFO ---- Initializing ProtocolHandler ["http-nio-8080"]
2018-04-05 21:36:26.587  INFO ---- Starting ProtocolHandler [http-nio-8080]
2018-04-05 21:36:26.608  INFO ---- Using a shared selector for servlet write/read
2018-04-05 21:36:26.659  INFO ---- Tomcat started on port(s): 8080 (http)

可以通过在浏览器中输入 http://localhost:8080/sofaboot/versions 来查看当前 SOFABoot 中使用 Maven 插件生成的版本信息汇总,结果类似如下:

[
  {
    GroupId: "com.alipay.sofa",
    Doc-Url: "https://github.com/sofastack/sofa-boot",
    ArtifactId: "infra-sofa-boot-starter",
    Built-Time: "2018-04-05T20:55:26+0800",
    Commit-Time: "2018-04-05T20:54:26+0800",
    Commit-Id: "049bf890bb468aafe6a3e07b77df45c831076996",
    Version: "2.4.4"
  }
]

注: 在 SOFABoot 3.x 中调整了 endpoint 路径,sofaboot/versions 更改为 actuator/versions

可以通过在浏览器中输入 http://localhost:8080/health/readiness 查看应用 Readiness Check 的状况,类似如下:

{
  status: "UP",
  sofaBootComponentHealthCheckInfo: {
    status: "UP"
  },
  springContextHealthCheckInfo: {
    status: "UP"
  },
  DiskSpace: {
    status: "UP",
    total: 250140434432,
    free: 22845308928,
    threshold: 10485760
  }
}

注: 在 SOFABoot 3.x 中调整了 endpoint 路径,health/readiness 更改为 actuator/readiness

status: "UP" 表示应用 Readiness Check 健康的。可以通过在浏览器中输入 http://localhost:8080/health 来查看应用的运行时健康状态(可能会随着时间发生变化)。

注: 在 SOFABOOT 3.X 中调整了 endpoint 路径,/health 更改为 /actuator/health

查看日志

在上面的 application.properties 里面,我们配置的日志打印目录是 ./logs 即当前应用的根目录(我们可以根据自己的实践需要配置),在当前工程的根目录下可以看到类似如下结构的日志文件:

./logs
├── health-check
│   ├── sofaboot-common-default.log
│   └── sofaboot-common-error.log
├── infra
│   ├── common-default.log
│   └── common-error.log
└── spring.log

如果应用启动失败或者健康检查返回失败,可以通过相应的日志文件找到错误的原因,有些需要关注 common-error.log 日志。

测试

我们知道,SpringBoot 官方提供了和 JUnit4 集成的 SpringRunner, 用于集成测试用例的编写; 在 SOFABoot 中,依然可以使用原生的 SpringRunner, 但是推荐使用 SOFABoot 自带的 SofaBootRunner 以及 SofaJUnit4Runner 编写集成测试和单元测试;应用需要额外引入如下 Starter:

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>test-sofa-boot-starter</artifactId>
</dependency>

需要注意的是,如果需要使用 SOFABoot 的类隔离的能力,则必须需要引入上述的依赖,并且使用 SofaBootRunner 和 SofaJUnit4Runner 来测试。

模块化开发

这部分将演示如何在 SOFABoot 环境下进行模块化开发,您可以直接在工程下找到本文档的示例代码,项目一共包括四个模块:

.
│
├── service-facade 
│ 
├── service-provider
│ 
├── service-consumer
│ 
└── sofa-boot-run

各个模块的作用如下:

  • service-facade: 演示 JVM 服务发布与引用的 API 包;
  • service-provider: 演示 XML 方式、Annotation 方式、API 方式发布 JVM 服务;
  • service-consumer: 演示 XML 方式、Annotation 方式、API 方式引用 JVM 服务;
  • sofa-boot-run: 启动包含 SOFABoot 模块的 SOFA Boot 应用。

建议在学习该 Demo 之前,参考模块隔离文档

定义服务 API

service-facade 模块包含用于演示 JVM 服务发布与引用的 API :

public interface SampleJvmService {
    String message();
}

发布 JVM 服务

service-provider 是一个 SOFABoot 模块,用于演示 XML 方式、Annotation 方式、API 方式发布 JVM 服务。

定义 SOFABoot 模块

为 service-provider 模块增加 sofa-module.properties 文件,将其定义为 SOFABoot 模块:

Module-Name=com.alipay.sofa.service-provider

XML 方式发布服务

实现 SampleJvmService 接口:

public class SampleJvmServiceImpl implements SampleJvmService {
    private String message;

    @Override
    public String message() {
        System.out.println(message);
        return message;
    }

    // getters and setters
}

增加 META-INF/spring/service-provide.xml 文件,将 SampleJvmServiceImpl 发布为 JVM 服务:

<bean id="sampleJvmService" class="com.alipay.sofa.isle.sample.SampleJvmServiceImpl">
    <property name="message" value="Hello, jvm service xml implementation."/>
</bean>

<sofa:service ref="sampleJvmService" interface="com.alipay.sofa.isle.sample.SampleJvmService">
    <sofa:binding.jvm/>
</sofa:service>

Annotation 方式发布服务

实现 SampleJvmService 接口并增加 @SofaService 注解:

@SofaService(uniqueId = "annotationImpl")
public class SampleJvmServiceAnnotationImpl implements SampleJvmService {
    @Override
    public String message() {
        String message = "Hello, jvm service annotation implementation.";
        System.out.println(message);
        return message;
    }
}

为了区分 XML 方式发布的 JVM 服务,注解上需要增加 uniqueId 属性。

将 SampleJvmServiceAnnotationImpl 配置成一个 Spring Bean,保证 @SofaService 注解生效:

<bean id="sampleJvmServiceAnnotation" class="com.alipay.sofa.isle.sample.SampleJvmServiceAnnotationImpl"/>

API 方式发布服务

增加 PublishServiceWithClient 类,演示 API 方式发布服务:

public class PublishServiceWithClient implements ClientFactoryAware {
    private ClientFactory clientFactory;

    public void init() {
        ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
        ServiceParam serviceParam = new ServiceParam();
        serviceParam.setInstance(new SampleJvmServiceImpl("Hello, jvm service service client implementation."));
        serviceParam.setInterfaceType(SampleJvmService.class);
        serviceParam.setUniqueId("serviceClientImpl");
        serviceClient.service(serviceParam);
    }

    @Override
    public void setClientFactory(ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }
}

将 PublishServiceWithClient 配置为 Spring Bean,并设置 init-method ,使PublishServiceWithClient 在 Spring 刷新时发布服务:

<bean id="publishServiceWithClient" class="com.alipay.sofa.isle.sample.PublishServiceWithClient" init-method="init"/>

引用 JVM 服务

service-consumer 是一个 SOFABoot 模块,用于演示 XML 方式、Annotation 方式、API 方式引用 JVM 服务。

定义 SOFABoot 模块

为 service-consumer 模块增加 sofa-module.properties 文件,将其定义为 SOFABoot 模块:

Module-Name=com.alipay.sofa.service-consumer
Require-Module=com.alipay.sofa.service-provider

在 sofa-module.properties 文件中需要指定 Require-Module,保证 service-provider 模块在 service-consumer 模块之前刷新。

XML 方式引用服务

增加 META-INF/spring/service-consumer.xml 文件,引用 service-provider 模块发布的服务:

<sofa:reference id="sampleJvmService" interface="com.alipay.sofa.isle.sample.SampleJvmService">
    <sofa:binding.jvm/>
</sofa:service>

Annotation 方式引用服务

定义 JvmServiceConsumer 类,并在其 sampleJvmServiceAnnotationImpl 属性上增加 @SofaReference 注解:

public class JvmServiceConsumer implements ClientFactoryAware {
    @SofaReference(uniqueId = "annotationImpl")
    private SampleJvmService sampleJvmServiceAnnotationImpl;
}

将 JvmServiceConsumer 配置成一个 Spring Bean,保证 @SofaReference 注解生效:

<bean id="consumer" class="com.alipay.sofa.isle.sample.JvmServiceConsumer" init-method="init" />

API 方式引用服务

JvmServiceConsumer 实现 ClientFactoryAware 接口,并在其 init 方法中引用 JVM 服务:

public class JvmServiceConsumer implements ClientFactoryAware {
    private ClientFactory    clientFactory;

    public void init() {
        ReferenceClient referenceClient = clientFactory.getClient(ReferenceClient.class);
        ReferenceParam<SampleJvmService> referenceParam = new ReferenceParam<>();
        referenceParam.setInterfaceType(SampleJvmService.class);
        referenceParam.setUniqueId("serviceClientImpl");
        SampleJvmService sampleJvmServiceClientImpl = referenceClient.reference(referenceParam);
        sampleJvmServiceClientImpl.message();
    }

    @Override
    public void setClientFactory(ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }
}

启动 SOFABoot 应用

将模块 parent 配置为 SOFABoot:

<parent>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>sofaboot-dependencies</artifactId>
    <version>2.4.4</version>
</parent>

为模块增加 isle-sofa-boot-starter 及 service-provider 、 service-consumer 依赖:

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>isle-sofa-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>service-provider</artifactId>
</dependency>
<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>service-consumer</artifactId>
</dependency>

启动 ApplicationRun 类,控制台将看到以下输出:

Hello, jvm service xml implementation.
Hello, jvm service annotation implementation.
Hello, jvm service service client implementation.

编写测试用例

SOFABoot 模块化测试方法与 Spring Boot 测试方法一致,只需在测试用例上增加 @SpringBootTest 注解及 @RunWith(SpringRunner.class) 注解即可。在测试用例中,还可以使用 @SofaReference 注解,对 SOFABoot 模块发布的服务进行测试:

@SpringBootTest
@RunWith(SpringRunner.class)
public class SofaBootWithModulesTest {
    @SofaReference
    private SampleJvmService sampleJvmService;

    @SofaReference(uniqueId = "annotationImpl")
    private SampleJvmService sampleJvmServiceAnnotationImpl;

    @SofaReference(uniqueId = "serviceClientImpl")
    private SampleJvmService sampleJvmServiceClientImpl;

    @Test
    public void test() {
        Assert.assertEquals("Hello, jvm service xml implementation.", sampleJvmService.message());
        Assert.assertEquals("Hello, jvm service annotation implementation.",
            sampleJvmServiceAnnotationImpl.message());
        Assert.assertEquals("Hello, jvm service service client implementation.",
            sampleJvmServiceClientImpl.message());
    }
}