美文网首页
SpringBoot框架搭建(redis多数据源+数据库多数据源

SpringBoot框架搭建(redis多数据源+数据库多数据源

作者: shendu2019 | 来源:发表于2019-01-23 10:18 被阅读0次

java骨架

目录结构

├── mvnw
├── mvnw.cmd
├── pom.xml
├── README.md
├── src/              源文件目录
│   ├── main
│   │   ├── java
│   │   │   └── cn
│   │   │       └── sigo
│   │   │           └── archtype
│   │   │               ├── ArchtypeApplication.java 启动类
│   │   │               ├── common/          公用参数配置目录
│   │   │               │   ├── ArchtypeConstantsConfig.java    公共参数配置
│   │   │               │   ├── CompareStatus.java
│   │   │               │   ├── Error.java
│   │   │               │   ├── ErrorCode.java
│   │   │               │   └──......
│   │   │               └── config/        公共配置目录
│   │   │                   ├── JwtAuthenticationFilter.java    解决前端到后台跨域问题
│   │   │                   ├── redis/    redis配置
│   │   │                   │   ├── DefaultRedisConfig.java
│   │   │                   │   ├── RedisConfig.java
│   │   │                   │   └── ......
│   │   │                   ├── controller/   控制器目录
│   │   │                   │   ├── TestController.java
│   │   │                   │   └── .....
│   │   │                   ├── mapper/          mapper 目录 
│   │   │                   │   └── .....
│   │   │                   ├── model/        Model 目录 (绑定数据表)
│   │   │                   │   ├── po/      此对象与数据库表结构一一对应,通过 mapper 层向上传输数据源对象(数据库)
│   │   │                   │   │   └── .....
│   │   │                   │   ├── dto/       数据传输对象, Service 或 Manager 向外传输的对象。(输出)
│   │   │                   │        └── .....
│   │   │                   │   ├── bo/      业务对象, 由 Service 层输出的封装业务逻辑的对象(业务处理,输入)
│   │   │                   │        └── .....
│   │   │                   │   ├── query/      数据查询对象,各层接收上层的查询请求。 注意超过 2 个参数的查询封装,禁止
                                                    使用 Map 类来传输(数据库逻辑处理)
│   │   │                   │        └── .....
│   │   │                   │   └── .....
│   │   │                   ├── service/      服务接口目录     
│   │   │                   │   ├── impl/     服务接口实现目录
│   │   │                   │       ├── TestServiceImpl.java
│   │   │                   │        └── .....
│   │   │                   │   ├── TestService.java
│   │   │                   │   └── .....
│   │   │                   └── utils/      公共工具类目录
│   │   │                        ├── DesUtil     DEC(ASC码加密)-带向量及密钥
│   │   │                        ├── EncrypDES     DEC(ASC码加密)-不带向量及密钥
│   │   │                        ├── JsonUtils     Json数据处理工具类
│   │   │                        ├── LogHelper     日志处理工具类
│   │   │                        ├── MD5Utils     MD5通用类
│   │   │                        ├── ObjectUtils     驼峰及下划线互转处理工具
│   │   │                        ├── RedisHelper     redis工具类
│   │   │                        ├── SendSmsUtil     发送短信工具类
│   │   │                        └── .....
│   │   └── resources/          资源总目录
│   │       ├── generator/ 
│   │       │   ├── generatorConfig.xml           代码自动生成工具
│   │       │   └── .....
│   │       ├── mapping/                           数据库sql配置文件
│   │       ├── static/                            静态资源文件目录
│   │       ├── templates/                         web 模板目录
│   │       ├── application-dev.yml                开发配置
│   │       ├── application-prod.yml               产品配置
│   │       ├── application.yml                    当前配置
│   │       ├── application-test.yml               测试配置
│   │       └── archtype.properties                公共参数properties

Redis多数据源

将redis数据库信息配置到application-test.yml,application-prod.yml,application-dev.yml配置文件中,
如业务需求需要连接多个redis数据源,配置多个DefaultRedisConfig.java文件。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author: panwei
 * @Date: 2019/1/22 11:58
 * @apiNote: Redis数据连接池及公共参数
 * @Version 1.0
 */
@EnableCaching
@Configuration
public class RedisConfig {
    @Value("${spring.redis.jedis.pool.max-active}")
    private int redisPoolMaxActive;

    @Value("${spring.redis.jedis.pool.max-wait}")
    private int redisPoolMaxWait;

    @Value("${spring.redis.jedis.pool.max-idle}")
    private int redisPoolMaxIdle;

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int redisPoolMinIdle;


    /**
     * 创建redis连接工厂
     *
     * @param dbIndex
     * @param host
     * @param port
     * @param password
     * @param timeout
     * @return
     */
    public JedisConnectionFactory createJedisConnectionFactory(int dbIndex, String host, int port, String password, int timeout) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setDatabase(dbIndex);
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(port);
        jedisConnectionFactory.setPassword(password);
        jedisConnectionFactory.setTimeout(timeout);
        jedisConnectionFactory.setPoolConfig(setPoolConfig(redisPoolMaxIdle, redisPoolMinIdle, redisPoolMaxActive, redisPoolMaxWait, true));
        return jedisConnectionFactory;
    }

    /**
     * 设置连接池属性
     *
     * @param maxIdle
     * @param minIdle
     * @param maxActive
     * @param maxWait
     * @param testOnBorrow
     * @return
     */
    public JedisPoolConfig setPoolConfig(int maxIdle, int minIdle, int maxActive, int maxWait, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxWaitMillis(maxWait);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }

    /**
     * 设置RedisTemplate的序列化方式
     *
     * @param redisTemplate
     */
    public void setSerializer(RedisTemplate redisTemplate) {
        StringRedisSerializer stringRedisSerializer =new StringRedisSerializer();
        //设置键(key)的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //设置值(value)的序列化方式
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.afterPropertiesSet();
    }
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Author: panwei
 * @Date: 2019/1/22 11:58
 * @apiNote: 默认的redis数据源
 * @Version 1.0
 */
@Configuration
@EnableCaching
public class DefaultRedisConfig extends RedisConfig {
    @Value("${spring.redis.database}")
    private int dbIndex;

    @Value("${spring.redis.redis1.host}")
    private String host;

    @Value("${spring.redis.redis1.port}")
    private int port;

    @Value("${spring.redis.redis1.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    /**
     * 配置redis连接工厂
     *
     * @return
     */
    @Bean
    public RedisConnectionFactory defaultRedisConnectionFactory() {
        return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
     *
     * @return
     */
    @Bean(name = "defaultRedisTemplate")
    public RedisTemplate defaultRedisTemplate() {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(defaultRedisConnectionFactory());
        setSerializer(template);
        template.afterPropertiesSet();
        return template;
    }
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Author: panwei
 * @Date: 2019/1/22 11:58
 * @apiNote: redis数据源-其他
 * @Version 1.0
 */
@Configuration
@EnableCaching
public class CacheRedisConfig extends RedisConfig {
    @Value("${spring.redis.database}")
    private int dbIndex;

    @Value("${spring.redis.redis2.host}")
    private String host;

    @Value("${spring.redis.redis2.port}")
    private int port;

    @Value("${spring.redis.redis2.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private int timeout;

    /**
     * 配置redis连接工厂
     *
     * @return
     */
    @Primary
    @Bean
    public RedisConnectionFactory cacheRedisConnectionFactory() {
        return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
     *
     * @return
     */
    @Bean(name = "cacheRedisTemplate")
    public RedisTemplate cacheRedisTemplate() {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(cacheRedisConnectionFactory());
        setSerializer(template);
        template.afterPropertiesSet();
        return template;
    }
}
  • 具体例子请查看TestController.java,其中也包含了切换的db方法。
  • 注意DefaultRedisConfig.java中@Bean对应的name必须唯一
 redis:
      #数据库索引
      database: 0
      redis1:
        host: 192.168.5.58
        port: 6379
        password:
      redis2:
        host: 192.168.5.87
        port: 6379
        password:
      jedis:
        pool:
          #最大连接数
          max-active: 8
          #最大阻塞等待时间(负数表示没限制)
          max-wait: -1
          #最大空闲
          max-idle: 8
          #最小空闲
          min-idle: 0
      #连接超时时间
      timeout: 10000
      keytimeout: 10000

数据库多数据源

项目中配置了两个mysql数据库和一个sqlserver数据库的数据源连接,均采用druid的连接方式
sqlserver:
  datasource:
    url: jdbc:sqlserver://10.0.180.152\ERPDB152;database=SigoBackEnd
    username: sa
    password: sigo.cn@123
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
mysql:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=GMT%2B8
    username: root
    password: 2wsx@WSX
    driverClassName: com.mysql.jdbc.Driver
  • 具体的数据库Config配置方法请查看MysqlDataSourceConfig.javaMysqlDataSourceConfig1.javaSqlServerDataSourceConfig.java
    import com.alibaba.druid.pool.DruidDataSource;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    
    import javax.sql.DataSource;
    
    /**
     * @Author: panwei
     * @Date: 2019/1/23 11:58
     * @apiNote: Mysql扫描 Mapper 接口并容器管理
     * @Version 1.0
     */
    @Configuration
    @MapperScan(basePackages = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
    public class MysqlDataSourceConfig {
        // 精确到 mysql 目录,以便跟其他数据源隔离
    
        /**
         * 对应的mysql dao层包
        */
        static final String PACKAGE = "cn.sigo.api.mapper.mysql";
    
        /**
         * 对应mysql的mapper.xml文件
         */
        static final String MAPPER_LOCATION = "classpath:mapping/mysql/*.xml";
    
        @Value("${spring.datasource.url}")
        private String url;
    
        @Value("${spring.datasource.username}")
        private String user;
    
        @Value("${spring.datasource.password}")
        private String password;
    
        @Value("${spring.datasource.driver-class-name}")
        private String driverClass;
    
        @Bean(name = "mysqlDataSource")
        @Primary
        public DataSource mysqlDataSource() {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName(driverClass);
            dataSource.setUrl(url);
            dataSource.setUsername(user);
            dataSource.setPassword(password);
            return dataSource;
        }
    
        @Bean(name = "mysqlTransactionManager")
        @Primary
        public DataSourceTransactionManager mysqlTransactionManager() {
            return new DataSourceTransactionManager(mysqlDataSource());
        }
    
        @Bean(name = "mysqlSqlSessionFactory")
        @Primary
        public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDataSource) throws Exception {
            final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
            sessionFactory.setDataSource(mysqlDataSource);
            sessionFactory.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION));
            return sessionFactory.getObject();
        }
    }
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Author: panwei
 * @Date: 2019/1/23 13:56
 * @apiNote: sqlserver扫描 Mapper 接口并容器管理
 * @Version 1.0
 */
@Configuration
@MapperScan(basePackages=SqlServerDataSourceConfig.PACKAGE,sqlSessionFactoryRef="sqlserverSqlSessionFactory")
public class SqlServerDataSourceConfig {
    // 精确到 sqlserver 目录,以便跟其他数据源隔离

    static final String PACKAGE = "cn.sigo.api.mapper.sqlserver";
    static final String MAPPER_LOCATION = "classpath:mapping/sqlserver/*.xml";

    @Value("${sqlserver.datasource.url}")
    private String url;

    @Value("${sqlserver.datasource.username}")
    private String user;

    @Value("${sqlserver.datasource.password}")
    private String password;

    @Value("${sqlserver.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "sqlserverDataSource")
    public DataSource sqlserverDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "sqlserverTransactionManager")
    public DataSourceTransactionManager sqlserverTransactionManager() {
        return new DataSourceTransactionManager(sqlserverDataSource());
    }

    @Bean(name = "sqlserverSqlSessionFactory")
    public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource sqlserverDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(sqlserverDataSource);
        sessionFactory.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(SqlServerDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

全局异常捕获

相关文章

网友评论

      本文标题:SpringBoot框架搭建(redis多数据源+数据库多数据源

      本文链接:https://www.haomeiwen.com/subject/sgrrjqtx.html