Merge pull request #23 from kissss/change-datasource-

Change datasource poll and upgrade mysql version
This commit is contained in:
纳兰丶 2018-07-12 14:13:18 +08:00 committed by GitHub
commit e8a5e15c9b
15 changed files with 80 additions and 173 deletions

View File

@ -73,10 +73,9 @@
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>

View File

@ -17,20 +17,16 @@
package com.hmily.tcc.admin.spi;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.base.Splitter;
import com.hmily.tcc.admin.service.CompensationService;
import com.hmily.tcc.admin.service.compensate.FileCompensationServiceImpl;
import com.hmily.tcc.admin.service.compensate.JdbcCompensationServiceImpl;
import com.hmily.tcc.admin.service.compensate.MongoCompensationServiceImpl;
import com.hmily.tcc.admin.service.compensate.RedisCompensationServiceImpl;
import com.hmily.tcc.admin.service.compensate.ZookeeperCompensationServiceImpl;
import com.hmily.tcc.admin.service.compensate.*;
import com.hmily.tcc.common.jedis.JedisClient;
import com.hmily.tcc.common.jedis.JedisClientCluster;
import com.hmily.tcc.common.jedis.JedisClientSingle;
import com.hmily.tcc.common.serializer.ObjectSerializer;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
@ -42,7 +38,6 @@ import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.core.MongoClientFactoryBean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
@ -79,22 +74,16 @@ public class CompensationConfiguration {
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(env.getProperty("compensation.db.driver"));
dataSource.setUrl(env.getProperty("compensation.db.url"));
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setDriverClassName(env.getProperty("compensation.db.driver"));
hikariDataSource.setJdbcUrl(env.getProperty("compensation.db.url"));
//用户名
dataSource.setUsername(env.getProperty("compensation.db.username"));
hikariDataSource.setUsername(env.getProperty("compensation.db.username"));
//密码
dataSource.setPassword(env.getProperty("compensation.db.password"));
dataSource.setInitialSize(2);
dataSource.setMaxActive(20);
dataSource.setMinIdle(0);
dataSource.setMaxWait(60000);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(false);
dataSource.setTestWhileIdle(true);
dataSource.setPoolPreparedStatements(false);
return dataSource;
hikariDataSource.setPassword(env.getProperty("compensation.db.password"));
hikariDataSource.setMinimumIdle(5);
hikariDataSource.setMaximumPoolSize(10);
return hikariDataSource;
}
@Bean

View File

