import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
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;
/**
* @author robot
*/
@Configuration
public class DbConfiguration {
@Bean(name = "alarmDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource alarmDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "profileDataSource")
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource profileDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 告警中心的db
*/
@Bean(value = "alarmDb")
public Neo alarmDb(@Qualifier("alarmDataSource")DataSource alarmDataSource) {
try {
Neo neo = Neo.connect(alarmDataSource);
// 关闭 explain 解析
neo.setExplainFlag(false);
// 关闭 sql 耗时监控
neo.setMonitorFlag(false);
// 开启日志打印
// neo.setLogPrint(true);
// 关闭 sql 规范校验
neo.setStandardFlag(false);
return neo;
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).warn("neo初始化异常", e);
return new Neo();
}
}
/**
* 环境变量的db
*/
@Bean(value = "profileDb")
public Neo profileDb(@Qualifier("profileDataSource") DataSource profileDataSource) {
try {
Neo neo = Neo.connect(profileDataSource);
// 关闭 explain 解析
neo.setExplainFlag(false);
// 关闭 sql 耗时监控
neo.setMonitorFlag(false);
// 开启日志打印
// neo.setLogPrint(true);
// 关闭 sql 规范校验
neo.setStandardFlag(false);
return neo;
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).warn("neo初始化异常", e);
return new Neo();
}
}
}
spring:
application:
name: isc-alarm-service
datasource:
jdbcUrl: jdbc:mysql://xxx:xxxx/xxxx?useUnicode=true&characterEncoding=UTF-8&useAffectedRows=true&allowMultiQueries=true&useSSL=false&AppName=${spring.application.name}&AppVersion=1.0&AppSignature=NA&serverTimezone=Asia/Shanghai
username: xxx
password: xxx
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
auto-commit: true
idle-timeout: 180000
pool-name: panda.server-HikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
maximum-pool-size: 10
minimum-idle: 10
datasource2:
jdbcUrl: jdbc:mysql://xxxx:xxx/xxx?useUnicode=true&characterEncoding=UTF-8&useAffectedRows=true&allowMultiQueries=true&useSSL=false&AppName=${spring.application.name}&AppVersion=1.0&AppSignature=NA&serverTimezone=Asia/Shanghai
username: xxxx
password: xxxx
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
auto-commit: true
idle-timeout: 180000
pool-name: panda.server-HikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
maximum-pool-size: 10
minimum-idle: 10