在Spring / J2EE应用程序中分离只读和读写 [英] Segregating the read-only and read-write in Spring/J2EE Apps

查看:119
本文介绍了在Spring / J2EE应用程序中分离只读和读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在项目中使用Spring,Spring-Data和JPA。

We using Spring, Spring-Data and JPA in our project.

对于生产服务器,我们希望设置数据库集群,使所有读取查询都定向到一个服务器和所有写入查询都被定向到另一个服务器。

For production servers, we would like to setup database cluster such that all read queries are directed to one server and all write queries are directed to another server.

这显然需要DAO的构建方式进行一些更改。

This obviously will require some changes in the way the DAOs are built.

有没有人知道如何实现这一点,到目前为止,使用Spring-Data / JPA跟踪厨师书风格的DAO创作,其中DAO实现负责读写操作?需要什么样的架构改变来隔离两种类型的调用?

Does anyone know how to achieve this if one has, so far, been following cook-book style DAO creations using Spring-Data/JPA where a DAO implementation is responsible for both reads and writes? What kind of changes in architecture will be needed to segregate the two types of calls?

推荐答案

使用MySQL时,常见的Java开发人员使用 Connector / J 作为JDBC驱动程序(因为这是官方的JDBC来自MySQL的驱动程序)。开发人员通常使用 com.mysql.jdbc.Driver 类作为驱动程序,其中包含一个URL,如 jdbc:mysql:// host [:port ] /数据库

When using MySQL, it is common for Java developers to use Connector/J as the JDBC driver (since this is the official JDBC driver from MySQL). Developers typically use the com.mysql.jdbc.Driver class as the driver, with a URL such as jdbc:mysql://host[:port]/database.

连接器/ J提供另一个驱动程序,名为 ReplicationDriver ,允许应用程序在多个MySQL主机之间进行负载平衡。当使用 ReplicationDriver 时,JDBC URL更改为 jdbc:mysql:replication:// master-host [:master-port] [,slave-1 -host [:从属-1-端口]] [,从属-2-主机[:从属-2-端口] /数据库。这允许应用程序连接到多个服务器之一,具体取决于任何给定时间点可用的服务器。

Connector/J offers another driver called the ReplicationDriver that allows an application to load-balance between multiple MySQL hosts. When using ReplicationDriver, the JDBC URL changes to jdbc:mysql:replication://master-host[:master-port][,slave-1-host[:slave-1-port]][,slave-2-host[:slave-2-port]]/database. This allows the application to connect to one of multiple servers depending on which one is available at any given point in time.

使用 ReplicationDriver ,如果JDBC连接设置为只读,则驱动程序将URL中声明的第一个主机视为读取 - 主机和所有其他的只读主机。开发人员可以在Spring应用程序中利用这一点构建代码,如下所示:

When using the ReplicationDriver, if a JDBC connection is set to read-only, the driver treats the first host declared in the URL as a read-write host and all others as read-only hosts. Developers can take advantage of this in a Spring application by structuring their code as follows:

@Service
@Transactional(readOnly = true)
public class SomeServiceImpl implements SomeService {
   public SomeDataType readSomething(...) { ... }

   @Transactional(readOnly = false)
   public void writeSomething(...) { ... }
}

这样,每当调用方法 readSomething 时,Spring事务管理代码将获取JDBC Connection 并调用 setReadOnly(true),因为服务方法默认使用 @Transactional(readOnly = true)注释。这将使来自 readSomething 方法的所有数据库查询都转到其中一个非主机MySQL主机,以循环方式进行负载平衡。类似地,每当 writeSomething 被调用时,Spring将调用底层JDBC 上的 setReadOnly(false)连接,强制数据库查询转到主服务器。

With code like this, whenever the method readSomething is called, the Spring transaction management code will obtain a JDBC Connection and call setReadOnly(true) on it because the service methods are annotated with @Transactional(readOnly = true) by default. This will make all database queries from the readSomething method to go to one of the non-master MySQL hosts, load-balanced in a round-robin fashion. Similarly, whenever writeSomething is called, Spring will call setReadOnly(false) on the underlying JDBC Connection, forcing the database queries to go to the master server.

此策略允许应用程序将所有只读流量指向一个集的MySQL服务器和所有读写流量到不同的服务器,而无需更改应用程序的逻辑架构或开发人员不必担心不同的数据库主机和角色。

This strategy allows the application to direct all read-only traffic to one set of MySQL servers and all read-write traffic to a different server, without changing the application's logical architecture or the developers having to worry about different database hosts and roles.

这篇关于在Spring / J2EE应用程序中分离只读和读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