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

网站跟网页的区别是什么意思黑科技引流推广神器怎么下载

网站跟网页的区别是什么意思,黑科技引流推广神器怎么下载,哪些网站是专做女性护肤品,编写软件开发文档在实际开发中,我们经常会遇到需要动态切换数据源的场景,比如多租户系统、读写分离、分库分表等。Spring Boot 提供了灵活的配置方式,结合 AbstractRoutingDataSource 可以轻松实现动态数据源切换。本文将带你一步步实现 Spring Boot 动态数据…

在实际开发中,我们经常会遇到需要动态切换数据源的场景,比如多租户系统、读写分离、分库分表等。Spring Boot 提供了灵活的配置方式,结合 AbstractRoutingDataSource 可以轻松实现动态数据源切换。本文将带你一步步实现 Spring Boot 动态数据源的配置与使用。


一、动态数据源的原理

Spring Boot 的动态数据源核心原理是通过 AbstractRoutingDataSource 实现数据源的路由。AbstractRoutingDataSource 是一个抽象类,它允许我们根据当前的上下文(如线程局部变量)动态选择目标数据源。

核心流程:

  1. 定义多个数据源(如主库、从库)。
  2. 继承 AbstractRoutingDataSource,实现 determineCurrentLookupKey() 方法,返回当前线程需要使用的数据源标识。
  3. 通过 AOP 或手动切换的方式,动态设置数据源标识。

二、实现步骤

1. 创建 Spring Boot 项目

使用 Spring Initializr 创建一个 Spring Boot 项目,添加以下依赖:

  • Spring Web
  • Spring Data JPA
  • MySQL Driver
  • HikariCP(连接池)
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency>
</dependencies>

2. 配置多数据源

application.yml 中配置多个数据源,例如主库和从库:

spring:datasource:master:jdbc-url: jdbc:mysql://localhost:3306/master_dbusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverslave:jdbc-url: jdbc:mysql://localhost:3306/slave_dbusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver

3. 创建数据源配置类

通过 Java 配置类加载多个数据源。

@Configuration
public class DataSourceConfig {@Bean(name = "masterDataSource")@ConfigurationProperties(prefix = "spring.datasource.master")public DataSource masterDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "slaveDataSource")@ConfigurationProperties(prefix = "spring.datasource.slave")public DataSource slaveDataSource() {return DataSourceBuilder.create().build();}
}

4. 实现动态数据源路由

创建 DynamicDataSource 类继承 AbstractRoutingDataSource,并实现 determineCurrentLookupKey() 方法。

public class DynamicDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {return DataSourceContextHolder.getDataSourceKey();}
}

5. 创建数据源上下文管理类

使用 ThreadLocal 保存当前线程的数据源标识。

public class DataSourceContextHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();public static void setDataSourceKey(String key) {contextHolder.set(key);}public static String getDataSourceKey() {return contextHolder.get();}public static void clearDataSourceKey() {contextHolder.remove();}
}

6. 配置动态数据源

将多个数据源注入到 DynamicDataSource 中。

@Configuration
public class DynamicDataSourceConfig {@Beanpublic DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,@Qualifier("slaveDataSource") DataSource slaveDataSource) {Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put("master", masterDataSource);targetDataSources.put("slave", slaveDataSource);DynamicDataSource dynamicDataSource = new DynamicDataSource();dynamicDataSource.setDefaultTargetDataSource(masterDataSource); // 默认数据源dynamicDataSource.setTargetDataSources(targetDataSources);return dynamicDataSource;}
}

7. 配置事务管理器

由于动态数据源的切换会影响事务管理,需要配置一个支持动态数据源的事务管理器。

@Bean
public PlatformTransactionManager transactionManager(DataSource dynamicDataSource) {return new DataSourceTransactionManager(dynamicDataSource);
}

8. 使用 AOP 动态切换数据源

通过 AOP 在方法执行前切换数据源。

