栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot+MyBatis+Nacos配置多数据源,MySQL和Redis多数据源怎么配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot+MyBatis+Nacos配置多数据源,MySQL和Redis多数据源怎么配置

配置多数据源,由于不常用就进行了百度,百度了很久(有很多都不能用),加上自己修修改改,终于能用了。那么就废话不多说,直接上代码。

  1. SpringBoot应用启动类注解:
@MapperScan("com.xxx.mapper")
@SpringBootApplication(scanbasePackages = "com.xxx")

MapperScan是配置到了DAO层的包上。不过这里的MapperScan应该不起作用。配上了也就没必要删掉。

  1. bootstrap-env.yaml配置Nacos:
spring:
  cloud:
    nacos:
      config:
        server-addr: nacos-ip:nacos-port
        namespace: my_namespace
        group: MY_GROUP
        file-extension: yaml
  1. Nacos配置:
server:
  port: 8060

spring:
  datasource:
    one:
      jdbc-url: jdbc:mysql://mysql-host-1:3306/database?useSSL=false&Unicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&useAffectedRows=true
      username: username
      password: password
      driver-class-name: com.mysql.cj.jdbc.Driver
    two:
      jdbc-url: jdbc:mysql://mysql-host-2:3306/database?useSSL=false&Unicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL&useAffectedRows=true
      username: username
      password: password
      driver-class-name: com.mysql.cj.jdbc.Driver
  
  redis:
    one: 
      database: 0
      host: redis-host-1
      port: 6379
      password: password
    two: 
      database: 1
      host: redis-host-2
      port: 6379
      password: password

这里要注意Nacos配置文件的命名:应用名.yaml,要加yaml哦,否则可能读不到配置。
4. 数据库Bean的配置:

@Configuration
@MapperScan(basePackages = "com.xxx.mapper.one", sqlSessionFactoryRef = "oneSqlSessionFactory")
public class OneDataSourceConfig {

    @Primary	// 这个注解意思是主库,只有一个配置类可以加
    @Bean("oneDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DataSource getOneDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("oneSqlSessionFactory")
    public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/one
@Configuration
@MapperScan(basePackages = "com.nucarf.rebate.calculate.mapper.two", sqlSessionFactoryRef = "twoSqlSessionFactory")
public class TwoDataSourceConfig {

    @Bean("twoDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.two")
    public DataSource getTwoDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("twoSqlSessionFactory")
    public SqlSessionFactory twoSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/two
    @Value("${spring.redis.one.host}")
    private String oneHost;
    @Value("${spring.redis.one.password}")
    private String onePassword;
    @Value("${spring.redis.one.port}")
    private String onePort;
    @Value("${spring.redis.one.database}")
    private int oneDatabase;

    
    @Value("${spring.redis.two.host}")
    private String twoHost;
    @Value("${spring.redis.two.password}")
    private String twoPassword;
    @Value("${spring.redis.two.port}")
    private String twoPort;
    @Value("${spring.redis.two.database}")
    private int twoDatabase;

    //最大空闲连接数
    private static final int MAX_IDLE = 8;
    //最大连接数
    private static final int MAX_TOTAL = 8;
    //建立连接最长等待时间
    private static final long MAX_WAIT_MILLIS = 10000;

    
    public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
                                                    int maxTotal, long maxWaitMillis, int index) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(port);

        if (StringUtils.isNotEmpty(password)) {
            jedisConnectionFactory.setPassword(password);
        }

        if (index != 0) {
            jedisConnectionFactory.setDatabase(index);
        }

        jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    
    public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }

    
    @Bean(name = "redisTemplateOne")
    public RedisTemplate redisTemplateOne(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(
                connectionFactory(oneHost, Integer.parseInt(onePort), onePassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, oneDatabase));
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    
    @Bean(name = "redisTemplateTwo")
    public RedisTemplate redisTemplateTwo(RedisConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(
                connectionFactory(twoHost, Integer.parseInt(twoPort), twoPassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, twoDatabase));
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
  1. 使用
    数据库使用没啥说的,直接@Autowired或者@Resource注入就可以了。
    Redis使用注入的时候把name写上:
@Resource(name = "redisTemplateOne")
private RedisTemplate redisTemplateOne;

创作很累,点赞免费。 自愿打赏,不为利往。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/630996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号