说到它,是真的深受广大程序员的喜爱,它封装了很多优秀api,其强大的wrapper构造器,以至于单表操作几乎不用书写sql语句,简捷的分页构造条件,默认支持的返回体基类等,推荐大家有时间一定要去官网看看有很多特性不妨一试~
下面这篇文章就是我之前写的,这篇文章主要是针对业务不同,我们是同样的业务不同的人在使用,也就是说代码要跟着不同的人设置的数据源进行不同的数据响应以达到高可用效果,大家有时间就看看咯,文章地址如下
不同用户动态切换数据源
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<!--Spring-boot 核心类-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>datasource-test</artifactId>
<dependencies>
<!--数据库支持依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!--mybatis封装依赖-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!--连接池使用阿里巴巴Druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
<!--SpringBoot web端支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--单体boot支持启动-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--动态数据源自行切换Mybatis-plus支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<!--引入该项目中所需xml配置文件支持-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<!--maven编译器指定jdk版本-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
server:
port: 7777
spring:
#基础连接四项配置
datasource:
dynamic:
primary: master #默认数据源
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/bladex?characterEncoding=utf-8
other: #从数据源
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://-:3306/bladex?characterEncoding=utf-8
druid:
initial-size: 10 #初始化10个连接
max-active: 100 #最大连接100
min-idle: 10 #最小连接10
max-wait: 60000 #获取连接最大等待时间(单位:毫秒)
validation-query: select 1 from dual
test-on-borrow: false #获取连接时是否监测连接有效(不建议开启)
test-on-return: false #归还连接时是否检测连接有效(不建议开启)
test-while-idle: true #申请连接时检测,如果空闲时间大于time-between-eviction-runs-millis即执行检测
time-between-eviction-runs-millis: 300000 # 检测并销毁
min-evictable-idle-time-millis: 300000 #连接在池中有效时间
filters: stat,wall #监控统计携带日志级别
stat-view-servlet:
enabled: true #开启监控统计
login-username: xuewenliang
login-password: 123456
reset-enable: false #不清空统计数据
web-stat-filter:
enabled: true #开启单个监控url调用的sql列表 (默认开启)
url-pattern: /* #映射规则到任何URL
exclusions: '*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*' #排除任何规则
session-stat-enable: true #开启session统计
session-stat-max-count: 100 #设置session统计最大值
#排除Druid连接池默认配置以及JDBC管理都是自定义形式
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure,org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
#服务名
application:
name: datasource-server
#IP
eureka:
instance:
hostname: localhost
#本例做单独服务不被注册中心发现
client:
fetch-registry: false
register-with-eureka: false
mybatis-plus:
configuration:
map-underscore-to-camel-case: true #开启驼峰命名规则支持
cache-enabled: false
#扫描mapper持久层接口
mapper-locations: classpath:com.xwl/**/mapper/*Mapper.xml
package com.itxwl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Auther: 薛
* @Date: 2020/7/18 15:19
* @Description:
*/
@SpringBootApplication
public class DataSourceApplication {
public static void main(String[] args) {
SpringApplication.run(DataSourceApplication.class,args);
}
}
package com.itxwl.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.itxwl.domain.TfEmail;
import com.itxwl.service.IDataSourceService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther: 薛
* @Date: 2020/7/18 15:25
* @Description:
*/
@RestController
@AllArgsConstructor
@RequestMapping("/test")
public class DataSourceController {
private IDataSourceService dataSourceService;
@GetMapping("/findSourceData/{id}")
public R<TfEmail> findSourceData(@PathVariable("id") Integer id){
return R.ok(dataSourceService.findSourceData(id));
}
}
package com.itxwl.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.itxwl.domain.TfEmail;
import com.itxwl.service.IDataSourceService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Auther: 薛
* @Date: 2020/7/18 15:25
* @Description:
*/
@RestController
@AllArgsConstructor
@RequestMapping("/test")
public class DataSourceController {
private IDataSourceService dataSourceService;
@GetMapping("/findSourceData/{id}")
public R<TfEmail> findSourceData(@PathVariable("id") Integer id){
return R.ok(dataSourceService.findSourceData(id));
}
}
package com.itxwl.service;
import com.itxwl.domain.TfEmail;
public interface IDataSourceService {
TfEmail findSourceData(Integer id);
}
package com.itxwl.service.impl;
import com.itxwl.domain.TfEmail;
import com.itxwl.mapper.DataSourceMapper;
import com.itxwl.service.IDataSourceService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
/**
* @Auther: 薛
* @Date: 2020/7/20 13:33
* @Description:
*/
@Service
@AllArgsConstructor
public class DataSourceServiceImpl implements IDataSourceService {
private DataSourceMapper dataSourceMapper;
@Override
public TfEmail findSourceData(Integer id) {
//如果是1调用基础库-也就是本身的默认库
if (id.equals(1)){
return findSourceDatas();
}
return findSourceDatasT();
}
public TfEmail findSourceDatas(){
return dataSourceMapper.findServer();
}
public TfEmail findSourceDatasT(){
return dataSourceMapper.findServerT();
}
}
注意:DS注解的值就相当于application.yml配置里面的主从数据源标识参数,如果不写就是默认的master源
package com.itxwl.mapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itxwl.domain.TfEmail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* @Auther: 薛
* @Date: 2020/7/20 13:34
* @Description:
*/
@Mapper
public interface DataSourceMapper extends BaseMapper<TfEmail> {
@DS("master")
@Select("select * from bladex.tf_email")
TfEmail findServer();
@DS("other")
@Select("select * from bladex.tf_email")
TfEmail findServerT();
}
http://localhost:7777/druid/sql.html
该监控界面可以清楚的查看每次访问的峰值由此来分析业务的流畅度依次进行优化
链接:https://pan.baidu.com/s/19nBMMot5lJW971jb27zUjQ
提取码:cpqj
QQ:2509647976
WX:x331191249