SOFABoot 依赖管理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jeikerxiao/article/details/90763845

SOFABoot 依赖管理

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

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

引入 SOFA 中间件

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

<dependency>
    <groupId>com.alipay.sofa</groupId>
    <artifactId>rpc-sofa-boot-starter</artifactId>
</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 命名空间

<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">
</beans>

引入 SOFABoot Infra 依赖

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

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

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

引入异步启动加速依赖

引入依赖

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

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

使用方法

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

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <!-- async init  test -->
    <bean id="testBean" class="com.alipay.sofa.runtime.beans.TimeWasteBean" init-method="init" async-init="true"/>
</beans>

配置

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

// 线程池基本大小,默认值为 CPU 核数加一
com.alipay.sofa.boot.asyncInitBeanCoreSize

// 线程池中允许的最大线程数大小,默认值为 CPU 核数加一
com.alipay.sofa.boot.asyncInitBeanMaxSize

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

猜你喜欢

转载自blog.csdn.net/jeikerxiao/article/details/90763845