Cyq. Data (V5) database read and write separation function usage experience

Source: Internet
Author: User
Tags tojson

Some words of thanks

First of all, nature is to thank "passing Autumn" to Cyq. Data is open source (not cyq.data, of course.) Look at his posture, it is ready to collect the Gold 12 Palace)

Passing Autumn Blog Address [There are more of his open source projects]:http://www.cnblogs.com/cyq1162/

The two QR codes on the left are his entrepreneurship and entrepreneurship projects [it apps], and interested friends can sweep code OH

--------------------------------------------------------------------------------------------------------------- ---------

I contacted Cyq. Data for a few days, in order to reconstruct the project, looking for an integrated data operation, distributed cache, database read and write separation framework. So, I saw ...

Last night, when testing the execution mechanism of read-write separation, some problems were encountered. So the first time to contact the fall himself. What I admire is, without further ado, one after the other to solve. I measured his change, two people toss to the middle of the night, just calculate the work. So I have this article

I want to say that in today's impetuous era, for a person who is not a stranger, can have such patience. It's rare.

Well, some feelings even if it's over, here is the dinner

Cyq. Data (V5) database read and write separation Getting Started

For some friends who have not used cyq.data to better understand, here first to say some basic

1: Configuration Rules

Taking MSSQL as an example

<add name="Conn"connectionstring="server=.; uid=sa;pwd=123456;database=db;"Providername="System.Data.SqlClient"/> <add name="Conn_bak"connectionstring="server=.; Uid=sa;pwd=123456;database=db_back;"Providername="System.Data.SqlClient"/> <add name="conn_slave1"connectionstring="server=.; uid=sa;pwd=123456;database=db_slave1;"Providername="System.Data.SqlClient"/> <add name="Conn_slave2"connectionstring="server=.; Uid=sa;pwd=123456;database=db_slave2;"Providername="System.Data.SqlClient"/> <add name="Conn_slave3"connectionstring="server=.; Uid=sa;pwd=123456;database=db_slave3;"Providername="System.Data.SqlClient"/>

which

Conn: Primary Library connection configuration [writable] (hereinafter referred to as the Main library )
Conn_bak: Backup number Library connection configuration [writable] (hereinafter referred to as the Standby library )
Conn_slave1 ... Conn_slaven: Connect configuration from library library [Read Only] (hereinafter referred to as from the library )

2: Execution mechanism

2.1: How to enable read-write separation
    The read-write separation mechanism is enabled when at least one [from library] item exists in the configuration section

2.2: Write operation
2.2.1: follow: master-Standby switching sequence
2.2.2: The main library and the standby are executed in Guthrie fallback order when writing. When the main library goes down, the framework automatically switches to the standby library
2.2.3: When the standby is also down, the write request is aborted. If you have configured from the library, do not worry, they will not be affected by any

2.3: Read operation
Follow: from ~ master ~ the order of preparation. The database of any one node unexpectedly, the framework will request the next valid connection by configuration item

3: General application Scenario

The most immediate purpose is of course to divert the database. We often say that the distributed database schema can be used as the data layer to complete. Operations similar to sub-libraries, tables, and so on, when you need multiple library operations in order to relieve the stress of the database.

4: simple Example analysis

For example, there is an article system, the core of the data is published articles. We can use the current very conventional way to generate static. html files to alleviate the real-time request pressure of the database, but the limit of the number of articles is not controllable, storage space, single-server load capacity these are not controllable factors.
At this point, our load balancer is ready to shine. Then there is a problem, since the deployment of multi-server load balancing, the database? What about the database?

The most brutal of course you can also use a single database directly. But obviously, we can have better solutions. This is: Data distributed architecture

We use the distribution of the primary database to subscribe to other databases to achieve the same data (full or specified) between servers.

The deployment of a distributed architecture presents a new requirement, and we need to contribute to every database, not just as a data backup. Based on this demand,cyq. Data will be perfect for your business needs

The simplest way: we can add cyq between the database-generated. HTML pages. Data. When a request is made, if the current server does not have a corresponding HTML page, fetch data from the database, generate the page, and return to the front end. [About page data changes that need to be rebuilt when done by other businesses, here we press no table]

When the page does not exist, we need to get the data from the database. But now that we have several databases, how can we reasonably get the data from these libraries? Very simple: Use cyq. Data, configure master-slave ~ from the rule can [configuration method and execution mechanism to look up]

5: Code Usage

Read data:

string json;                 using New Maction (" table name "))                {                    = action. Select (). ToJson ();                }

Configuration in config in the master ~ ~ from the node, the above code can easily get the data from the database of the specified table [about read and write separation in the execution mechanism of the reading operation, look up]; it's so simple, without any complicated business code, everything is cyq. Data helps you complete the

  Write data [Update for example]:

using New Maction (" table name "))                {                    action. SetExpression ("a=1");                    Action. Update ("id=1");                }

As with the read operation, after the node is configured, it is possible to update the data according to its execution mechanism. Say it again: everything is so simple

  except maction , the framework also provides Mproc to call the stored procedure. In a similar situation, we will not repeat

The above example is for the case of a master ~ prepare ~ from the same structure as the three libraries. What if the table from the library is inconsistent with the main library? For example, you need a request now: Table A, but there is no table A in the library, it may only exist in the master repository, and the result of the request will be unexpected. So for the sub-business module of the read and write separation we also need to have a corresponding solution; This is the egg and the bright spot today.

  Realization of sub-module read/write separation [sub-Library table]

  The implementation of the function is simple enough for you to imagine. The following example is configured to have a vertical slice of the article table, not with the main library. So the article series only saved the article from the Library

  1: Configuration

<add name="Articleconn"connectionstring="To set up a primary library connection"Providername="System.Data.SqlClient"/> <add name="articleconn_slave1"connectionstring="server=.; uid=sa;pwd=123456;database=articledb_slave1;"Providername="System.Data.SqlClient"/> <add name="Articleconn_slave2"connectionstring="server=.; uid=sa;pwd=123456;database=articledb_slave1;"Providername="System.Data.SqlClient"/> <add name="Articleconn_slave3"connectionstring="server=.; Uid=sa;pwd=123456;database=articledb_slave3;"Providername="System.Data.SqlClient"/>

2: Code

string json;                 // passing                configuration key:articleconn as a parameter // the next request will be articleconn as the main library, using the articleconn_slave1 in the configuration ... The Articleconn_slaven master-slave relationship realizes the read data                usingthenew maction ("article"  "articleconn"))                {                    = action. Select (). ToJson ();                }


An argument is passed in, and theArticleconn as the primary repository does not have any effect in reading the data. You can use the connection of the main library [Conn] without passing this parameter in the write operation

using New Maction ("article"))                {                    action. SetExpression ("a=1");                    Action. Update ("id=1");                }

  Modular read-Write separation requirements Scenario

take the off-the-shelf blog Park as an example, we can put users, articles , comments, three modules are stored separately. Each establishes the Master ~ Prepare ~ from the relationship. On this basis, we can split the data of each module horizontally, use statements or views to aggregate, such performance and efficiency, far from the single-machine library can be compared. Of course, the premise is that you have enough money~~~ hahaha

  In fact, I want to focus on the latter part of the day, but want to find out, in fact, everything is very powerful. So, finally, it's just a scenario-based way of listing it.

Finally, you have to say once: everything is so simple , what are you waiting for? Interested friends to the fall of the Great God's blog download Ah

Cyq. Data (V5) database read and write separation function usage experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.