本文共 9944 字,大约阅读时间需要 33 分钟。
相当值得阅读的一篇文档 https://blogs.oracle.com/jbalint/entry/accessing_fabric_ha_groups_from
It's been almost a year now since the initial release of MySQL Fabric. It is maturing and reached a GA state earlier this year as part of MySQL Utilities 1.4. Developers and architects are evaluating it for their applications, learning the capabilities of the system, and providing feedback for future features and usability enhancements. Connector/J has supported Fabric from the very beginning and continues to support the latest features. This blog post is an introductory level guide to using Fabric for high-availability from Java applications. Sharding features and Java APIs will be covered in a later post. The commands shown and feature descriptions are current for Fabric 1.5.1. In addition, Connector/J 5.1.32 and later support only Fabric 1.5 due to communication protocol differences from Fabric 1.4.
High-availability is a core concept in Fabric and is implemented by HA groups. An HA group is a set of servers configured with MySQL replication in a master/slaves topology. Fabric can take care of the replication configuration, so you only need to have the servers configured properly and Fabric will take care of the rest. The example included here will use three MySQL servers for the application; one master and two slaves. There is an additional database for the backing store for the Fabric node, but it's not considered from the application's perspective. If you plan to follow along, you should setup your Fabric node, MySQL backing store for Fabric and three MySQL 5.6 instances for the application.
You will need three server instances for the basic HA group. Binlogging is required for replication and GTIDs are required for Fabric. I configured my servers with the following properties:
# server id should be different for each instanceserver-id = 1# enable binloglog-bin = mysql-binlog-slave-updates = true# enable GTIDgtid-mode = onenforce-gtid-consistency = true
At this point, we have three isolated MySQL instances. We can use Fabric to arrange them into an HA group and setup the replication streams. Here are the Fabric steps needed to build the HA group:
$ mysqlfabric group create mygroupFabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249eTime-To-Live: 100 uuid finished success result------------------------------------ -------- ------- ------c99b4348-9b26-4d79-bccc-0ec42ca89e0e 1 1 1 state success when description----- ------- ------------- ------------------------------------------------------------- 3 2 1406082130.78 Triggered by. 4 2 1406082130.84 Executing action (_create_group). 5 2 1406082130.98 Executed action (_create_group).
$ mysqlfabric group add mygroup 192.168.1.100:3306$ mysqlfabric group promote mygroup$ mysqlfabric group lookup_groups
$ mysqlfabric group add mygroup 192.168.1.101:3306$ mysqlfabric group add mygroup 192.168.1.101:3306$ mysqlfabric group lookup_servers mygroup
Now that we've setup an HA group, we need to write a client to access it. Connector/J provides access to Fabric-managed farms with the same configuration approach used for single servers. A typical JDBC URL might look like:
jdbc:mysql://localhost/test?user=simon&password=secret
The above URL points to the test database on the localhost instance authenticating with the simonaccount. To access a Fabric-managed farm, the URL changes to something like:
jdbc:mysql:fabric://localhost/test?user=simon&password=secret& fabricUsername=app01&fabricPassword=secret&fabricServerGroup=mygroup
We've changed the prefix to include the "fabric" marker and added the username and password to authenticate to the Fabric node. The database is still test and we still authenticate to the MySQL instances as simon. The difference here is that Connector/J will request the set of servers in the HA group mygroup from the Fabric node. Connections to the individual servers will be made as necessary during the processing of transactions.
To illustrate how this is all put together, check out this sample code:
String url = "jdbc:mysql:fabric://localhost/test?user=simon&password=secret&" + "fabricUsername=app01&fabricPassword=secret&fabricServerGroup=mygroup";Connection con = DriverManager.getConnection(url);PreparedStatment ps = con.prepareStatement("select first_name from" + " employees where emp_no = ?");
You may notice that this is no different than writing to the standard JDBC APIs. However, a few benefits will come from using a Fabric-aware client:
In addition to property-based configuration, we can also use MySQL-specific APIs to change the way Connector/J handles Fabric connections. The connection object returned from the DriverManager can be cast to an instance of com.mysql.fabric.jdbc.FabricMySQLConnection. This interface provides to change the HA group and sharding properties of the connection. If you plan to access multiple HA groups from a single connection or wish to explore the sharding features, this is the API to use. Here's an illustration that queries tables from two different HA groups:
String url = "jdbc:mysql:fabric://localhost/test?user=simon&password=secret&" + "fabricUsername=app01&fabricPassword=secret";Connection con = DriverManager.getConnection(url);com.mysql.fabric.jdbc.FabricMySQLConnection fabricCon = (com.mysql.fabric.jdbc.FabricMySQLConnection) con;con.setServerGroupName("mygroup");PreparedStatment ps = con.prepareStatement("select first_name from " + "employees where empno = ?");// this is where you would deal with results, etccon.setServerGroupName("anothergroup");// proceed with additional JDBC code here accessing servers in "anothergroup"
Connector/J supports all features in Fabric and is quite flexible. There is a comprehensive section in the manual detailing the use of . The Connector/J package includes several demo programs in the src/demo/fabric directory. TheEmployeesJdbc program is a complete runnable example showing both HA and sharded setups.
As you've seen, Fabric is a novel method for managing and accessing farms of MySQL servers. Not only can it help configure and manage replication, but it provides an additional layer of abstraction for your application's need to access MySQL instances. The centralized configuration in Fabric means that you can easily make changes to your farm without having to worry about client configuration changes and additional application downtime. In addition to the high-availability features, Fabric provides a complete sharding solution built on HA groups. The Java APIs in Connector/J provide access to the sharding features and will be covered in a later post.
If you plan to attend the 2014 Oracle OpenWorld / JavaOne, check out the session on Tuesday afternoon. If you're using Connector/J, you might also be interested in the BOF later that evening.
转载地址:http://zpgmb.baihongyu.com/