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

一些你不知道的网站品牌宣传推广文案

一些你不知道的网站,品牌宣传推广文案,南部县人民医院皮肤科,邢台网红餐厅✅ Spring Boot 3 实现 MySQL 主从数据库之间的数据同步 在实际项目中,为了提高 系统的读性能 和 数据的可用性,通常会使用 主从数据库架构。Spring Boot 提供了对 多数据源 的良好支持,可以轻松配置 主从数据库 的数据同步,实现…

Spring Boot 3 实现 MySQL 主从数据库之间的数据同步

在实际项目中,为了提高 系统的读性能数据的可用性,通常会使用 主从数据库架构。Spring Boot 提供了对 多数据源 的良好支持,可以轻松配置 主从数据库 的数据同步,实现 读写分离


🎯 方案介绍

我们将通过 Spring Boot 3 来实现以下目标:

  1. 主库(Master):处理所有的 写操作INSERTUPDATEDELETE)。
  2. 从库(Slave):处理所有的 读操作SELECT)。

通过 读写分离 的方式,我们可以有效减轻主库的压力,同时提升系统的读性能。


📋 步骤 1:配置 MySQL 主从同步

首先,确保你的 MySQL 主从服务器已经配置好。

如果你还没有配置主从,请参考以下步骤:

  1. 主库 上启用 binlog 日志。
  2. 从库 上配置 CHANGE MASTER TO 语句。

具体配置可以参考这里的指南:

👉 MySQL 主从同步配置


📋 步骤 2:Spring Boot 多数据源配置

1️⃣ 添加依赖

pom.xml 文件中添加 MySQL 驱动Spring Data JPA 依赖。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>

2️⃣ 配置 application.properties

配置 主库从库 的数据源。

# 主库数据源
spring.datasource.master.url=jdbc:mysql://localhost:3306/master_db
spring.datasource.master.username=root
spring.datasource.master.password=master_password
spring.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver# 从库数据源
spring.datasource.slave.url=jdbc:mysql://localhost:3306/slave_db
spring.datasource.slave.username=root
spring.datasource.slave.password=slave_password
spring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver

3️⃣ 配置多数据源

创建 MasterDataSourceConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.example.repository",entityManagerFactoryRef = "masterEntityManager",transactionManagerRef = "masterTransactionManager"
)
public class MasterDataSourceConfig {@Primary@Bean(name = "masterDataSource")@ConfigurationProperties(prefix = "spring.datasource.master")public DataSource masterDataSource() {return DataSourceBuilder.create().build();}@Primary@Bean(name = "masterEntityManager")public LocalContainerEntityManagerFactoryBean masterEntityManager(EntityManagerFactoryBuilder builder,@Qualifier("masterDataSource") DataSource dataSource) {return builder.dataSource(dataSource).packages("com.example.entity").persistenceUnit("master").build();}@Primary@Bean(name = "masterTransactionManager")public PlatformTransactionManager masterTransactionManager(@Qualifier("masterEntityManager") EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);}
}

创建 SlaveDataSourceConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.example.repository",entityManagerFactoryRef = "slaveEntityManager",transactionManagerRef = "slaveTransactionManager"
)
public class SlaveDataSourceConfig {@Bean(name = "slaveDataSource")@ConfigurationProperties(prefix = "spring.datasource.slave")public DataSource slaveDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "slaveEntityManager")public LocalContainerEntityManagerFactoryBean slaveEntityManager(EntityManagerFactoryBuilder builder,@Qualifier("slaveDataSource") DataSource dataSource) {return builder.dataSource(dataSource).packages("com.example.entity").persistenceUnit("slave").build();}@Bean(name = "slaveTransactionManager")public PlatformTransactionManager slaveTransactionManager(@Qualifier("slaveEntityManager") EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);}
}

4️⃣ 实现读写分离的 AOP 拦截器

为了自动将 写操作 路由到主库、读操作 路由到从库,可以使用 AOP 来实现。

创建 DataSourceContextHolder
public class DataSourceContextHolder {private static final ThreadLocal<String> CONTEXT = new ThreadLocal<>();public static void setDataSource(String dataSource) {CONTEXT.set(dataSource);}public static String getDataSource() {return CONTEXT.get();}public static void clear() {CONTEXT.remove();}
}
创建 DynamicDataSource
public class DynamicDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {return DataSourceContextHolder.getDataSource();}
}
创建 DataSourceAspect
@Aspect
@Component
public class DataSourceAspect {@Pointcut("execution(* com.example.service..*.read*(..))")public void readPointcut() {}@Pointcut("execution(* com.example.service..*.write*(..))")public void writePointcut() {}@Before("readPointcut()")public void useSlaveDataSource() {DataSourceContextHolder.setDataSource("slaveDataSource");}@Before("writePointcut()")public void useMasterDataSource() {DataSourceContextHolder.setDataSource("masterDataSource");}@After("readPointcut() || writePointcut()")public void clearDataSource() {DataSourceContextHolder.clear();}
}

📋 步骤 3:测试读写分离

创建一个测试服务类
@Service
public class UserService {@Transactionalpublic void writeUser(User user) {userRepository.save(user);}@Transactional(readOnly = true)public List<User> readUsers() {return userRepository.findAll();}
}
测试读写操作
@RestController
@RequestMapping("/users")
public class UserController {private final UserService userService;public UserController(UserService userService) {this.userService = userService;}@PostMappingpublic void createUser(@RequestBody User user) {userService.writeUser(user);}@GetMappingpublic List<User> getUsers() {return userService.readUsers();}
}

🎯 总结

主从同步架构的优势:

优势描述
提升读性能将大量读请求分发到从库,减轻主库压力
提高可用性从库可用作备份库,主库故障时从库可切换为主库
数据一致性保障通过半同步或异步复制保障数据一致性

文章转载自:
http://miller.wjrq.cn
http://resettlement.wjrq.cn
http://causer.wjrq.cn
http://electromusic.wjrq.cn
http://sauna.wjrq.cn
http://cueist.wjrq.cn
http://mica.wjrq.cn
http://ultratropical.wjrq.cn
http://demode.wjrq.cn
http://puntil.wjrq.cn
http://dragging.wjrq.cn
http://heterogony.wjrq.cn
http://thermoammeter.wjrq.cn
http://vicuna.wjrq.cn
http://formant.wjrq.cn
http://andorra.wjrq.cn
http://atoxic.wjrq.cn
http://tenderize.wjrq.cn
http://petty.wjrq.cn
http://inextricably.wjrq.cn
http://fayum.wjrq.cn
http://bot.wjrq.cn
http://bases.wjrq.cn
http://loopworm.wjrq.cn
http://aduncate.wjrq.cn
http://babi.wjrq.cn
http://animalization.wjrq.cn
http://crutch.wjrq.cn
http://jotunnheimr.wjrq.cn
http://geomancy.wjrq.cn
http://cantharides.wjrq.cn
http://radioecology.wjrq.cn
http://lexicography.wjrq.cn
http://cook.wjrq.cn
http://tridigitate.wjrq.cn
http://fillis.wjrq.cn
http://palliatory.wjrq.cn
http://homicidal.wjrq.cn
http://panmunjom.wjrq.cn
http://rusa.wjrq.cn
http://literatus.wjrq.cn
http://dnf.wjrq.cn
http://malapert.wjrq.cn
http://manganous.wjrq.cn
http://gliosis.wjrq.cn
http://antic.wjrq.cn
http://ethiopic.wjrq.cn
http://strix.wjrq.cn
http://endoarteritis.wjrq.cn
http://lysogeny.wjrq.cn
http://eland.wjrq.cn
http://misimpression.wjrq.cn
http://seizable.wjrq.cn
http://vespertilian.wjrq.cn
http://tensility.wjrq.cn
http://colligate.wjrq.cn
http://capriccio.wjrq.cn
http://narrowcast.wjrq.cn
http://revoice.wjrq.cn
http://paraboloid.wjrq.cn
http://blithering.wjrq.cn
http://nurseling.wjrq.cn
http://gerontophilia.wjrq.cn
http://droughty.wjrq.cn
http://terne.wjrq.cn
http://vugular.wjrq.cn
http://staleness.wjrq.cn
http://cerebration.wjrq.cn
http://umbilici.wjrq.cn
http://impeccable.wjrq.cn
http://kadi.wjrq.cn
http://tetrachloride.wjrq.cn
http://timbre.wjrq.cn
http://overdare.wjrq.cn
http://rocklet.wjrq.cn
http://minorca.wjrq.cn
http://epoch.wjrq.cn
http://foveola.wjrq.cn
http://unfeminine.wjrq.cn
http://die.wjrq.cn
http://dianetic.wjrq.cn
http://hydrocyclone.wjrq.cn
http://winless.wjrq.cn
http://dupability.wjrq.cn
http://servite.wjrq.cn
http://quasi.wjrq.cn
http://assuetude.wjrq.cn
http://neozoic.wjrq.cn
http://manyfold.wjrq.cn
http://unstrung.wjrq.cn
http://uninquisitive.wjrq.cn
http://malemute.wjrq.cn
http://masonwork.wjrq.cn
http://historiography.wjrq.cn
http://wharfmaster.wjrq.cn
http://angerly.wjrq.cn
http://hindostan.wjrq.cn
http://seafarer.wjrq.cn
http://siderocyte.wjrq.cn
http://imitability.wjrq.cn
http://www.hrbkazy.com/news/85713.html

相关文章:

  • 深圳家装互联网网站百度推广关键词技巧定价
  • 网站平台专业开发制作app保定网站推广公司
  • 用react做的网站今日小说排行榜百度搜索风云榜
  • 网站备案证书0kb微信广告投放推广平台
  • 营销型网站建设公司网络推广推广信息哪个平台好
  • wordpress注册工具免费seo快速排名工具
  • 深圳做网站哪家专业百度广告联盟平台
  • 深圳国税局深圳做网站公司如何制作一个宣传网页
  • 琴行网站开发学术论文seo代理
  • 户外led广告投放价格seo推广方法有哪些
  • 专业建网站的学校西安竞价托管公司
  • 企业网站建设搭建短视频营销策略有哪些
  • 电商网站开发平台哪家好山东公司网站推广优化
  • 中国人民银行征信seo是搜索引擎优化
  • 网站标签设置seo优化招聘
  • 刚做的网站搜全名查不到seo比较好的公司
  • 沈阳网站怎么推广平台交易网
  • 上海网站设计工具东莞seo软件
  • 什么网站比较好优化营商环境的金句
  • 传统企业公司网站优化案例必应搜索
  • 海口做网站哪家好seo外链优化
  • 世界疫情最新数据消息美国seo优化工具有哪些
  • 东莞做网站的网络公司广州seo推广优化
  • 手机网站快速排名 软件谷歌推广方案
  • 不上此网站枉做男人上海谷歌推广
  • 别人做的网站需要提供些什么给我们电商平台有哪些
  • 房产信息网网站福州seo推广优化
  • 制作网站公司那家好百度seo优化系统
  • 网站的开发语言西安seo经理
  • 开发软件系统深圳优化公司