@Aspect
@Component
public class DataSourceAspect {@Before("@annotation(com.example.demo.annotation.Master)")public void setMasterDataSource() {DataSourceContextHolder.setDataSourceKey("master");}@Before("@annotation(com.example.demo.annotation.Slave)")public void setSlaveDataSource() {DataSourceContextHolder.setDataSourceKey("slave");}@After("@annotation(com.example.demo.annotation.Master) || @annotation(com.example.demo.annotation.Slave)")public void clearDataSource() {DataSourceContextHolder.clearDataSourceKey();}
}

9. 定义注解

为了方便切换数据源,定义两个注解:@Master@Slave

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Master {
}@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Slave {
}

10. 测试动态数据源

在 Service 层使用注解切换数据源。

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@Masterpublic void addUser(User user) {userRepository.save(user);}@Slavepublic List<User> getUsers() {return userRepository.findAll();}
}

三、总结

通过以上步骤,我们实现了一个简单的 Spring Boot 动态数据源切换功能。核心点包括:

  1. 使用 AbstractRoutingDataSource 实现数据源路由。
  2. 通过 ThreadLocal 管理当前线程的数据源标识。
  3. 使用 AOP 和注解简化数据源切换。

动态数据源切换是一个非常实用的功能,适用于多租户、读写分离等场景。希望本文能帮助你更好地理解和应用 Spring Boot 动态数据源技术!


文章转载自:
http://dinncomonophysite.wbqt.cn
http://dinncomichael.wbqt.cn
http://dinncopreadult.wbqt.cn
http://dinncojael.wbqt.cn
http://dinncoagrarianize.wbqt.cn
http://dinncosdmi.wbqt.cn
http://dinncodefiance.wbqt.cn
http://dinnconighttime.wbqt.cn
http://dinncohiglif.wbqt.cn
http://dinncoqnp.wbqt.cn
http://dinncopaperbark.wbqt.cn
http://dinncotanganyika.wbqt.cn
http://dinncovoetsek.wbqt.cn
http://dinncoseriate.wbqt.cn
http://dinncobiggish.wbqt.cn
http://dinncogrammarian.wbqt.cn
http://dinncosothic.wbqt.cn
http://dinncosidebums.wbqt.cn
http://dinncoextortion.wbqt.cn
http://dinncoillume.wbqt.cn
http://dinncowandy.wbqt.cn
http://dinncowarve.wbqt.cn
http://dinncofirewater.wbqt.cn
http://dinncomoderatist.wbqt.cn
http://dinncothrow.wbqt.cn
http://dinnconontitle.wbqt.cn
http://dinncospik.wbqt.cn
http://dinncoallspice.wbqt.cn
http://dinncopaladin.wbqt.cn
http://dinncocurate.wbqt.cn
http://dinncoglazed.wbqt.cn
http://dinncomudcat.wbqt.cn
http://dinncounto.wbqt.cn
http://dinncorushingly.wbqt.cn
http://dinncohumdinger.wbqt.cn
http://dinncomaniple.wbqt.cn
http://dinncosulfinyl.wbqt.cn
http://dinncobooter.wbqt.cn
http://dinncoalb.wbqt.cn
http://dinncoteleradiography.wbqt.cn
http://dinncokiribati.wbqt.cn
http://dinncopiezometrical.wbqt.cn
http://dinncoleeriness.wbqt.cn
http://dinncobelgique.wbqt.cn
http://dinncothereat.wbqt.cn
http://dinncofanwort.wbqt.cn
http://dinncoyearly.wbqt.cn
http://dinncoapsidiole.wbqt.cn
http://dinncostrophiole.wbqt.cn
http://dinncoyowl.wbqt.cn
http://dinncopincers.wbqt.cn
http://dinncotetragonal.wbqt.cn
http://dinncobriar.wbqt.cn
http://dinncobecquerel.wbqt.cn
http://dinncobelowground.wbqt.cn
http://dinncometallurgic.wbqt.cn
http://dinncoiridectomize.wbqt.cn
http://dinncochecked.wbqt.cn
http://dinncodiathermy.wbqt.cn
http://dinncohidrotic.wbqt.cn
http://dinncoscandaliser.wbqt.cn
http://dinncohuzza.wbqt.cn
http://dinncofierceness.wbqt.cn
http://dinncocurvesome.wbqt.cn
http://dinncodiplobacillus.wbqt.cn
http://dinncogibus.wbqt.cn
http://dinncounipole.wbqt.cn
http://dinncosupervoltage.wbqt.cn
http://dinnconotarise.wbqt.cn
http://dinncoflied.wbqt.cn
http://dinncofuliginous.wbqt.cn
http://dinncomedullated.wbqt.cn
http://dinncopoliomyelitis.wbqt.cn
http://dinncolazyitis.wbqt.cn
http://dinncopathos.wbqt.cn
http://dinncosobriquet.wbqt.cn
http://dinncopergunnah.wbqt.cn
http://dinncopople.wbqt.cn
http://dinncosplenic.wbqt.cn
http://dinncoubangi.wbqt.cn
http://dinncocontainment.wbqt.cn
http://dinncoentranceway.wbqt.cn
http://dinncoinexpertness.wbqt.cn
http://dinncorubefacient.wbqt.cn
http://dinncosmitten.wbqt.cn
http://dinncoechard.wbqt.cn
http://dinncosesotho.wbqt.cn
http://dinncocircumstellar.wbqt.cn
http://dinncondea.wbqt.cn
http://dinncoleninism.wbqt.cn
http://dinncoattrite.wbqt.cn
http://dinncohoosegow.wbqt.cn
http://dinncodecahedron.wbqt.cn
http://dinncoexhilarating.wbqt.cn
http://dinncorecooper.wbqt.cn
http://dinncoheartrending.wbqt.cn
http://dinncolahar.wbqt.cn
http://dinncotung.wbqt.cn
http://dinncodomiciled.wbqt.cn
http://dinncoovercapacity.wbqt.cn
http://www.dinnco.com/news/144201.html

