Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #370 from sofastack/yuan_dev
Browse files Browse the repository at this point in the history
Module reuse base data source
  • Loading branch information
yuanyuancin authored Dec 11, 2023
2 parents ec783fe + f660a68 commit 8c48aeb
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,38 @@ weight: 600
强烈建议使用本文档方式,在模块中尽可能**复用基座数据源**,否则模块反复部署就会反复创建、消耗数据源连接,导致模块发布运维会变慢,同时也会额外消耗内存。<br/>

## SpringBoot 解法
在模块的代码中写个 MybatisConfig 类即可,这样事务模板都是复用基座的,只有 Mybatis 的 SqlSessionFactoryBean 需要新创建。<br />通过`BaseAppUtils.getBean`获取到基座的 Bean 对象,然后注册成模块的 Bean:
在模块的代码中写个 MybatisConfig 类即可,这样事务模板都是复用基座的,只有 Mybatis 的 SqlSessionFactoryBean 需要新创建。<br /> 参考 demo:/sofa-serverless/samples/springboot-samples/db/mybatis/biz1

通过`SpringBeanFinder.getBaseBean`获取到基座的 Bean 对象,然后注册成模块的 Bean:

```java

@Configuration
@MapperScan(basePackages = "com.alipay.serverless.dal.dao", sqlSessionFactoryRef = "mysqlSqlFactory")
@MapperScan(basePackages = "com.alipay.sofa.biz1.mapper", sqlSessionFactoryRef = "mysqlSqlFactory")
@EnableTransactionManagement
public class MybatisConfig {

// 注意:不要初始化一个基座的 DataSource,会导致模块被热卸载的时候,基座的数据源被销毁,不符合预期。
// 但是 transactionManager,transactionTemplate,mysqlSqlFactory 这些资源被销毁没有问题
//tips:不要初始化一个基座的DataSource,当模块被卸载的是,基座数据源会被销毁,transactionManager,transactionTemplate,mysqlSqlFactory被销毁没有问题

@Bean(name = "transactionManager")
public PlatformTransactionManager platformTransactionManager() {
return (PlatformTransactionManager) BaseAppUtils.getBean("transactionManager");
return (PlatformTransactionManager) getBaseBean("transactionManager");
}

@Bean(name = "transactionTemplate")
public TransactionTemplate transactionTemplate() {
return (TransactionTemplate) BaseAppUtils.getBean("transactionTemplate");
return (TransactionTemplate) getBaseBean("transactionTemplate");
}

@Bean(name = "mysqlSqlFactory")
public SqlSessionFactoryBean mysqlSqlFactory() throws IOException {
//数据源不能申明成模块spring上下文中的bean,因为模块卸载时会触发close方法
ZdalDataSource dataSource = (ZdalDataSource) BaseAppUtils.getBean("dataSource");

DataSource dataSource = (DataSource) getBaseBean("dataSource");
SqlSessionFactoryBean mysqlSqlFactory = new SqlSessionFactoryBean();
mysqlSqlFactory.setDataSource(dataSource);
mysqlSqlFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml"));
.getResources("classpath:mappers/*.xml"));
return mysqlSqlFactory;
}
}
Expand Down
67 changes: 66 additions & 1 deletion samples/springboot-samples/db/mybatis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ biz 包含两个模块,分别为 biz1 和 biz2, 都是普通 springboot,修
注意这里将不同 biz 的web context path 修改成不同的值,以此才能成功在一个 tomcat host 里安装多个 web 应用。


## 实验步骤
## 基座、模块各自定义数据源

### 本地部署 mysql 并启动

Expand Down Expand Up @@ -200,6 +200,71 @@ curl http://localhost:8080/biz1mybatis/testmybatis
```
返回 student 表中的内容,且可以发现使用的数据源已经变为 DruidDataSource

## 模块复用基座数据源

### 修改模块配置

在上一节「基座、模块各自定义数据源」的基础上

1. 移除模块数据源配置,在biz的application.properties文件中注释掉数据源datasource相关配置项
```properties
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.username=root
#spring.datasource.password=Zfj1995!
#spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
#
#mybatis.mapper-locations=classpath:mappers/*.xml
#
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.druid.initial-size=5
#spring.datasource.druid.min-idle=5
#spring.datasource.druid.max-active=200
#spring.datasource.druid.max-wait=60000
#spring.datasource.druid.time-between-eviction-runs-millis=60000
#spring.datasource.druid.min-evictable-idle-time-millis=300000
#spring.datasource.druid.test-while-idle=true
#spring.datasource.druid.test-on-borrow=false
#spring.datasource.druid.test-on-return=false
#spring.datasource.druid.pool-prepared-statements=false
#spring.datasource.druid.filters=stat,wall,slf4j
```

2. 添加模块MybatisConfig
```java
@Configuration
@MapperScan(basePackages = "com.alipay.sofa.biz1.mapper", sqlSessionFactoryRef = "mysqlSqlFactory")
@EnableTransactionManagement
public class MybatisConfig {

//tips:不要初始化一个基座的DataSource,当模块被卸载的是,基座数据源会被销毁,transactionManager,transactionTemplate,mysqlSqlFactory被销毁没有问题

@Bean(name = "transactionManager")
public PlatformTransactionManager platformTransactionManager() {
return (PlatformTransactionManager) getBaseBean("transactionManager");
}

@Bean(name = "transactionTemplate")
public TransactionTemplate transactionTemplate() {
return (TransactionTemplate) getBaseBean("transactionTemplate");
}

@Bean(name = "mysqlSqlFactory")
public SqlSessionFactoryBean mysqlSqlFactory() throws IOException {
//数据源不能申明成模块spring上下文中的bean,因为模块卸载时会触发close方法

DataSource dataSource = (DataSource) getBaseBean("dataSource");
SqlSessionFactoryBean mysqlSqlFactory = new SqlSessionFactoryBean();
mysqlSqlFactory.setDataSource(dataSource);
mysqlSqlFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mappers/*.xml"));
return mysqlSqlFactory;
}
}
```

