前言 本篇主要分两部分 ①SpringBoot整合druid的代码配置,以及druid的监控页面演示;②对实际场景中多数据源的配置使用进行讲解。 一、springboot整合druid的演示demo 可以用idea快速生成一个可运行的dem
本篇主要分两部分 ①SpringBoot整合druid的代码配置,以及druid的监控页面演示;②对实际场景中多数据源的配置使用进行讲解。
可以用idea快速生成一个可运行的demo工程,具体可以参考如何快速创建springboot项目
org.springframework.boot spring-boot-starter-data-jdbc Mysql mysql-connector-java runtime com.alibaba druid 1.2.17 log4j log4j 1.2.17 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-WEB
spring: datasource: username: root passWord: root url: jdbc:mysql://localhost:3306/firsttest?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource poolPreparedStatements: true #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入 #如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
package com.czing.jdbcdemo.config;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.support.Http.StatViewServlet;import com.alibaba.druid.support.http.WebStatFilter;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.web.servlet.FilterReGIStrationBean;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.servlet.Servlet;import javax.sql.DataSource;import java.util.Arrays;import java.util.HashMap;import java.util.Map;@Configurationpublic class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource(){ return new DruidDataSource(); } @Bean public ServletRegistrationBean druidServletRegistrationBean(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid@RestControllerpublic class JDBCController { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/selectTest") public List
项目启动日志:
druid的监控页面访问 http://localhost:8080/druid/sql.html
访问编写的controller测试类:http://localhost:8080/selectTest(访问之后可以观察druid监控页面的情况)
修改application.yml文件
spring: datasource: local: username: root password: root #注意多数据源配置的时候此处为jdbc-url jdbc-url: jdbc:mysql://localhost:3306/firsttest?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource remote: username: root password: root jdbc-url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource
编写多数据源的配置类:
package com.czing.jdbcdemo.config;import com.czing.jdbcdemo.mult.DataSourceType;import com.czing.jdbcdemo.mult.DynamicDataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;@Configurationpublic class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.remote") public DataSource remoteDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.local") public DataSource localDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dataSource(DataSource remoteDataSource, DataSource localDataSource) { Map
使用自定义注解,在查询方法的时候指定对应的数据源:
package com.czing.jdbcdemo.mult;public enum DataSourceType { REMOTE, LOCAL}
package com.czing.jdbcdemo.mult;public class DynamicDataSourceContextHolder { private static final ThreadLocal CONTEXT_HOLDER = new ThreadLocal<>(); public static void setDataSourceType(String dataSourceType){ System.out.printf("切换到{%s}数据源", dataSourceType); CONTEXT_HOLDER.set(dataSourceType); } public static String getDataSourceType(){ return CONTEXT_HOLDER.get(); } public static void clearDataSourceType(){ CONTEXT_HOLDER.remove(); }}
package com.czing.jdbcdemo.mult;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import javax.sql.DataSource;import java.util.Map;public class DynamicDataSource extends AbstractRoutingDataSource { public DynamicDataSource(DataSource defaultTargetDataSource, Map
package com.czing.jdbcdemo.config;import com.czing.jdbcdemo.mult.DataSourceType;import com.czing.jdbcdemo.mult.DynamicDataSource;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;@Configurationpublic class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.remote") public DataSource remoteDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties("spring.datasource.local") public DataSource localDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "dynamicDataSource") @Primary public DynamicDataSource dataSource(DataSource remoteDataSource, DataSource localDataSource) { Map
package com.czing.jdbcdemo.mult;import java.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface DataSource { DataSourceType value() default DataSourceType.REMOTE;}
package com.czing.jdbcdemo.mult;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Aspect@Order(1)@Componentpublic class DataSourceAspect { @Pointcut("@annotation(com.czing.jdbcdemo.mult.DataSource)") public void dsPointCut() { } @Around("dsPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { MethodSignature signature = (MethodSignature) point.getSignature(); Method method = signature.getMethod(); DataSource dataSource = method.getAnnotation(DataSource.class); if (dataSource != null) { DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name()); } try { return point.proceed(); } finally { // 销毁数据源 在执行方法之后 DynamicDataSourceContextHolder.clearDataSourceType(); } }}
package com.czing.jdbcdemo.controller;import com.czing.jdbcdemo.mult.DataSource;import com.czing.jdbcdemo.mult.DataSourceType;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestControllerpublic class JDBCController { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/selectTest") @DataSource(value=DataSourceType.LOCAL) public List> getTestAccount(){ String sql ="select * from account"; List> maps = jdbcTemplate.queryForList(sql); return maps; } @RequestMapping("/selectRemote") @DataSource(DataSourceType.REMOTE) public List> getRemoteAccount(){ String sql ="select * from account"; List> maps = jdbcTemplate.queryForList(sql); return maps; }}
http://localhost:8080/selectTest 的访问数据:
http://localhost:8080/selectRemote 的访问数据
以上为springboot实现druid配置查询的demo,并且演示了如何实现多数据源配置动态切换的代码实现,注意多数据源配置的时候url的配置为jdbc-url,其他运行中遇到的问题欢迎留言交流,希望对您的学习有所帮助。
来源地址:https://blog.csdn.net/weixin_48583915/article/details/130631197
--结束END--
本文标题: springboot整合druid及多数据源配置
本文链接: https://www.lsjlt.com/news/441763.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-03
2024-04-03
2024-04-01
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0