SOFABoot 依赖管理

亦凉 2022-01-23 14:29 574阅读 0赞

SOFABoot 依赖管理

在使用 SOFA 中间件之前,需要引入 SOFABoot 依赖管理。

  1. <parent>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>sofaboot-dependencies</artifactId>
  4. <version>${sofa.boot.version}</version>
  5. </parent>

引入 SOFA 中间件

SOFABoot 使用一系列后缀为 -sofa-boot-starter 来标示一个中间件组件,如果想要使用某个中间件,直接添加对应的依赖即可。

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>rpc-sofa-boot-starter</artifactId>
  4. </dependency>





















中间件 starter
SOFARPC rpc-sofa-boot-starter
SOFATracer tracer-sofa-boot-starter
SOFALookout lookout-sofa-boot-starter

引入 SOFABoot 扩展组件

SOFABoot 基于 Spring Boot 提供了健康检查,模块隔离,类隔离等扩展能力。


























扩展组件 starter
健康检查 healthcheck-sofa-boot-starter
模块化隔离 isle-sofa-boot-starter
类隔离 sofa-ark-springboot-starter
测试扩展 test-sofa-boot-starter

引入 SOFA 中间件 ark 插件

SOFABoot 提供了类隔离组件 SOFAArk,借助 SOFAArk 容器,用户可以将依赖冲突的三方包打包成 ark 插件。运行时,ark 插件使用单独的类加载器加载,可以和其他 ark 插件以及业务依赖隔离,解决类冲突问题。

SOFABoot 官方提供了 SOFARPC 和 SOFATracer 的 ark 插件,例如在应用中引入 SOFARPC ark 插件依赖替代 SOFARPC starter,从而隔离应用和 SOFARPC 及其间接依赖。


















Ark插件 plugin
SOFARPC rpc-sofa-boot-plugin
SOFATracer tracer-sofa-boot-plugin

引入 SOFABoot 命名空间

  1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sofa="http://sofastack.io/schema/sofaboot" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd" default-autowire="byName">
  2. </beans>

引入 SOFABoot Infra 依赖

要在 SOFABoot 中直接通过浏览器查看 SOFA 中间件的版本信息,只需要在 Maven 依赖中增加如下的内容即可:

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>infra-sofa-boot-starter</artifactId>
  4. </dependency>

在应用启动成功后,可以在浏览器中输入

  • 2.x: http://localhost:8080/sofaboot/versions
  • 3.x: http://localhost:8080/actuator/versions

引入异步启动加速依赖

引入依赖

SOFABoot 在 v2.6.0 开始提供异步初始化 Spring Bean 能力,引入如下 Starter 即可:

  1. <dependency>
  2. <groupId>com.alipay.sofa</groupId>
  3. <artifactId>runtime-sofa-boot-starter</artifactId>
  4. </dependency>

使用方法

异步初始化 Bean 的原理是开启单独线程负责执行 Bean 的初始化方法(init-method),因此在使用过程中,除了引入上述依赖管理,还需要在 Bean 的 xml 定义中配置 async-init="true" 属性,用于指定是否异步执行该 Bean 的初始化方法,例如:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sofa="http://sofastack.io/schema/sofaboot" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd" default-autowire="byName">
  3. <!-- async init test -->
  4. <bean id="testBean" class="com.alipay.sofa.runtime.beans.TimeWasteBean" init-method="init" async-init="true"/>
  5. </beans>

配置

SOFABoot 异步初始化能力提供两个属性配置,用于指定负责异步执行 Bean 初始化方法(init-method)的线程池大小:

  1. // 线程池基本大小,默认值为 CPU 核数加一
  2. com.alipay.sofa.boot.asyncInitBeanCoreSize
  3. // 线程池中允许的最大线程数大小,默认值为 CPU 核数加一
  4. com.alipay.sofa.boot.asyncInitBeanMaxSize

配置可以通过 VM -D 参数或者 Spring Boot 配置文件 application.yml 设置。

发表评论

表情:
评论列表 (有 0 条评论,574人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Sofaboot模块说明

    SOFABoot支持基于 Spring 上下文隔离的模块化开发能力。 常见的模块化形式: 基于代码组织上的模块化:这是最常见的形式,在开发期,将不同功能的代码放在不同