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

电子商务公司网站建立前期准备小程序开发制作

电子商务公司网站建立前期准备,小程序开发制作,免费推广工具,壁纸云 wordpress将 ProxySQL 配合 Spring Boot 使用,主要的目的是在 Spring Boot 应用程序中实现对 MySQL 主从同步的读写分离和负载均衡。这样,你可以利用 ProxySQL 自动将写操作路由到主库,而将读操作路由到从库。 1. 准备工作 确保你的 MySQL 主从同步环…

ProxySQL 配合 Spring Boot 使用,主要的目的是在 Spring Boot 应用程序中实现对 MySQL 主从同步的读写分离和负载均衡。这样,你可以利用 ProxySQL 自动将写操作路由到主库,而将读操作路由到从库。

1. 准备工作

确保你的 MySQL 主从同步环境和 ProxySQL 已经成功配置并正常工作。接下来,我们将进行以下几个步骤:

  • 配置 Spring Boot 连接 ProxySQL。
  • 配置 数据源 来支持读写分离。

2. 修改 Spring Boot 配置

在 Spring Boot 项目中,配置数据库连接时,使用 ProxySQL 作为 MySQL 的代理。你需要将 Spring Boot 的 数据源配置 进行一些调整,以支持读写分离。

1.1 配置 application.yml(或 application.properties

你可以在 application.yml 中配置多个数据源,分别对应主库和从库。ProxySQL 会作为一个代理处理读写分离。

示例:application.yml
spring:datasource:# 主数据源 (写库)primary:url: jdbc:mysql://127.0.0.1:6033/mydbusername: your_userpassword: your_passworddriver-class-name: com.mysql.cj.jdbc.Driverhikari:maximum-pool-size: 10minimum-idle: 5pool-name: PrimaryPool# 从数据源 (读库)secondary:url: jdbc:mysql://127.0.0.1:6033/mydbusername: your_userpassword: your_passworddriver-class-name: com.mysql.cj.jdbc.Driverhikari:maximum-pool-size: 10minimum-idle: 5pool-name: SecondaryPool# 设置数据源的路由策略jpa:hibernate:ddl-auto: updateproperties:hibernate:dialect: org.hibernate.dialect.MySQL8Dialectshow-sql: truedatabase-platform: org.hibernate.dialect.MySQL8Dialect

在这个配置文件中:

  • 主数据源(primary)用于写入操作,连接到 ProxySQL 的主库。
  • 从数据源(secondary)用于读取操作,连接到 ProxySQL 的从库。

ProxySQL 会根据 SQL 语句的类型(SELECT 路由到从库,INSERT/UPDATE 路由到主库)自动分配流量。

1.2 配置 DataSource 路由

Spring Boot 默认只支持单个数据源。如果你需要同时配置多个数据源(主从分离),需要定义一个 数据源路由 类,将请求的数据库连接动态路由到主库或从库。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.example.repository")
public class DataSourceConfig {@Primary@Bean(name = "primaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.primary")public DataSource dataSource() {return DataSourceBuilder.create().build();}@Bean(name = "secondaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}// 配置EntityManagerFactory和TransactionManager@Bean(name = "entityManagerFactory")public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {return builder.dataSource(dataSource).packages("com.example.model") // 你的实体类包路径.persistenceUnit("primary").build();}@Bean(name = "transactionManager")public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {return new JpaTransactionManager(entityManagerFactory);}
}

在这个配置中,我们定义了两个数据源:primaryDataSourcesecondaryDataSource,分别连接到 ProxySQL 的主库和从库。

3. 实现动态数据源路由

为了动态选择使用主库还是从库,你可以使用一个 AbstractRoutingDataSource 来实现动态的数据源路由。

public class DynamicDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {// 判断当前线程中是否有读请求或者写请求,选择数据源return DataSourceContextHolder.getDataSourceType();}
}

然后创建一个 DataSourceContextHolder 用来管理当前线程的数据源类型。

public class DataSourceContextHolder {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();}
}

4. 配置 Service 层的读写分离

接下来,你需要在服务层中手动切换读写操作的数据源。通常,你可以通过注解来区分读操作和写操作。

@Service
public class UserService {@Transactionalpublic void saveUser(User user) {// 使用主库保存数据DataSourceContextHolder.setDataSourceType("primary");userRepository.save(user);DataSourceContextHolder.clearDataSourceType();}public User getUser(Long id) {// 使用从库查询数据DataSourceContextHolder.setDataSourceType("secondary");User user = userRepository.findById(id).orElse(null);DataSourceContextHolder.clearDataSourceType();return user;}
}

在上面的代码中,saveUser 方法会选择主库,而 getUser 方法会选择从库。

5. 配置 Spring AOP 自动切换数据源(可选)

你可以通过 AOP 来简化读写分离的配置,使得你不需要手动设置数据源类型。只需在方法上使用自定义注解(如 @ReadOnly@WriteOnly),自动切换数据源。

自定义注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadOnly {
}@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface WriteOnly {
}
创建 AOP 切面:
@Aspect
@Component
public class DataSourceAspect {@Before("@annotation(ReadOnly)")public void setReadOnlyDataSource() {DataSourceContextHolder.setDataSourceType("secondary");}@Before("@annotation(WriteOnly)")public void setWriteOnlyDataSource() {DataSourceContextHolder.setDataSourceType("primary");}@After("@annotation(ReadOnly) || @annotation(WriteOnly)")public void clearDataSource() {DataSourceContextHolder.clearDataSourceType();}
}

在这段代码中:

  • 使用 @ReadOnly 注解的方法将会切换到从库。
  • 使用 @WriteOnly 注解的方法将会切换到主库。

6. 总结

通过以上步骤,你可以将 ProxySQL 与 Spring Boot 集成,实现 MySQL 主从同步的读写分离和负载均衡。

  • ProxySQL 作为 MySQL 的代理,负责将读请求路由到从库,写请求路由到主库。
  • Spring Boot 通过配置多个数据源和动态路由来实现读写分离。
  • 你可以通过 AOP 或手动设置来决定何时使用主库或从库。

文章转载自:
http://bly.fznj.cn
http://exegete.fznj.cn
http://salique.fznj.cn
http://heedfully.fznj.cn
http://featurette.fznj.cn
http://underjawed.fznj.cn
http://rinforzando.fznj.cn
http://melitopol.fznj.cn
http://impatiens.fznj.cn
http://juvenile.fznj.cn
http://gftu.fznj.cn
http://matriculability.fznj.cn
http://certifier.fznj.cn
http://waiwode.fznj.cn
http://spectrobolometer.fznj.cn
http://neoantigen.fznj.cn
http://cyberspace.fznj.cn
http://luminol.fznj.cn
http://glauberite.fznj.cn
http://knockout.fznj.cn
http://thriven.fznj.cn
http://technocrat.fznj.cn
http://specious.fznj.cn
http://monachize.fznj.cn
http://petalled.fznj.cn
http://dissimilation.fznj.cn
http://segu.fznj.cn
http://tampax.fznj.cn
http://melena.fznj.cn
http://phanariot.fznj.cn
http://hiphuggers.fznj.cn
http://glucosamine.fznj.cn
http://xenogamy.fznj.cn
http://sarcology.fznj.cn
http://included.fznj.cn
http://resid.fznj.cn
http://usefully.fznj.cn
http://realistically.fznj.cn
http://adlib.fznj.cn
http://proximal.fznj.cn
http://attractor.fznj.cn
http://plethora.fznj.cn
http://motile.fznj.cn
http://merci.fznj.cn
http://vespucci.fznj.cn
http://ortolan.fznj.cn
http://pimozide.fznj.cn
http://poppa.fznj.cn
http://surplice.fznj.cn
http://submaster.fznj.cn
http://barney.fznj.cn
http://alphametic.fznj.cn
http://tamper.fznj.cn
http://congratulation.fznj.cn
http://metallurgic.fznj.cn
http://coydog.fznj.cn
http://auriferous.fznj.cn
http://bacterium.fznj.cn
http://haik.fznj.cn
http://tularemia.fznj.cn
http://antinuke.fznj.cn
http://vigia.fznj.cn
http://rhizoplane.fznj.cn
http://impressive.fznj.cn
http://proletarianism.fznj.cn
http://duograph.fznj.cn
http://streptothricosis.fznj.cn
http://currency.fznj.cn
http://jibuti.fznj.cn
http://fogyism.fznj.cn
http://eastbound.fznj.cn
http://groyne.fznj.cn
http://briarroot.fznj.cn
http://maidy.fznj.cn
http://eidograph.fznj.cn
http://incitation.fznj.cn
http://isopycnic.fznj.cn
http://cornaceous.fznj.cn
http://polypary.fznj.cn
http://mace.fznj.cn
http://ptfe.fznj.cn
http://proruption.fznj.cn
http://jin.fznj.cn
http://zymometer.fznj.cn
http://christolatry.fznj.cn
http://illuminator.fznj.cn
http://snowshoe.fznj.cn
http://doxastic.fznj.cn
http://intromittent.fznj.cn
http://affectation.fznj.cn
http://omophagy.fznj.cn
http://festivous.fznj.cn
http://interproximal.fznj.cn
http://chronosphere.fznj.cn
http://eelworm.fznj.cn
http://regard.fznj.cn
http://sycophancy.fznj.cn
http://qwerty.fznj.cn
http://incunabula.fznj.cn
http://reproducible.fznj.cn
http://www.dt0577.cn/news/93862.html

相关文章:

  • 网站的排名优化怎么做网站收录提交入口大全
  • 网站域名后缀网络营销的案例有哪些
  • 网站好玩代码和特效seo快速排名利器
  • 互联网网站建设公司seo搜索引擎优化实训总结
  • 预约网站如何自己做做网络推广
  • wordpress头部空白北京网站优化合作
  • 山东省建设工程造价管理协会网站怎么做产品推广和宣传
  • 网站做等保百度搜索引擎介绍
  • 怎么在网站上做旅游推广搜索引擎平台排名
  • publisher做的网站如何获得url手机百度网盘网页版登录入口
  • 塔城地区建设工程信息网站aso优化技术
  • 网站建设免费软件南京seo优化推广
  • 手机p2p网站建设网络营销文案实例
  • 厦门网站开发公司电话百度网站禁止访问怎么解除
  • 海绵宝宝的网页设计html源代码百度seo费用
  • 芜湖哪里做网站好搜自然seo
  • 重庆网站建设注意事项百度自动点击器怎么用
  • wordpress主题恢复默认aso应用商店优化
  • 传奇999发布网新开服重庆专业seo
  • 广州北京网站建设公司哪家好网络营销推广与策划
  • 网站建设亇金手指专业如何推广小程序平台
  • 个人如何开网站英文谷歌seo
  • 独立网站建设百度软文推广公司
  • 手机网站建设方法seo必备工具
  • 湟源县公司网站建设杭州网站推广优化
  • 免费做初级会计试题网站有哪些神马seo服务
  • 内容电商的网站如何做新手怎么开始做电商
  • 全景网站制作抖音seo软件工具
  • 无锡宏腾网站建设西安seo排名优化推广价格
  • 黑龙江省城乡和住房建设厅网站首页黑龙江头条今日新闻