同上一节「基座、模块各自定义数据源」启动基座、部署模块、发起验证即可。


## 注意事项
这里主要使用简单应用做验证,如果复杂应用,需要注意模块做好瘦身,基座有的依赖,模块尽可能设置成 provided,尽可能使用基座的依赖。

29 changes: 12 additions & 17 deletions samples/springboot-samples/db/mybatis/biz1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,24 @@
<scope>test</scope>
</dependency>

<!-- <dependency>-->
<!-- <groupId>com.alipay.sofa.serverless</groupId>-->
<!-- <artifactId>sofa-serverless-app-starter</artifactId>-->
<!-- <version>${sofa.serverless.runtime.version}</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.kafka</groupId>-->
<!-- <artifactId>spring-kafka</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-app-starter</artifactId>
<version>${sofa.serverless.runtime.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-log4j2</artifactId>
<version>${sofa.serverless.runtime.version}</version>
<scope>provided</scope>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-actuator-autoconfigure</artifactId>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-api</artifactId>
<version>${sofa.ark.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
@MapperScan(basePackages = "com.alipay.sofa.biz1.mapper")
//@MapperScan(basePackages = "com.alipay.sofa.biz1.mapper")
public class Biz1Application {
private static Logger LOGGER = LoggerFactory.getLogger(Biz1Application.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.alipay.sofa.biz1;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.io.IOException;

import static com.alipay.sofa.serverless.common.api.SpringBeanFinder.getBaseBean;

/**
* @author: yuanyuan
* @date: 2023/12/5 3:37 下午
* 模块复用基座 transactionManager、transactionTemplate、以及 dataSource
*/
@Configuration
@MapperScan(basePackages = "com.alipay.sofa.biz1.mapper", sqlSessionFactoryRef = "mysqlSqlFactory")
@EnableTransactionManagement
public class MybatisConfig {

//tips:不要初始化一个基座的DataSource,当模块被卸载的是,基座数据源会被销毁,transactionManager,transactionTemplate,mysqlSqlFactory被销毁没有问题

@Bean(name = "transactionManager")
public PlatformTransactionManager platformTransactionManager() {
return (PlatformTransactionManager) getBaseBean("transactionManager");
}

@Bean(name = "transactionTemplate")
public TransactionTemplate transactionTemplate() {
return (TransactionTemplate) getBaseBean("transactionTemplate");
}

@Bean(name = "mysqlSqlFactory")
public SqlSessionFactoryBean mysqlSqlFactory() throws IOException {
//数据源不能申明成模块spring上下文中的bean,因为模块卸载时会触发close方法

DataSource dataSource = (DataSource) getBaseBean("dataSource");
SqlSessionFactoryBean mysqlSqlFactory = new SqlSessionFactoryBean();
mysqlSqlFactory.setDataSource(dataSource);
mysqlSqlFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mappers/*.xml"));
return mysqlSqlFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ logging.level.root=INFO
logging.level.com.alipay.sofa.arklet=INFO
logging.config=classpath:log4j2-spring.xml

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=Zfj1995!
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useSSL=false

mybatis.mapper-locations=classpath:mappers/*.xml

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=5
spring.datasource.druid.min-idle=5
spring.datasource.druid.max-active=200
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.filters=stat,wall,slf4j
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.username=root
#spring.datasource.password=Zfj1995!
#spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useSSL=false
#
#mybatis.mapper-locations=classpath:mappers/*.xml
#
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.druid.initial-size=5
#spring.datasource.druid.min-idle=5
#spring.datasource.druid.max-active=200
#spring.datasource.druid.max-wait=60000
#spring.datasource.druid.time-between-eviction-runs-millis=60000
#spring.datasource.druid.min-evictable-idle-time-millis=300000
#spring.datasource.druid.test-while-idle=true
#spring.datasource.druid.test-on-borrow=false
#spring.datasource.druid.test-on-return=false
#spring.datasource.druid.pool-prepared-statements=false
#spring.datasource.druid.filters=stat,wall,slf4j
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

/**
* 支持将配置转换为 log4j2 context.
* 1. 设置LoggingSystem=SOFAServerlessLog4j2LoggingSystem
* 2. 添加属性到log4j2 ThreadContext
*
* @author ruoshan
* @version $Id: AlipayLog4j2SpringContextListener.java, v 0.1 2018年08月23日 10:32 PM ruoshan Exp $
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.serverless.common.api;

import com.alipay.sofa.ark.api.ArkClient;
import com.alipay.sofa.ark.spi.model.Biz;
import com.alipay.sofa.serverless.common.BizRuntimeContext;
import com.alipay.sofa.serverless.common.BizRuntimeContextRegistry;

/**
* @author: yuanyuan
* @date: 2023/12/8 5:26 下午
*
* SpringBeanFinder 查找基座bean工具类,无跨classloader支持
*/
public class SpringBeanFinder {

public static Object getBaseBean(String name) {
Biz masterBiz = ArkClient.getMasterBiz();
BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry
.getBizRuntimeContext(masterBiz);
return bizRuntimeContext.getRootApplicationContext().getBean(name);
}

public static <T> T getBaseBean(Class<T> type) {
Biz masterBiz = ArkClient.getMasterBiz();
BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry
.getBizRuntimeContext(masterBiz);
return bizRuntimeContext.getRootApplicationContext().getBean(type);
}
}
Loading

0 comments on commit 8c48aeb

Please sign in to comment.