Mysql read availability and application integration mycat, to achieve separation myCat

Antecedent Review

By the previous two blog posts: Mycat - read and write database separate from the high availability and Mycat - to achieve high availability and load balancing, full of dry goods! We completed the deployment assembly shown below

Mysql read availability and application integration mycat, to achieve separation myCat

A device structure of FIG.


SQL request to the VIP, VIP keepalived complete mapping and forwarding the request by lvs mycat, mycat according to the type of SQL request (the SELECT SQL or SQL the DML, or also enforce the node db) will be distributed to specific SQL db, accomplished by specific SQL database service to complete the execution.

But just to stay deployed in the database level, not integrated our application has no real meaning, then how do we integrate our applications, to achieve mycat's mission?

Application Integration

If mycat build a good, application integration is very simple, let's step by step to achieve application integration in a variety of situations

Mysql availability of the separate read and write

Separate read and write database can be implemented in the code level (refer: spring integrated mybatis achieve mysql separate read and write), but not recommended, the core responsibilities of the code should be to achieve the business, if a large piece of the code used to implement the database read write separation and high availability, it would be a departure from the original intention, widely divergent.

Computer field have a saying: "Any problem in computer science can be solved by adding an indirect middle layer." Since our code directly docking database to read and write well separated from the high-availability database, then add a layer of middleware to achieve in the middle, creating a database middleware (mycat just realize one), and application code directly docking an intermediate database, read and write to the database by the separation of the highly available database middleware. At this time, the following components of the structure of FIG.

Mysql read availability and application integration mycat, to achieve separation myCat

FIG two structural components


Specific deployment process may refer to: Mycat - database read and write separation and high availability, application integrated at this time how? In fact, very simple, just to address our database connection pool configuration can be changed to address the mycat (mycat as the database), as follows

application.yml

server:
 port: 8886
spring:
 #连接池配置
 datasource:
 type: com.alibaba.druid.pool.DruidDataSource
 druid:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql://192.168.1.212:8066/TESTDB?useSSL=false&useUnicode=true&characterEncoding=utf-8
 username: root
 password: 123456
 initial-size: 1 #连接池初始大小
 max-active: 20 #连接池中最大的活跃连接数
 min-idle: 1 #连接池中最小的活跃连接数
 max-wait: 60000 #配置获取连接等待超时的时间
 pool-prepared-statements: true #打开PSCache,并且指定每个连接上PSCache的大小
 max-pool-prepared-statement-per-connection-size: 20
 validation-query: SELECT 1 FROM DUAL
 validation-query-timeout: 30000
 test-on-borrow: false #是否在获得连接后检测其可用性
 test-on-return: false #是否在连接放回连接池后检测其可用性
 test-while-idle: true #是否在连接空闲一段时间后检测其可用性
