当前位置: 首页 > news >正文

金坛网站建设哪家好外链发布平台大全

金坛网站建设哪家好,外链发布平台大全,wordpress 当前页url,建设什么网站好文章目录 前言一、多数据源配置与切换方案二、实现步骤1. 创建多个 DataSource 配置类2. 创建 DataSource 配置类3. 创建动态数据源路由类4. 实现 DynamicDataSource 类5. 创建 DataSourceContextHolder 来存储当前的数据源标识6. AOP 方式切换数据源7. 自定义注解来指定数据源…

文章目录

  • 前言
  • 一、多数据源配置与切换方案
  • 二、实现步骤
    • 1. 创建多个 `DataSource` 配置类
    • 2. 创建 `DataSource` 配置类
    • 3. 创建动态数据源路由类
    • 4. 实现 `DynamicDataSource` 类
    • 5. 创建 `DataSourceContextHolder` 来存储当前的数据源标识
    • 6. AOP 方式切换数据源
    • 7. 自定义注解来指定数据源
    • 8. 在 Service 层使用注解指定数据源
  • 总结


前言

  在 Spring Boot 中实现多数据源连接和切换,可以通过以下几种方案来实现,具体取决于项目的需求、数据库的使用模式和管理的复杂性。以下是一个常见的多数据源切换的实现方案,使用 AbstractRoutingDataSource 来动态选择数据源。


一、多数据源配置与切换方案

在多数据源场景中,通常有如下步骤:

  • 配置多个数据源的 DataSource bean。
  • 使用 AbstractRoutingDataSource 来动态切换数据源。
  • 使用 ThreadLocal 存储当前的数据库类型或数据源标识符。
  • 配置数据源切换的逻辑,例如基于当前的用户、请求路径、服务标识等来选择不同的数据源。

二、实现步骤

1. 创建多个 DataSource 配置类

首先,为每个数据源创建单独的配置类,通常你会在 application.ymlapplication.properties 中配置每个数据源的连接信息。

spring:datasource:# 默认数据源配置primary:url: jdbc:mysql://localhost:3306/primary_dbusername: rootpassword: passworddriver-class-name: com.mysql.cj.jdbc.Driverhikari:maximum-pool-size: 10# 第二数据源配置secondary:url: jdbc:mysql://localhost:3306/secondary_dbusername: rootpassword: passworddriver-class-name: com.mysql.cj.jdbc.Driverhikari:maximum-pool-size: 10

2. 创建 DataSource 配置类

@Configuration
@EnableTransactionManagement
public class DataSourceConfig {@Bean(name = "primaryDataSource")@Primary@ConfigurationProperties(prefix = "spring.datasource.primary")public DataSource primaryDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "secondaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}
}

3. 创建动态数据源路由类

AbstractRoutingDataSource 允许我们在运行时根据某些条件动态选择数据源。

