`

基于amoeba的mysql分布式数据库学习(一)

 
阅读更多

一、 下载 amoeba 代码

 

首先先到网站( http://sourceforge.net/projects/amoeba )上下载 amoeba for Mysql  代码。然后解压到

C:/amoeba  目录。增加系统环境变量: amoeba.home = C:/amoeba

 

二、 准备 mysql 数据库

 

Server1   localhost schema: test table: test_table2

Server2   10.2.224.241 schema: test table: test_table2

        

          表名称 test_table1 结构为:

                      `ID` INTEGER(11) NOT NULL,

                    `NAME` VARCHAR(250) COLLATE gbk_chinese_ci DEFAULT NULL,

        

          先插入一些模拟数据。

 

三、 修改配置文件

 

    找到amoeba.xml配置,修改mysql代理服务器配置信息:

 

Java代码   收藏代码
  1. <server>  
  2.     <!-- proxy server绑定的端口 -->  
  3.     <property name="port" > 8066 </property>  
  4.       
  5.     <!-- proxy server绑定的IP -->  
  6.     <property name="ipAddress" > 127.0 . 0.1 </property>  
  7.       
  8.     <!-- proxy server net IO Read thread size -->  
  9.     <property name="readThreadPoolSize" > 20 </property>  
  10.       
  11.     <!-- proxy server client process thread size -->  
  12.     <property name="clientSideThreadPoolSize" > 30 </property>  
  13.       
  14.     <!-- mysql server data packet process thread size -->  
  15.     <property name="serverSideThreadPoolSize" > 30 </property>  
  16.       
  17.     <!-- socket Send and receive BufferSize(unit:K)  -->  
  18.     <property name="netBufferSize" > 100 </property>  
  19.       
  20.     <!-- Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). -->  
  21.     <property name="tcpNoDelay" > true </property>  
  22.       
  23.     <!-- 对外验证的用户名 -->  
  24.     <property name="user" >root</property>  
  25.       
  26.     <!-- 对外验证的密码 -->  
  27.     <property name="password" >admin</property>  
  28.       
  29. </server>  

 

 

 

     我设置的监控端口为:8066。

 

    配置两个数据库服务器地址,分别对应Server1和Server2。

  

    修改查询规则文件:

 

 

Java代码   收藏代码
  1. <tableRule name= "test_table2"  schema= "test"  defaultPools= "server2,server1" >  
  2.       
  3.   
  4.     <rule name="rule1" >  
  5.         <parameters>ID</parameters>  
  6.         <expression><![CDATA[  ID <= 20  ]]></expression>  
  7.         <defaultPools>server1</defaultPools>  
  8.         <readPools>server1</readPools>  
  9.         <writePools>server1</writePools>  
  10.     </rule>  
  11.   
  12.     <rule name="rule2" >  
  13.         <parameters>ID</parameters>  
  14.         <expression><![CDATA[ ID > 20  ]]></expression>  
  15.         <defaultPools>server2</defaultPools>  
  16.         <writePools>server2</writePools>  
  17.         <readPools>server2</readPools>  
  18.     </rule>  
  19.                </tableRule>   

 

 

 

    规则:以ID=20为界。

 

 

 

     OK,配置文件配置好了以后,我们可以执行:${amoeba.home}/bin/amoeba.bat ,启动代理服务器。

 

四、 编写查询代码

 

  

Java代码   收藏代码
  1. import  java.sql.Connection;  
  2. import  java.sql.DriverManager;  
  3. import  java.sql.PreparedStatement;  
  4. import  java.sql.ResultSet;  
  5. import  java.sql.SQLException;  
  6. import  java.sql.Statement;  
  7.   
  8. public   class  DataAccess {  
  9.   
  10.     private  String CONNECTION_STRING =  "jdbc:mysql://localhost:8066/test" ;  
  11. //  jdbc:mysql://localhost:8066   
  12.     private  String connErrInfo;  
  13.       
  14.     private  Connection conn;  
  15.       
  16.     public  DataAccess(){  
  17.         if (conn ==  null ){  
  18.             conn = GetConnObj();  
  19.         }  
  20.     }  
  21.   
  22.     public  Connection GetConnObj() {  
  23.         try  {  
  24.             Class.forName("com.mysql.jdbc.Driver" );  
  25.             conn = DriverManager.getConnection(CONNECTION_STRING,"root" , "admin" );  
  26.             return  conn;  
  27.         } catch  (ClassNotFoundException ex) {  
  28.             this .connErrInfo +=  ";dbConn ex:"  + ex.toString();  
  29.             ex.printStackTrace();  
  30.         } catch  (SQLException es) {  
  31.             this .connErrInfo +=  ";dbConn es:"  + es.getMessage();  
  32.             es.printStackTrace();  
  33.         } catch  (Exception e) {  
  34.             this .connErrInfo +=  ";dbConn e:"  + e.getMessage();  
  35.             e.printStackTrace();  
  36.         }  
  37.         return   null ;  
  38.     }  
  39.   
  40.     public  String commonUpdate(String rSqlString) {  
  41.         if  (conn !=  null ) {  
  42.             try  {  
  43.                 Statement stmt = conn.createStatement();  
  44.                 stmt.execute(rSqlString);  
  45.                 //conn.close();   
  46.             } catch  (SQLException e) {  
  47.                 return  e.getMessage();  
  48.             }  
  49.         }  
  50.         return   "" ;  
  51.     }  
  52.   
  53.       
  54.       
  55.     public  ResultSet commonSelect(String rSqlString) {  
  56.         if  (conn !=  null ) {  
  57.             try  {  
  58.                 Statement stmt = conn.createStatement();  
  59.                 stmt.execute(rSqlString);  
  60.                 ResultSet result = stmt.executeQuery(rSqlString);  
  61.                 //conn.close();   
  62.                 return  result;  
  63.             } catch  (SQLException es) {  
  64.                 this .connErrInfo =  "dbProcess es:"  + es.getMessage();  
  65.             } catch  (Exception e) {  
  66.                 this .connErrInfo =  "dbProcess e:"  + e.getMessage();  
  67.             }  
  68.         }  
  69.         return   null ;  
  70.     }  
  71.   
  72.       
  73.     public   void  close(){  
  74.         if (conn !=  null ){  
  75.             try  {  
  76.                 conn.close();  
  77.             } catch  (SQLException e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.         }  
  81.     }  
  82.       
  83.     public  String getConnErrInfo() {  
  84.         return  connErrInfo;  
  85.     }  
  86.   
  87.     public   void  setConnErrInfo(String connErrInfo) {  
  88.         this .connErrInfo = connErrInfo;  
  89.     }  
  90.   
  91.     public   static   void  main(String[] args)  throws  SQLException{  
  92.           
  93.         DataAccess dataAccess = new  DataAccess();  
  94.           
  95.         java.util.Date startDate = new  java.util.Date();  
  96.         ResultSet rs = dataAccess.commonSelect("select * from test_table2 where ID in(14,15,16,50)" );  
  97.         while (rs.next()){  
  98.             String siteName = (String)rs.getString("name" );  
  99.             System.out.println("siteName:"  + siteName );  
  100.         }  
  101.         java.util.Date endDate = new  java.util.Date();  
  102.         long  period = endDate.getTime() - startDate.getTime();  
  103.         System.out.println("耗费时间:"  + period);  
  104.           
  105.         dataAccess.close();  
  106.     }  
  107.   
  108.     public  Connection getConn() {  
  109.         return  conn;  
  110.     }  
  111.   
  112.     public   void  setConn(Connection conn) {  
  113.         this .conn = conn;  
  114.     }  
  115. }  

 

 

 

五、 查询结果

 

Java代码   收藏代码
  1. siteName: 10.2 . 224 .241_test_table1_14  
  2. siteName:10.2 . 224 .241_test_table1_15  
  3. siteName:10.2 . 224 .241_test_table1_16  
  4. siteName:test_table2_14  
  5. siteName:test_table2_15  
  6. siteName:test_table2_16  
  7. 耗费时间:156   

 

 

我现在只是模拟简单的规则查询,后面我们将逐步深入了解。

分享到:
评论

相关推荐

    Amoeba:分布式数据库Proxy解决方案

    提供mysql数据库读写分离的最佳方案 减轻mysql数据库压力,使用分布式数据库。为企业提供最佳解决方案。

    amoeba分布式数据库解决方案

    Amoeba在分布式数据库领域将致力解决数据切分,应付客户端“集中式”处理分布式数据。这 儿集中式是一个相对概念,客户端不需要知道某种数据的物理存储地。避免这种逻辑出现在业务端, 大大简化了客户端操作分布式...

    Amoeba for mysql

    Amoeba for MySQL致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当query 路由功能,专注 分布式数据库 proxy 开发。座落与Client、DB Server(s)之间。对客户端透明。具有负载均衡、高可用性...

    amoeba-mysql

    Amoeba在分布式数据库领域将致力解决数据切分,应付客户端“集中式”处理分布式数据

    amoeba-mysql-binary-2.2.0.tar.gz

    Amoeba for MySQL致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当query 路由功能,专注 分布式数据库 proxy 开发。座落与Client、DB Server(s)之间。对客户端透明。具有负载均衡、高可用性...

    amoeba-mysql-1.0.2-BETA.zip_SQL 高并发_mysql-proxy java_代理_分布式java_

    Amoeba属于分布式数据库代理开发框架,专注于分布式数据库 proxy 开发。座落与Client、DB Server(s)之间。对客户 端透明。具有负载均衡、高可用性、sql过滤、读写分离、可路由相关的query到目标数据库、可并发请 求...

    MySQL分布式集群之MyCAT权威指南

    Cobar是由 Alibaba 开源的 MySQL 分布式处理中间件,它可以在分布式的环境下看上去像传统数据库一样提供海量数据服务。 Cobar自诞生之日起, 就受到广大程序员的追捧,但是自2013年后,几乎没有后续更新。在此情况下...

    windows平台用amoeba实现读写分离

    这个软件基于Java致力于MySQL的分布式数据库前端代理层,处于在应用和数据库之间,对客户端透明,它主要在应用层访问MySQL的时候充当SQL路由功能,解析应用传递过来的SQL语句,专注于分布式数据库代理层(Database ...

    2017最新老男孩MySQL高级专业DBA实战课程全套【清晰不加密】,看完教程月入40万没毛病

    03-mysql主从复制介绍及分布式数据库架构实现介绍.avi 04-主从同步的应用场景及切换从库不丢数据多方案介绍.avi 05-mysql数据库读写分离介绍及企业生产实现方案.avi 06-根据企业业务拆分业务应用到不同的从库思想....

    amoeba-mysql-3.0.4-BETA.tar.gz 实现集群管理 读写分离

    Amoeba主要解决以下问题: a). 数据切分后复杂数据源整合 b). 提供数据切分规则并降低数据切分规则给数据库带来的影响 c). 降低数据库与客户端连接 d). 读写分离路由 通过Amoeba实现读写分离

Global site tag (gtag.js) - Google Analytics