问题描述
我正在使用以下库从 Java Spring 应用程序启动嵌入式 maria DB 容器:
com.playtika.testcontainers.embedded-mariadb
https://mvnrepository.com/artifact/com.playtika.testcontainers/embedded-mariadb
第三方的这个类用于作为Spring bean启动容器:
@Slf4j
@Configuration
@ConditionalOnExpression("${embedded.containers.enabled:true}")
@AutoConfigureAfter(DockerPresenceBootstrapConfiguration.class)
@ConditionalOnProperty(name = "embedded.mariadb.enabled",matchIfMissing = true)
@EnableConfigurationProperties(MariaDBProperties.class)
public class EmbeddedMariaDBBootstrapConfiguration {
@Bean(name = BEAN_NAME_EMBEDDED_MARIADB,destroyMethod = "stop")
public MariaDBContainer mariadb(ConfigurableEnvironment environment,MariaDBProperties properties) throws Exception {
log.info("Starting mariadb server. Docker image: {}",properties.dockerImage);
MariaDBContainer mariadb =
new MariaDBContainer<>(properties.dockerImage)
.withEnv("MYSQL_ALLOW_EMPTY_PASSWORD","yes")
.withUsername(properties.getUser())
.withPassword(properties.getPassword())
.withDatabaseName(properties.getDatabase())
.withCommand(
"--character-set-server=" + properties.getEncoding(),"--collation-server=" + properties.getCollation(),"--max_allowed_packet=" + properties.getMaxAllowedPacket())
.withExposedPorts(properties.port)
.withCreateContainerCmdModifier(cmd -> cmd.getHostConfig().withCapAdd(Capability.NET_ADMIN))
.withInitScript(properties.initScriptPath);
mariadb = (MariaDBContainer) configureCommonsAndStart(mariadb,properties,log);
registerMariadbEnvironment(mariadb,environment,properties);
return mariadb;
}
private void registerMariadbEnvironment(MariaDBContainer mariadb,ConfigurableEnvironment environment,MariaDBProperties properties) {
Integer mappedPort = mariadb.getMappedPort(properties.port);
String host = mariadb.getContainerIpAddress();
LinkedHashMap<String,Object> map = new LinkedHashMap<>();
map.put("embedded.mariadb.port",mappedPort);
map.put("embedded.mariadb.host",host);
map.put("embedded.mariadb.schema",properties.getDatabase());
map.put("embedded.mariadb.user",properties.getUser());
map.put("embedded.mariadb.password",properties.getPassword());
String jdbcURL = "jdbc:mysql://{}:{}/{}";
log.info("Started mariadb server. Connection details: {}," +
"JDBC connection url: " + jdbcURL,map,host,mappedPort,properties.getDatabase());
MapPropertySource propertySource = new MapPropertySource("embeddedMariaInfo",map);
environment.getPropertySources().addFirst(propertySource);
}
}
这个类创建了 MariaDBContainer
类。
MariaDBContainer
扩展 GenericContainer
,从 GenericContainer
类可以使用以下代码进行卷映射:
new GenericContainer(...)
.withClasspathResourceMapping("redis.conf","/etc/redis.conf",BindMode.READ_ONLY)
问题是我无法修改EmbeddedMariaDBBootstrapConfiguration
,它是com.playtika.testcontainers.embedded-mariadb
3rd 方的一部分。
您知道如何将卷映射到 maria db 嵌入式容器吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)