@Configuration
public class DynamicDataSourceConfig {@Autowired@Qualifier("primaryDataSource")private DataSource primaryDataSource;@Autowired@Qualifier("secondaryDataSource")private DataSource secondaryDataSource;@Beanpublic DataSource dataSource() {// 创建一个路由数据源DynamicDataSource dataSource = new DynamicDataSource();dataSource.setDefaultTargetDataSource(primaryDataSource); // 默认数据源Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put("primary", primaryDataSource);targetDataSources.put("secondary", secondaryDataSource);dataSource.setTargetDataSources(targetDataSources);return dataSource;}
}

4. 实现 DynamicDataSource

DynamicDataSource 是继承自 AbstractRoutingDataSource,它通过 determineCurrentLookupKey() 方法来动态确定当前的数据源。

public class DynamicDataSource extends AbstractRoutingDataSource {// 从 ThreadLocal 获取当前的数据库标识@Overrideprotected Object determineCurrentLookupKey() {return DataSourceContextHolder.getDataSourceType();}
}

5. 创建 DataSourceContextHolder 来存储当前的数据源标识

使用 ThreadLocal 来保持当前线程的数据库标识,以便在不同的数据源之间切换。

public class DataSourceContextHolder {// 使用 ThreadLocal 存储当前线程的数据源标识private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();public static void setDataSourceType(String dataSourceType) {contextHolder.set(dataSourceType);}public static String getDataSourceType() {return contextHolder.get();}public static void clearDataSourceType() {contextHolder.remove();}
}

6. AOP 方式切换数据源

为了在运行时动态切换数据源,通常会使用 AOP 切面来拦截方法执行并指定数据源。

@Aspect
@Component
public class DataSourceAspect {// 通过注解来指定使用哪个数据源@Before("@annotation(dataSource)")public void switchDataSource(DataSourceType dataSource) {// 切换数据源DataSourceContextHolder.setDataSourceType(dataSource.value());}@After("@annotation(dataSource)")public void clearDataSource(DataSourceType dataSource) {// 清理数据源标识,避免影响其他线程DataSourceContextHolder.clearDataSourceType();}
}

7. 自定义注解来指定数据源

创建一个自定义注解 DataSourceType,用于指定当前方法执行时需要使用的数据源。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSourceType {String value() default "primary";  // 数据源标识,默认使用primary数据源
}

8. 在 Service 层使用注解指定数据源

在 Service 层,可以使用刚才创建的 @DataSourceType 注解来指定不同的方法使用不同的数据源。

@Service
public class UserService {@DataSourceType("primary")public List<User> getPrimaryUsers() {// 查询主数据库return userRepository.findAll();}@DataSourceType("secondary")public List<User> getSecondaryUsers() {// 查询次数据库return secondaryUserRepository.findAll();}
}

总结

  1. 数据源配置:为每个数据源配置 DataSource Bean。
  2. 动态数据源路由:使用 AbstractRoutingDataSource 来实现动态切换数据源。
  3. ThreadLocal存储:使用 ThreadLocal 存储和获取当前线程的数据源标识。
  4. AOP切换数据源:使用 AOP 来拦截方法并切换数据源。
  5. 注解方式指定数据源:通过自定义注解来指定方法使用的具体数据源。

这种方式比较灵活,能够在运行时根据业务需求选择不同的数据源,适用于多数据源的场景,尤其是分库分表、读写分离等复杂应用场景。


文章转载自:
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://.
http://www.tj-hxxt.cn/news/36479.html

相关文章:

  • 视觉差网站插件昆明seocn整站优化
  • 吴忠网站建设报价百度助手官网
  • 泉州wap网站制作免费推广产品的平台
  • 做钢材都有什么网站深圳网站优化排名
  • 可以做网络推广的网站国际新闻今日头条
  • 网站代码需要注意什么在百度上做广告推广要多少钱
  • 大连 响应式网站制作seo诊断分析工具
  • 开启IIs动态网站开发百度入口的链接
  • 江苏省建设类高工申报网站域名查询注册信息查询
  • 电脑web是什么意思网站快速优化排名app
  • 工业企业网络推广seo关键词优化经验技巧
  • 中英文 微信网站 怎么做怎么推广自己的网站
  • 铁岭手机网站建设b2b平台运营模式
  • 小型网站建设seo网址优化靠谱
  • 网站名字怎么取最好湖南知名网络推广公司
  • adobeXD做网站推广app赚钱的平台
  • 软文推广教程seo主要是指优化
  • 惠州外包网站建设站长统计app软件下载官网
  • 网站带做收录排名谷歌搜索引擎入口2023
  • 上海公司注册名字查询百度ocpc如何优化
  • 做网站有什么好的推荐什么是广告营销
  • vk社交网站做婚介优化师
  • wordpress面包屑seo外推软件
  • 一般设计网站页面用什么软件做百度正式员工工资待遇
  • 前端做数据表格的网站搜索引擎的营销方法有哪些
  • 微信网站制作入门汕头seo推广优化
  • 长沙营销网站建设公司沈阳百度推广优化
  • 发票 网站建设 单位经典模板网站建设
  • 珠海网站建设网络公司怎么样seol英文啥意思
  • 南宁网站设计多少钱seo优化软件有哪些