之前使用的数据库连接池 是 ali 的Druid ,不过springboot 默认是使用Hikari 作为数据库连接池的,听说要比Druid 更优秀一些。
正常我们使用Druid 和spring 的jdbcTemplate 一起操作,不过 JDBCTemplate 好像不是纯面向对象的数据库操作,拼写sql ,总是感觉 弱弱的,也容易出错
还有就是 mybatis ,这个吧,要为每个sql template 都要配置一下,有点冗余啰嗦
这个时候,不得不提 jooq ,超级强大,拿来主义,dao 层不用手写,可以使用插件主动生成相关的orm 映射的代码,现在建议使用 jooq 3.11.5 版本,网上很多是3.9 或3.8,3.11.5版本的已经不兼容低版本了,生成的代码不匹配,
最后研究得出,springboot hikari 和jooq 作为数据库的操作框架是比较合适的
有一点 好像是hikari可以不需要怎么配置就可以直接在springboot中开启的,不过
你需要在application.properties 配置文件里 声明 hikari 的相关配置
首先 创建springboot 的项目 , 其中MongoDB consul hystrix zipikin sleuth 大家不需要引用
pom 引入相关依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>mymodeldb</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>muller-modeldb</name>
<description>mysql as database for modeldb</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<thrift_version>0.10.0</thrift_version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M1</spring-cloud.version>
<jooq.version>3.11.5</jooq.version>
<mongo.version>3.8.2</mongo.version>
<spring.cloud.starter.version>2.0.1.RELEASE</spring.cloud.starter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jooq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
<!--<version>2.1.0.RELEASE</version>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<!--<version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<!--<version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>commons-math</groupId>
<artifactId>commons-math</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<scope>compile</scope>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>${thrift_version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>${mongo.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>${mongo.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>${mongo.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>${spring.cloud.starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring.cloud.starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>${spring.cloud.starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>${spring.cloud.starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>${spring.cloud.starter.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<profiles>
<profile>
<!-- mvn -PDBGen generate-sources -->
<id>DBGen</id>
<build>
<plugins>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>3.11.5</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://cdhcode:3306/modeldb_test</url>
<user>muller</user>
<password>7104</password>
</jdbc>
<generator>
<database>
<name>org.jooq.util.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>modeldb_test</inputSchema>
</database>
<target>
<!-- The destination package of your generated classes (within the
destination directory) -->
<packageName>jooq.mysql.gen</packageName>
<!-- The destination directory of your generated classes -->
<directory>target/generated-sources/jooq</directory>
</target>
<generate>
<pojos>true</pojos>
<daos>true</daos>
<deprecated>false</deprecated>
</generate>
</generator>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
然后在 application.properties 中配置 hikari 和jooq
spring.jooq.sql-dialect=mysql
#hikari datasource pool
spring.datasource.hikari.jdbc-url=jdbc:mysql://${mysql_host}:3306/modeldb_test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
spring.datasource.hikari.username=muller
spring.datasource.hikari.password=7104
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=60000
spring.datasource.hikari.max-lifetime=1765000
spring.datasource.hikari.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.allow-pool-suspension=true
spring.datasource.hikari.pool-name=modeldb_ds_pool
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/modeldb_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=muller
spring.datasource.password=7104
然后是 写一个Bean ,来初始化 Hikari 和jooq ,注意 要在springboot的启动配置类
加 扫描包的注解 ,不然可能启动不了,
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.DriverManager;
@Configuration
@Component
public class MySqlDBConfig {
protected static final Logger logger=LoggerFactory.getLogger(MySqlDBConfig.class);
private DSLContext ctx;
// @Autowired
private HikariDataSource hikariDataSource;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String userName;
@Value("${spring.datasource.password}")
private String password;
// @Autowired
// private HikariConfig hikariConfig;
//
// @Bean
// public HikariDataSource getHikariDataSource( ){
//
// this.hikariDataSource=new HikariDataSource(hikariConfig);
// return this.hikariDataSource;
// }
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public HikariConfig getConfig( ){
HikariConfig config =new HikariConfig( );
return config;
}
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
public HikariDataSource dataSource() throws Exception{
// this.hikariDataSource=(HikariDataSource) DataSourceBuilder.create().type(HikariDataSource.class).build();
this.hikariDataSource=new HikariDataSource(getConfig());
logger.info("hikari datasource pool session create succesfully "+hikariDataSource.getConnection().toString());
return this.hikariDataSource;
}
@Bean
public DSLContext dslContext( ) throws Exception{
// this.hikariDataSource=(HikariDataSource) DataSourceBuilder.create().type(HikariDataSource.class).build();
// logger.info("hikari datasource pool session create succesfully ");
// return this.hikariDataSource;
// Connection conn = DriverManager.getConnection(url, userName, password);
Connection conn=this.hikariDataSource.getConnection();
logger.info("use hikari datasource pool "+conn.toString());
this.ctx=DSL.using(conn,SQLDialect.MYSQL);
logger.info("jooq username: "+userName+ " pwd :"+password+ " url : "+url );
logger.info(" jooq dsl create successfully ");
return this.ctx;
}
// @Bean(name = "primaryDataSource")
// @Qualifier("primaryDataSource")
// @Primary
// @ConfigurationProperties(prefix = "spring.datasource.primary")
// public DruidDataSource primaryDatasource() {
// logger.info("database primary connection config is parsed by druid!!!");
// return DruidDataSourceBuilder.create().build();
// }
}
//import edu.mit.csail.db.ml.main.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"edu.mit.*"})
public class MullerModeldbApplication {
public static void main(String[] args)throws Exception {
SpringApplication.run(MullerModeldbApplication.class, args);
// Main mainz=new Main();
// mainz.main(args);
}
}
至此 基本配置ok,注意写正确的配置,其中最关键的是 HikariConfig 的注入,只有这里把配置文件的Hikari的配置注入成功了,后面的Hikari 和jooq 才能配置成功,springboot 对Hikari的封装还是很不错的,
@Bean
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public HikariConfig getConfig( ){
HikariConfig config =new HikariConfig( );
return config;
}
这种书写方式,可以正确注入
MongoDB
另外 MongoDB 在 springboot 有专门的 MongoDBTemplate ,
首先得配置项
#mongodb
spring.data.mongodb.host=${mongo_env_ip}
spring.data.mongodb.port=27017
spring.data.mongodb.password=
spring.data.mongodb.database=modeldb_metadata
spring.data.mongodb.grid-fs-database=
spring.data.mongodb.uri=mongodb://${mongo_env_ip}:27017/modeldb_metadata
写的Bean
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Component;
@Component
@Configuration
@EnableMongoRepositories
public class MongoDBConfig extends AbstractMongoConfiguration {
protected final Logger logger=LoggerFactory.getLogger(MongoDBConfig.class);
@Value("${spring.data.mongodb.host}")
private String mongoHost; // = "localhost";
@Value("${spring.data.mongodb.port}")
private int mongoPort ; //= 27017;
@Value("${spring.data.mongodb.database}")
private String dbName ;//= "modeldb_metadata";
private static final String MONGO_BASE_PACKAGE = "com.tz.config";
@Autowired
private ApplicationContext appContext;
@Override
protected String getDatabaseName() {
return dbName;
}
// @Override
// public Mongo mongo() {
// MongoClient mongoClient = new MongoClient(mongoHost, mongoPort);
// return mongoClient;
// }
@Bean
@Override
public MongoClient mongoClient() {
MongoClient mongoClient = new MongoClient(mongoHost, mongoPort);
logger.info("Mongodb client session create succesfully : ");
return mongoClient;
}
@Override
protected String getMappingBasePackage() {
return MONGO_BASE_PACKAGE;
}
@Override
@Bean(name = "mongoTemplate")
@Qualifier("mongoTemplate")
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate= new MongoTemplate(mongoClient(), getDatabaseName());
MongoDatabase db= mongoTemplate.getDb();
logger.warn(db.toString());
return mongoTemplate;
}
}
另外 thrift 的配置也可以作为bean 来管理
@Bean
public TThreadPoolServer createThriftserver() throws Exception{
TServerTransport transport = new TServerSocket(thrift_post);
logger.info(" thrift config : username : "+userName+" pwd "+password+" metadbhost: "+metadataDbHost+" url "+url);
// We use the binary protocol.
metadataDb.open();
TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
TTransportFactory transportFactory = new TFramedTransport.Factory();
TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(transport)
.processor(new ModelDBService.Processor<ModelDbServer>(new ModelDbServer(
// userName,
// password,
// url,
// ModelDbConfig.DatabaseType.MYSQL,
// metadataDbHost,
// metadataDbPort,
// metadataDbName,
// ModelDbConfig.MetadataDbType.MONGODB
this.ctx,
this.metadataDb
)))
.protocolFactory(protocolFactory)
.transportFactory(transportFactory)
.minWorkerThreads(1)
.maxWorkerThreads(100);
logger.info("begin");
TThreadPoolServer server = new TThreadPoolServer(serverArgs);
// Launch the server.
System.out.printf("Starting the simple server on port %d...\n", thrift_post);
server.serve();
return server;
}
网友评论