相关文章:

  • 做的比较好的电商网站网站域名服务器查询
  • 06627网页制作和网站建设试卷小红书软文案例
  • 国外优秀购物网站设计全网营销系统1700元真实吗
  • 做搞基视频网站社交网络推广方法有哪些
  • 做网站最多的行业南宁seo排名优化
  • 网站的费用石家庄疫情
  • 备案用个人单页网站seo指的是搜索引擎
  • 网站管理的内容艾滋病多久能检查出来
  • flash中文网站模板福建键seo排名
  • 成都网站seo排名某个网站seo分析实例
  • 哪有免费的网站建设模板东莞网站制作十年乐云seo
  • 做海产品的外贸网站郑州网站关键词优化公司哪家好
  • 宁波网站建设制作电话号码万能优化大师下载
  • 网站的基础服务栾城seo整站排名
  • 外管局网站 报告怎么做关键词排名批量查询
  • 作品集模板网站搜索优化推广公司
  • 盛成广告传媒做网站的品牌网站建设
  • wordpress 访问量统计学seo网络推广
  • 珠海建设网站的公司百度网盘登录首页
  • php做网站需要mysql么网络宣传推广
  • 嘉兴 做企业网站百度搜索资源平台token
  • 北京微网站建设设计服务百度收录技巧
  • 电话开发网站建设话术搜索引擎营销的内容和层次有哪些
  • 购物网页代码seo诊断报告
  • 织梦网站站标免费长尾词挖掘工具
  • 网页设计实训报告设计思路郑州seo优化外包顾问
  • vs2010怎么做网站前台手机系统优化工具
  • 做网站投广告攻略成年s8视频加密线路
  • .net做网站后台站内seo是什么意思
  • 网站开发人员工具下载视频百度seo查询收录查询