@ -19,8 +19,15 @@ package com.hmily.tcc.common.config;
import lombok.Data;
import javax.sql.DataSource;
import java.util.Map;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* 采用数据库保存事务日志配置时,数据库配置信息.
*
* @author xiaoyu
*/
@Data
@ -46,66 +53,64 @@ public class TccDbConfig {
*/
private String password;
/**
* 初始化时建立物理连接的个数初始化发生在显示调用init方法或者第一次getConnection时.
*/
private int initialSize = 10;
/**
* 最大连接池数量.
*
* https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing
*/
private int maxActive = 100;
private int maxActive = 20;
/**
* 最小连接池数量.
*/
private int minIdle = 20;
private int minIdle = 10;
/**
* 配置获取连接等待超时的时间.
* This property controls the maximum number of milliseconds that a client (that's you) will wait for a connection from the pool.
* If this time is exceeded without a connection becoming available, a SQLException will be thrown.
* Lowest acceptable connection timeout is 250 ms.
* Default: 30000 (30 seconds)
*/
private int maxWait = 60000;
private long connectionTimeout = SECONDS.toMillis(30);
/**
* 配置间隔多久才进行一次检测检测需要关闭的空闲连接单位是毫秒.
* This property controls the maximum amount of time that a connection is allowed to sit idle in the pool.
* This setting only applies when minimumIdle is defined to be less than maximumPoolSize.
* Idle connections will not be retired once the pool reaches minimumIdle connections.
* Whether a connection is retired as idle or not is subject to a maximum variation of +30 seconds,
* and average variation of +15 seconds. A connection will never be retired as idle before this timeout.
* A value of 0 means that idle connections are never removed from the pool. The minimum allowed value is 10000ms (10 seconds).
* Default: 600000 (10 minutes)
*/
private int timeBetweenEvictionRunsMillis = 60000;
private long idleTimeout = MINUTES.toMillis(10);
/**
* 配置一个连接在池中最小生存的时间单位是毫秒.
* This property controls the maximum lifetime of a connection in the pool. An in-use connection will never be
* retired, only when it is closed will it then be removed. On a connection-by-connection basis, minor negative
* attenuation is applied to avoid mass-extinction in the pool. We strongly recommend setting this value, and it
* should be several seconds shorter than any database or infrastructure imposed connection time limit. A value of
* 0 indicates no maximum lifetime (infinite lifetime), subject of course to the idleTimeout setting.
* Default: 1800000 (30 minutes)
*/
private int minEvictableIdleTimeMillis = 300000;
private long maxLifetime = MINUTES.toMillis(30);
private String validationQuery = " SELECT 1 ";
/**
* 申请连接时执行validationQuery检测连接是否有效做了这个配置会降低性能.
* If your driver supports JDBC4 we strongly recommend not setting this property. This is for "legacy" drivers that
* do not support the JDBC4 Connection.isValid() API. This is the query that will be executed just before a connection
* is given to you from the pool to validate that the connection to the database is still alive. Again, try running
* the pool without this property, HikariCP will log an error if your driver is not JDBC4 compliant to let you know.
* Default: none
*/
private Boolean testOnBorrow = false;
private String connectionTestQuery;
/**
* 归还连接时执行validationQuery检测连接是否有效做了这个配置会降低性能.
* Add a property (name/value pair) that will be used to configure the {@link DataSource}/{@link java.sql.Driver}.
*/
private Boolean testOnReturn = false;
/**
* 建议配置为true不影响性能并且保证安全性申请连接的时候检测.
* 如果空闲时间大于timeBetweenEvictionRunsMillis.
* 执行validationQuery检测连接是否有效.
*/
private Boolean testWhileIdle = true;
/**
* 是否缓存preparedStatement也就是PSCache.
* PSCache对支持游标的数据库性能提升巨大比如说oracle在mysql下建议关闭.
*/
private Boolean poolPreparedStatements = false;
/**
* 要启用PSCache必须配置大于0当大于0时poolPreparedStatements自动触发修改为true.
* 在Druid中.
* 不会存在Oracle下PSCache占用内存过多的问题可以把这个数值配置大一些比如说100.
*/
private int maxPoolPreparedStatementPerConnectionSize = 100;
private Map<String, Object> dataSourcePropertyMap;
}

View File

@ -43,13 +43,11 @@
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>

View File

