传统我的项目
传统我的项目中有时会Klass service层间接调用Student service层的Bean去实现某些逻辑,当我的项目变得越来越大时,这种层与层之间的依赖关系变得十分复杂,不利于开发。多个团队同时开发时,本团队只心愿关怀本人负责的模块。这种状况下,就须要将模块于模块之间互相独立,也就是对模块做隔离,export能够提供的Bean,import须要的bean,这样也能让开发者分明每个模块依赖的货色。当我的项目收缩到须要去做服务化的时候,这样,拆分起来就非常简单了。
sofa
SOFABoot 是蚂蚁金服开源的基于 Spring Boot 的研发框架。提供了很多性能,如扩大 Spring Boot 健康检查的能力,提供模块化开发的能力,减少模块并行加载和 Spring Bean 异步初始化能力,减少日志空间隔离的能力,减少类隔离的能力,减少中间件集成治理的能力,其中就有模块化隔离性能。
模块化隔离
原理
原理:基于 Spring 上下文隔离的模块化,用 Spring 上下文来做不同功能模块的隔离,在开发期和编译期,代码和配置也会分在不同 Java 工程中,但在运行期,不同模块间的 Spring Bean 互相不可见,然而所有的 Java 类还是在同一个 ClassLoader 下。
https://www.sofastack.tech/pr...
sofa模块
首先咱们须要创立一个基于sofa的模块,一个sofa模块与一般我的项目类似,多一个配置文件,使之可能被辨认成一个sofa模块。同时多个sofa模块又都在同一个Spring Boot 我的项目文件夹下。
.├── README.md├── build.gradle├── gradle│ └── wrapper├── gradle.properties├── gradlew├── gradlew.bat├── logs│ ├── health-check│ ├── infra│ ├── sofa-runtime│ └── spring.log├── pom.xml├── service-consumer│ ├── build│ ├── build.gradle│ ├── pom.xml│ └── src├── service-facade│ ├── build│ ├── build.gradle│ ├── pom.xml│ └── src├── service-provider│ ├── build│ ├── build.gradle│ ├── pom.xml│ └── src├── settings.gradle└── sofa-boot-run ├── build ├── build.gradle ├── pom.xml └── src
对于每一个sofa模块,多一个sofa-module.properties 文件(src/main/resources 目录下)
Module-Name=com.alipay.test.biz.service.implRequire-Module=com.alipay.test.biz.shared
定义sofa模块的名字和须要的sofa模块。
公布与援用
SOFABoot 提供三种形式给开发人员公布和援用 JVM 服务
- XML 形式
- Annotation 形式
- 编程 API 形式
咱们定义一个service-provider模块用于提供服务,定义一个service-consumer模块用于援用服务
其中要在service-consumer模块的配置文件中退出
Module-Name=com.alipay.sofa.service-consumerRequire-Module=com.alipay.sofa.service-provider
用于定义启动程序
Annotation 形式
service-facade 模块蕴含用于演示 JVM 服务公布与援用的 API
public interface SampleJvmService { String message();}
公布服务
实现 SampleJvmService 接口并减少 @SofaService 注解:
@SofaServicepublic class SampleJvmServiceAnnotationImpl implements SampleJvmService { @Override public String message() { String message = "Hello, jvm service annotation implementation."; System.out.println(message); return message; }}
将 SampleJvmServiceAnnotationImpl 配置成一个 Spring Bean,保障 @SofaService 注解失效:
<bean id="sampleJvmServiceAnnotation" class="com.alipay.sofa.isle.sample.provider.SampleJvmServiceAnnotationImpl"/>
援用服务
定义 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.consumer.JvmServiceConsumer" init-method="init" />
参考
https://www.sofastack.tech/pr...
https://github.com/sofastack-...