[英]In Spring-Boot, How can we connect to two databases (Mysql database and MongoDB) in the same project?
I am trying to create a "Spring-Boot" project in which I have a requirement where I want to connect to to different databases "MySql" and "MongoDB".我正在尝试创建一个“Spring-Boot”项目,其中我需要连接到不同的数据库“MySql”和“MongoDB”。
Do I need to do something special to connect to both the databases or spring-boot will figure automatically to connect to both the databases by itself.我需要做一些特殊的事情来连接到两个数据库还是 spring-boot 会自动连接到两个数据库。 Do I need to define datasource for "mongodb" as well?我是否还需要为“mongodb”定义数据源?
MySQL specific "YML" file is like below MySQL特定的“YML”文件如下
# Default DB parameter definitions for the URL parameters in the spring.datasource.url property below
database:
host: localhost
port: 3306
schema: subscriptions
username: root
password: root
autoconnect:
maxReconnects: 3
initialTimeout: 2
timeout:
connectTimeout: 0
socketTimeout: 0
failover:
host: localhost
port: 3306
queriesBeforeRetryMaster: 50
secondsBeforeRetryMaster: 30
properties: useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false&failOverReadOnly=false&autoReconnect=true&maxReconnects=${database.autoconnect.maxReconnects}&initialTimeout=${database.autoconnect.initialTimeout}&connectTimeout=${database.timeout.connectTimeout}&socketTimeout=${database.timeout.socketTimeout}&queriesBeforeRetryMaster=${database.failover.queriesBeforeRetryMaster}&secondsBeforeRetryMaster=${database.failover.secondsBeforeRetryMaster}
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://${database.host}:${database.port},${database.failover.host}:${database.failover.port}/${database.schema}?${database.properties}
username: ${database.username}
password: ${database.password}
continueOnError: true
initialize: false
initialSize: 0
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 5000
removeAbandonedTimeout: 60
removeAbandoned: true
minIdle: 0
jpa:
show-sql: true
hibernate:
ddl-auto: none
naming_strategy: org.hibernate.cfg.DefaultNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
hbm2ddl:
auto: none
temp:
use_jdbc_metadata_defaults: false
My MongoDB "YML" looks like below我的 MongoDB“YML”如下所示
spring:
data:
mongodb.host: localhost
mongodb.port: 27017
mongodb.database: eventsarchive
mongodb.username: root
mongodb.password: root
mongodb.repositories.enabled: true
Application.java file below Application.java 文件如下
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@EntityScan(basePackages = { "persistence.mysql.domain" })
@EnableJpaRepositories("persistence.mysql.dao")
@EnableMongoRepositories("persistence.mongodb.dao")
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = ApiAuthenticationFilter.class) },
basePackages = {
"admin",
"common",
"mqclient",
"scheduler" })
public class Application {
@Value("${service.name}")
private String serviceName;
@Value("${service.namespace}")
private String serviceNamespace;
@Value("${webservice.namespace}")
private String webserviceNamespace;
@Value("${webservice.port}")
private int webservicePort;
@Value("${jersey.request-filters}")
private String requestFilters;
@Value("${jersey.response-filters}")
private String responseFilters;
private static final String MAPPING_URL = "/%s/*";
static {
System.setProperty(USER_TIMEZONE, "UTC");
}
/**
* Java main method.
*/
public static void main(String[] args) {
/*
* Defines which Spring Boot Profiles should be active on startup. Please see
* http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-profiles for details.
*/
final SpringApplicationBuilder appBuilder = new SpringApplicationBuilder(Application.class);
appBuilder.profiles("common", "common_mongo_db", "common_mysql_db", "common_rabbitmq", "admin").application()
.run(args);
}
/**
* Registers Jersey with Spring Boot.
*/
@Bean
public ServletRegistrationBean registerJersey() {
final ServletRegistrationBean registration = new ServletRegistrationBean(new SpringServlet(),
String.format(MAPPING_URL,
this.serviceNamespace));
registration.addInitParameter(JERSEY_MAPPING_FEATURE, Boolean.toString(true));
registration.addInitParameter(PROPERTY_CONTAINER_REQUEST_FILTERS, this.requestFilters);
registration.addInitParameter(PROPERTY_CONTAINER_RESPONSE_FILTERS, this.responseFilters);
registration.addInitParameter(PROPERTY_RESOURCE_FILTER_FACTORIES, ValidationResourceFilterFactory.class.getName());
return registration;
}
/**
* This method initializes SimpleHttpServerJaxWsServiceExporter bean which reads all @Webservice annotated components
* and hosts web service for them.
*
* @return SimpleHttpServerJaxWsServiceExporter
*/
@Bean
public SimpleHttpServerJaxWsServiceExporter exportJaxwsService() {
final SimpleHttpServerJaxWsServiceExporter jaxWsServiceExporter = new SimpleHttpServerJaxWsServiceExporter();
jaxWsServiceExporter.setPort(this.webservicePort);
jaxWsServiceExporter.setBasePath("/" + this.serviceNamespace + "/" + this.webserviceNamespace + "/");
return jaxWsServiceExporter;
}
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setTomcatContextCustomizers(Arrays.asList(new CustomCustomizer()));
return factory;
}
static class CustomCustomizer implements TomcatContextCustomizer {
@Override
public void customize(Context context) {
context.setUseHttpOnly(false);
context.setCookies(false);
}
}
}
There were no questions in this forum which talks about the connecting the two different databases (Mysql database and MongoDB) in the same spring-boot project.本论坛没有任何关于连接同一个spring-boot项目中的两个不同数据库(Mysql数据库和MongoDB)的问题。
I have done the work to do that hence I am posting it as answer here.我已经完成了这项工作,因此我将其作为答案发布在这里。
Use below given configurations and the repositories as, either JpaRepository or MongoRepository, depending on the DB particular repository belongs to.使用下面给定的配置和存储库作为 JpaRepository 或 MongoRepository,具体取决于数据库特定存储库所属。
MySQL specific "YML" file is like below MySQL特定的“YML”文件如下
# Default DB parameter definitions for the URL parameters in the spring.datasource.url property below
database:
host: localhost
port: 3306
schema: subscriptions
username: root
password: root
autoconnect:
maxReconnects: 3
initialTimeout: 2
timeout:
connectTimeout: 0
socketTimeout: 0
failover:
host: localhost
port: 3306
queriesBeforeRetryMaster: 50
secondsBeforeRetryMaster: 30
properties: useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false&failOverReadOnly=false&autoReconnect=true&maxReconnects=${database.autoconnect.maxReconnects}&initialTimeout=${database.autoconnect.initialTimeout}&connectTimeout=${database.timeout.connectTimeout}&socketTimeout=${database.timeout.socketTimeout}&queriesBeforeRetryMaster=${database.failover.queriesBeforeRetryMaster}&secondsBeforeRetryMaster=${database.failover.secondsBeforeRetryMaster}
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://${database.host}:${database.port},${database.failover.host}:${database.failover.port}/${database.schema}?${database.properties}
username: ${database.username}
password: ${database.password}
continueOnError: true
initialize: false
initialSize: 0
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 5000
removeAbandonedTimeout: 60
removeAbandoned: true
minIdle: 0
jpa:
show-sql: true
hibernate:
ddl-auto: none
naming_strategy: org.hibernate.cfg.DefaultNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
hbm2ddl:
auto: none
temp:
use_jdbc_metadata_defaults: false
My MongoDB "YML" looks like below我的 MongoDB“YML”如下所示
spring:
data:
mongodb.host: localhost
mongodb.port: 27017
mongodb.database: eventsarchive
mongodb.username: root
mongodb.password: root
mongodb.repositories.enabled: true
Application.java file below Application.java 文件如下
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@EntityScan(basePackages = { "persistence.mysql.domain" })
@EnableJpaRepositories("persistence.mysql.dao")
@EnableMongoRepositories("persistence.mongodb.dao")
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
value = ApiAuthenticationFilter.class) },
basePackages = {
"admin",
"common",
"mqclient",
"scheduler" })
public class Application {
@Value("${service.name}")
private String serviceName;
@Value("${service.namespace}")
private String serviceNamespace;
@Value("${webservice.namespace}")
private String webserviceNamespace;
@Value("${webservice.port}")
private int webservicePort;
@Value("${jersey.request-filters}")
private String requestFilters;
@Value("${jersey.response-filters}")
private String responseFilters;
private static final String MAPPING_URL = "/%s/*";
static {
System.setProperty(USER_TIMEZONE, "UTC");
}
/**
* Java main method.
*/
public static void main(String[] args) {
/*
* Defines which Spring Boot Profiles should be active on startup. Please see
* http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-profiles for details.
*/
final SpringApplicationBuilder appBuilder = new SpringApplicationBuilder(Application.class);
appBuilder.profiles("common", "common_mongo_db", "common_mysql_db", "common_rabbitmq", "admin").application()
.run(args);
}
/**
* Registers Jersey with Spring Boot.
*/
@Bean
public ServletRegistrationBean registerJersey() {
final ServletRegistrationBean registration = new ServletRegistrationBean(new SpringServlet(),
String.format(MAPPING_URL,
this.serviceNamespace));
registration.addInitParameter(JERSEY_MAPPING_FEATURE, Boolean.toString(true));
registration.addInitParameter(PROPERTY_CONTAINER_REQUEST_FILTERS, this.requestFilters);
registration.addInitParameter(PROPERTY_CONTAINER_RESPONSE_FILTERS, this.responseFilters);
registration.addInitParameter(PROPERTY_RESOURCE_FILTER_FACTORIES, ValidationResourceFilterFactory.class.getName());
return registration;
}
/**
* This method initializes SimpleHttpServerJaxWsServiceExporter bean which reads all @Webservice annotated components
* and hosts web service for them.
*
* @return SimpleHttpServerJaxWsServiceExporter
*/
@Bean
public SimpleHttpServerJaxWsServiceExporter exportJaxwsService() {
final SimpleHttpServerJaxWsServiceExporter jaxWsServiceExporter = new SimpleHttpServerJaxWsServiceExporter();
jaxWsServiceExporter.setPort(this.webservicePort);
jaxWsServiceExporter.setBasePath("/" + this.serviceNamespace + "/" + this.webserviceNamespace + "/");
return jaxWsServiceExporter;
}
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setTomcatContextCustomizers(Arrays.asList(new CustomCustomizer()));
return factory;
}
static class CustomCustomizer implements TomcatContextCustomizer {
@Override
public void customize(Context context) {
context.setUseHttpOnly(false);
context.setCookies(false);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.