@ -17,7 +17,6 @@
package com.hmily.tcc.core.spi.repository;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Maps;
import com.hmily.tcc.common.bean.entity.Participant;
import com.hmily.tcc.common.bean.entity.TccTransaction;
@ -31,6 +30,7 @@ import com.hmily.tcc.common.utils.DbTypeUtils;
import com.hmily.tcc.common.utils.RepositoryPathUtils;
import com.hmily.tcc.core.helper.SqlHelper;
import com.hmily.tcc.core.spi.CoordinatorRepository;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -54,7 +54,7 @@ public class JdbcCoordinatorRepository implements CoordinatorRepository {
private Logger logger = LoggerFactory.getLogger(JdbcCoordinatorRepository.class);
private DruidDataSource dataSource;
private HikariDataSource dataSource;
private String tableName;
@ -194,22 +194,21 @@ public class JdbcCoordinatorRepository implements CoordinatorRepository {
@Override
public void init(final String modelName, final TccConfig txConfig) {
dataSource = new DruidDataSource();
final TccDbConfig tccDbConfig = txConfig.getTccDbConfig();
dataSource.setUrl(tccDbConfig.getUrl());
dataSource = new HikariDataSource();
dataSource.setJdbcUrl(tccDbConfig.getUrl());
dataSource.setDriverClassName(tccDbConfig.getDriverClassName());
dataSource.setUsername(tccDbConfig.getUsername());
dataSource.setPassword(tccDbConfig.getPassword());
dataSource.setInitialSize(tccDbConfig.getInitialSize());
dataSource.setMaxActive(tccDbConfig.getMaxActive());
dataSource.setMinIdle(tccDbConfig.getMinIdle());
dataSource.setMaxWait(tccDbConfig.getMaxWait());
dataSource.setValidationQuery(tccDbConfig.getValidationQuery());
dataSource.setTestOnBorrow(tccDbConfig.getTestOnBorrow());
dataSource.setTestOnReturn(tccDbConfig.getTestOnReturn());
dataSource.setTestWhileIdle(tccDbConfig.getTestWhileIdle());
dataSource.setPoolPreparedStatements(tccDbConfig.getPoolPreparedStatements());
dataSource.setMaxPoolPreparedStatementPerConnectionSize(tccDbConfig.getMaxPoolPreparedStatementPerConnectionSize());
dataSource.setMaximumPoolSize(tccDbConfig.getMaxActive());
dataSource.setMinimumIdle(tccDbConfig.getMinIdle());
dataSource.setConnectionTimeout(tccDbConfig.getConnectionTimeout());
dataSource.setIdleTimeout(tccDbConfig.getIdleTimeout());
dataSource.setMaxLifetime(tccDbConfig.getMaxLifetime());
dataSource.setConnectionTestQuery(tccDbConfig.getConnectionTestQuery());
if (tccDbConfig.getDataSourcePropertyMap() != null && !tccDbConfig.getDataSourcePropertyMap().isEmpty()) {
tccDbConfig.getDataSourcePropertyMap().forEach((key, value) -> dataSource.addDataSourceProperty(key, value));
}
this.tableName = RepositoryPathUtils.buildDbTableName(modelName);
//save current database type
this.currentDBType = DbTypeUtils.buildByDriverClassName(tccDbConfig.getDriverClassName());

View File

@ -86,13 +86,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
<version>5.1.46</version>
</dependency>
<dependency>

View File

@ -82,13 +82,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
<version>5.1.46</version>
</dependency>
<dependency>

View File

@ -100,13 +100,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
<version>5.1.46</version>
</dependency>
<dependency>

View File

@ -104,16 +104,9 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -10,20 +10,6 @@ spring:
url: jdbc:mysql://192.168.1.68:3306/tcc_account?useUnicode=true&characterEncoding=utf8
username: xiaoyu
password: Wgj@555888
initialSize: 20
minIdle: 20
maxActive: 100
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
application:
name: account-service

View File

@ -101,7 +101,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
<version>5.1.46</version>
</dependency>
<dependency>
@ -109,13 +109,6 @@
<artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -9,22 +9,6 @@ spring:
url: jdbc:mysql://192.168.1.68:3306/tcc_stock?useUnicode=true&characterEncoding=utf8
username: xiaoyu
password: Wgj@555888
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 20
minIdle: 20
maxActive: 100
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
application:
name: inventory-service

View File

@ -110,13 +110,7 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
<version>5.1.46</version>
</dependency>
<dependency>

View File

@ -10,20 +10,6 @@ spring:
url: jdbc:mysql://192.168.1.68:3306/tcc_order?useUnicode=true&characterEncoding=utf8
username: xiaoyu
password: Wgj@555888
initialSize: 20
minIdle: 20
maxActive: 100
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 30000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,slf4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
application:
name: order-service

View File

@ -69,11 +69,11 @@
<httpclient.version>4.5.1</httpclient.version>
<okhttp.version>3.7.0</okhttp.version>
<gson.verions>2.6.2</gson.verions>
<druid.version>1.0.29</druid.version>
<springfox.version>2.6.1</springfox.version>
<lombok.version>1.16.14</lombok.version>
<motan.version>1.0.0</motan.version>
<disruptor.version>3.4.0</disruptor.version>
<hikaricp.version>3.2.0</hikaricp.version>
</properties>
@ -155,9 +155,9 @@
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikaricp.version}</version>
</dependency>
<dependency>
@ -354,7 +354,6 @@
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>