refactoring imdg settings
This commit is contained in:
parent
7d55b4cb1b
commit
c28ad05c6e
6 changed files with 98 additions and 23 deletions
|
|
@ -0,0 +1,73 @@
|
||||||
|
package ru.spcex.clearing.imdg.config;
|
||||||
|
|
||||||
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import ru.spcex.clearing.imdg.config.element.DatabaseSettings;
|
||||||
|
import ru.spcex.clearing.imdg.config.element.ImdgSettings;
|
||||||
|
import ru.spcex.clearing.imdg.error.ModuleInitializeException;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.sql.Connection;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnnecessaryLocalVariable")
|
||||||
|
@Configuration
|
||||||
|
public class DbConnectionConfig {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
private final DatabaseSettings settings;
|
||||||
|
|
||||||
|
public DbConnectionConfig(ImdgSettings settings) {
|
||||||
|
this.settings = settings.getDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() {
|
||||||
|
DataSource result;
|
||||||
|
String login = settings.getLogin();
|
||||||
|
String password = settings.getPassword();
|
||||||
|
String logTimeoutPart = "";
|
||||||
|
String dbPath = settings.getUrl();
|
||||||
|
int timeoutSec = 30;
|
||||||
|
|
||||||
|
ComboPooledDataSource cpds = new ComboPooledDataSource();
|
||||||
|
try {
|
||||||
|
cpds.setDriverClass("org.postgresql.Driver");
|
||||||
|
} catch (Exception ue) {
|
||||||
|
throw new RuntimeException(ue);
|
||||||
|
}
|
||||||
|
cpds.setJdbcUrl(dbPath);
|
||||||
|
cpds.setUser(login);
|
||||||
|
cpds.setPassword(password);
|
||||||
|
cpds.setInitialPoolSize(10);
|
||||||
|
cpds.setMinPoolSize(10);
|
||||||
|
cpds.setMaxPoolSize(30);
|
||||||
|
int numHelperThreads = Runtime.getRuntime().availableProcessors() * 2;
|
||||||
|
cpds.setNumHelperThreads(numHelperThreads);
|
||||||
|
cpds.setCheckoutTimeout(timeoutSec * 1000);
|
||||||
|
logTimeoutPart = String.format(" (timeout=%ds)", timeoutSec);
|
||||||
|
result = cpds;
|
||||||
|
|
||||||
|
String OPERATION_DATABASE_CONNECTION_CHECK = String.format("Database [%s] connection check", dbPath);
|
||||||
|
try {
|
||||||
|
Connection conn = result.getConnection();
|
||||||
|
conn.close();
|
||||||
|
log.info("{}: success", OPERATION_DATABASE_CONNECTION_CHECK);
|
||||||
|
return result;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
String msg = String.format("%s%s: failed: %s -> %s",
|
||||||
|
OPERATION_DATABASE_CONNECTION_CHECK, logTimeoutPart, e.getClass().getSimpleName(), e.getMessage());
|
||||||
|
log.error(msg);
|
||||||
|
throw new ModuleInitializeException(msg, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
|
||||||
|
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
return jdbcTemplate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,8 @@ import com.hazelcast.core.HazelcastInstance;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import ru.spcex.clearing.imdg.config.element.HazelcastServerSettings;
|
||||||
|
import ru.spcex.clearing.imdg.config.element.ImdgSettings;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -13,10 +15,11 @@ import java.util.List;
|
||||||
public class HazelcastConfiguration {
|
public class HazelcastConfiguration {
|
||||||
|
|
||||||
private final PoolMapConfigs poolMapConfigs;
|
private final PoolMapConfigs poolMapConfigs;
|
||||||
private final HazelcastServerElement hzSettings;
|
private final HazelcastServerSettings hzSettings;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public HazelcastConfiguration(PoolMapConfigs poolMapConfigs, ImdgSettings imdgSettings) {
|
public HazelcastConfiguration(PoolMapConfigs poolMapConfigs,
|
||||||
|
ImdgSettings imdgSettings) {
|
||||||
this.poolMapConfigs = poolMapConfigs;
|
this.poolMapConfigs = poolMapConfigs;
|
||||||
this.hzSettings = imdgSettings.getHazelcast();
|
this.hzSettings = imdgSettings.getHazelcast();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package ru.spcex.clearing.imdg.config;
|
package ru.spcex.clearing.imdg.config.element;
|
||||||
|
|
||||||
public class DatabaseConnectionElement {
|
public class DatabaseSettings {
|
||||||
private String login;
|
private String login;
|
||||||
private String password;
|
private String password;
|
||||||
private String url;
|
private String url;
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package ru.spcex.clearing.imdg.config;
|
package ru.spcex.clearing.imdg.config.element;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class HazelcastServerElement {
|
public class HazelcastServerSettings {
|
||||||
|
|
||||||
private int listenPort = 5071;
|
private int listenPort = 5071;
|
||||||
private String login = "dev";
|
private String login;
|
||||||
private String password = "dev-pass";
|
private String password;
|
||||||
private List<String> clusterMembers;
|
private List<String> clusterMembers;
|
||||||
|
|
||||||
public int getListenPort() {
|
public int getListenPort() {
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package ru.spcex.clearing.imdg.config;
|
package ru.spcex.clearing.imdg.config.element;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
@ -8,23 +8,22 @@ import org.springframework.stereotype.Component;
|
||||||
@PropertySource("file:${spring.config.location}/application.properties")
|
@PropertySource("file:${spring.config.location}/application.properties")
|
||||||
@ConfigurationProperties("imdg")
|
@ConfigurationProperties("imdg")
|
||||||
public class ImdgSettings {
|
public class ImdgSettings {
|
||||||
|
private HazelcastServerSettings hazelcast;
|
||||||
|
private DatabaseSettings database;
|
||||||
|
|
||||||
private HazelcastServerElement hazelcast;
|
public HazelcastServerSettings getHazelcast() {
|
||||||
private DatabaseConnectionElement database;
|
|
||||||
|
|
||||||
public DatabaseConnectionElement getDatabase() {
|
|
||||||
return database;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDatabase(DatabaseConnectionElement database) {
|
|
||||||
this.database = database;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HazelcastServerElement getHazelcast() {
|
|
||||||
return hazelcast;
|
return hazelcast;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHazelcast(HazelcastServerElement hazelcast) {
|
public void setHazelcast(HazelcastServerSettings hazelcast) {
|
||||||
this.hazelcast = hazelcast;
|
this.hazelcast = hazelcast;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DatabaseSettings getDatabase() {
|
||||||
|
return database;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDatabase(DatabaseSettings database) {
|
||||||
|
this.database = database;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
docker build -t backend-api:1.0.0 ../modules/backend-api/
|
docker build -t backend-api:1.0.0 ../modules/backend-api/
|
||||||
docker run -p 8090:8080 -v /opt/clearing/logs:/opt/clearing/bin/logs backend-api:1.0.0 &
|
docker run -d -p 8090:8080 -v /opt/clearing/logs:/opt/clearing/bin/logs backend-api:1.0.0
|
||||||
Loading…
Add table
Reference in a new issue