#mybatis配置
mybatis:
 type-aliases-package: com.lee.mycat.entity
 #config-location: classpath:mybatis/mybatis-config.xml
 mapper-locations: classpath:mybatis/*.xml
# pagehelper配置
pagehelper:
 helperDialect: mysql
 #分页合理化,pageNum<=0则查询第一页的记录;pageNum大于总页数,则查询最后一页的记录
 reasonable: true
 supportMethodsArguments: true
 params: count=countSql
logging:
 level:
 com.lee.mycat.mapper: DEBUG

UserWeb.java

package com.lee.mycat.web;
import com.lee.mycat.entity.User;
import com.lee.mycat.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/mycat")
public class UserWeb {
 @Autowired
 private IUserService userService;
 @RequestMapping("/getUserByNameFromMasterDb")
 public User getUserByNameFromMasterDb(String name) {
 return userService.getUserByNameFromMasterDb(name);
 }
 @RequestMapping("/getUserByNameFromSlaveDb")
 public User getUserByNameFromSlaveDb(String name) {
 return userService.getUserByNameFromSlaveDb(name);
 }
 @RequestMapping("/getUserByName")
 public User getUserByName(String name) {
 return userService.getUserByName(name);
 }
 @RequestMapping("/addUser")
 public Integer addUser(String name, Integer age) {
 return userService.insertUser(new User(name, age));
 }
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lee.mycat.mapper.UserMapper">
 <sql id="Base_Column_List">
 id,name,age
 </sql>
 <select id="getUserByNameFromMasterDb" resultType="User" parameterType="String">
 /*!mycat:db_type=master*/ SELECT
 <include refid="Base_Column_List" />
 FROM
 tbl_user
 WHERE name=#{name}
 </select>
 <select id="getUserByNameFromSlaveDb" resultType="User" parameterType="String">
 /*!mycat:db_type=slave*/ SELECT
 <include refid="Base_Column_List" />
 FROM
 tbl_user
 WHERE name=#{name}
 </select>
 <select id="getUserByName" resultType="User" parameterType="String">
 SELECT
 <include refid="Base_Column_List" />
 FROM
 tbl_user
 WHERE name=#{name}
 </select>
 <insert id="insertUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
 INSERT INTO
 tbl_user(name, age)
 VALUES
 (#{name}, #{age})
 </insert>
</mapper>

UserMapper.xml文件中会与我们平时的写法有些许不同,有时候需要明确指定强制走master还是slave节点。具体细节可查看:spring-boot-mycat

测试结果

Mysql read availability and application integration mycat, to achieve separation myCat



如上图所示,我们一开始新增了一个用户:Jiraiye,其年龄是50,我们手动改了mysql slave中Jiraiye的年龄为52是为了更直观的验证SQL请求最终走的是mysql master还是mysql slave。从上图可知,一般的Select SQL走的是从库(DML SQL走主库这个就不用说了),如在mapper.xml中强制指定了db节点,那么就会在指定的mysql节点上来执行SQL。

mysql的高可用就没进行测试了,应用其实是感知不到的;mysql master宕机了,mycat会按我们配置好的进行mysql db的切换,正常服务于我们的应用。

Mycat的高可用

mysql的读写分离与高可用我们是实现了,可mycat却存在高可用问题,一旦mycat宕机了,整个数据库层就相当于宕机了。可想而知,我们需要实现mycat的高可用。

mycat的高可用搭建过程可参考:Mycat - 高可用与负载均衡实现,满满的干货!,此时的组件结构图如下

Mysql read availability and application integration mycat, to achieve separation myCat

组件结构图三

应用工程改动非常小,只需要将数据库连接配置的url改成VIP即可,如下

jdbc:mysql://192.168.1.212:8066/TESTDB?useSSL=false&useUnicode=true&characterEncoding=utf-8
改成
jdbc:mysql://192.168.1.200:8066/TESTDB?useSSL=false&useUnicode=true&characterEncoding=utf-8

测试结果

Mysql read availability and application integration mycat, to achieve separation myCat


Mysql to read and write separation is still working properly, when mycat master downtime, mycat slave to take over the task of forwarding sql to achieve a high availability of mycat; there has been an exception in a very short time period, it is because the database connection pool mycat are connected on the 212, 212 now is down, so there will be an unusually prompt, but the connection pool immediately responded by re-establishing a database connection, then the connection pool is connected 110.

Mycat load balancing

Availability mycat above, the vast majority of cases, mycat slave has been in a wait state, it does not provide any services, because our mycat master general is not downtime. Is there any approach that allows slave also handles SQL requests, but master and mutual support to achieve high availability mycat it? That is the load balancing Mycat, mycat master-slave relationship does not exist at this time, but Talia pairwise prepared, this time is a configuration diagram of the assembly of a device structure of FIG. Application engineering do not change, configuration database connection or VIP. Specific not demonstrated, we have to go to practice.

to sum up

1, the database middleware can reduce the complexity of application code, allowed to achieve full-time business code, and database-level work to database middleware; mycat just a database middleware implementation, but also to achieve relatively good, she is open source.

2, concurrent amount is not high, high availability can mycat, without the need of load balancing Mycat; load balancing mycat require more hardware and maintenance costs, but there is no qualitative change in revenue, it is cost-effective For, falling instead of rising.

3, specifically what components need to be deployed into a structure, you need to look at specific needs, in many cases never use mycat middleware, if used the mycat middleware, personally I think the best is still to achieve high availability mycat, as to or need to achieve mycat load balancing, to see a specific amount of concurrency, and this is also not a standard, it is necessary to troubleshoot the actual situation is not mycat load is too high, and if indeed mycat load is too high, then it is necessary to achieve the mycat load balancing to reduce the load of the single mycat. There is no absolute optimal deployment, deploying only the most appropriate moment.


Guess you like

Origin blog.51cto.com/14455981/2436880