mirror of
https://github.com/didi/KnowStreaming.git
synced 2025-12-24 03:42:07 +08:00
[Optimize]优化 MySQL & ES 测试容器的初始化(#906)
主要的变更 1、knowstreaming/knowstreaming-manager 容器; 2、knowstreaming/knowstreaming-mysql 容器调整为使用 mysql:5.7 容器; 3、初始化 mysql:5.7 容器后,增加初始化 MySQL 表及数据的动作; 被影响的变更: 1、移动 km-dist/init/sql 下的MySQL初始化脚本至 km-persistence/src/main/resource/sql 下,以便项目测试时加载到所需的初始化 SQL; 2、删除无用的 km-dist/init/template 目录; 3、因为 km-dist/init/sql 和 km-dist/init/template 目录的调整,因此也调整 ReleaseKnowStreaming.xml 内的文件内容;
This commit is contained in:
@@ -42,8 +42,16 @@ public abstract class KMTestEnvService {
|
||||
@DynamicPropertySource
|
||||
static void setUp(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.datasource.know-streaming.jdbc-url", mySQLTestContainer.jdbcUrl());
|
||||
registry.add("spring.datasource.know-streaming.username", mySQLTestContainer.jdbcUsername());
|
||||
registry.add("spring.datasource.know-streaming.password", mySQLTestContainer.jdbcPassword());
|
||||
|
||||
registry.add("spring.logi-job.jdbc-url", mySQLTestContainer.jdbcUrl());
|
||||
registry.add("spring.logi-job.username", mySQLTestContainer.jdbcUsername());
|
||||
registry.add("spring.logi-job.password", mySQLTestContainer.jdbcPassword());
|
||||
|
||||
registry.add("spring.logi-security.jdbc-url", mySQLTestContainer.jdbcUrl());
|
||||
registry.add("spring.logi-security.username", mySQLTestContainer.jdbcUsername());
|
||||
registry.add("spring.logi-security.password", mySQLTestContainer.jdbcPassword());
|
||||
|
||||
registry.add("es.client.address", esTestContainer.esUrl());
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.xiaojukeji.know.streaming.test.container.es;
|
||||
|
||||
import com.xiaojukeji.know.streaming.test.container.BaseTestContainer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.elasticsearch.ElasticsearchContainer;
|
||||
import org.testcontainers.lifecycle.Startables;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
@@ -19,14 +18,6 @@ public class ESTestContainer extends BaseTestContainer {
|
||||
.withEnv("ES_JAVA_OPTS", "-Xms512m -Xmx512m")
|
||||
.withEnv("discovery.type", "single-node");
|
||||
|
||||
// km容器,需要初始化es索引模版
|
||||
private static final GenericContainer<?> INIT_CONTAINER = new GenericContainer<>(
|
||||
"knowstreaming/knowstreaming-manager:latest"
|
||||
)
|
||||
.withEnv("TZ", "Asia/Shanghai")
|
||||
.withCommand("/bin/bash", "/es_template_create.sh")
|
||||
.dependsOn(ES_CONTAINER);
|
||||
|
||||
@NotNull
|
||||
public Supplier<Object> esUrl() {
|
||||
return () -> ES_CONTAINER.getHost() + ":" + ES_CONTAINER.getMappedPort(9200);
|
||||
@@ -34,7 +25,7 @@ public class ESTestContainer extends BaseTestContainer {
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
Startables.deepStart(ES_CONTAINER, INIT_CONTAINER).join();
|
||||
Startables.deepStart(ES_CONTAINER).join();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
package com.xiaojukeji.know.streaming.test.container.mysql;
|
||||
|
||||
import com.didiglobal.logi.log.ILog;
|
||||
import com.didiglobal.logi.log.LogFactory;
|
||||
import com.xiaojukeji.know.streaming.km.common.utils.Tuple;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.testcontainers.containers.ContainerLaunchException;
|
||||
import org.testcontainers.containers.JdbcDatabaseContainer;
|
||||
import org.testcontainers.containers.MySQLContainer;
|
||||
import org.testcontainers.delegate.DatabaseDelegate;
|
||||
import org.testcontainers.ext.ScriptUtils;
|
||||
import org.testcontainers.jdbc.JdbcDatabaseDelegate;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import javax.script.ScriptException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -13,6 +22,8 @@ import java.util.Set;
|
||||
* @see org.testcontainers.containers.MySQLContainer
|
||||
*/
|
||||
public class KSMySQLContainer<SELF extends KSMySQLContainer<SELF>> extends JdbcDatabaseContainer<SELF> {
|
||||
private static final ILog LOGGER = LogFactory.getLog(KSMySQLContainer.class);
|
||||
|
||||
public static final String NAME = "mysql";
|
||||
|
||||
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mysql");
|
||||
@@ -39,6 +50,8 @@ public class KSMySQLContainer<SELF extends KSMySQLContainer<SELF>> extends JdbcD
|
||||
|
||||
private static final String MYSQL_ROOT_USER = "root";
|
||||
|
||||
private List<Tuple<String, String>> initScriptPathAndContentList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* @deprecated use {@link MySQLContainer(DockerImageName)} instead
|
||||
*/
|
||||
@@ -169,4 +182,31 @@ public class KSMySQLContainer<SELF extends KSMySQLContainer<SELF>> extends JdbcD
|
||||
this.password = password;
|
||||
return self();
|
||||
}
|
||||
|
||||
public SELF addInitScriptPathAndContent(String initScriptPath, String initScriptContent) {
|
||||
initScriptPathAndContentList.add(new Tuple<>(initScriptPath, initScriptContent));
|
||||
return self();
|
||||
}
|
||||
|
||||
// KS改动的地方
|
||||
@Override
|
||||
public DatabaseDelegate getDatabaseDelegate() {
|
||||
return new JdbcDatabaseDelegate(this, "");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runInitScriptIfRequired() {
|
||||
if (initScriptPathAndContentList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Tuple<String, String> elem: initScriptPathAndContentList) {
|
||||
try {
|
||||
ScriptUtils.executeDatabaseScript(this.getDatabaseDelegate(), elem.getV1(), elem.getV2());
|
||||
} catch (ScriptException var5) {
|
||||
LOGGER.error("Error while executing init script: {}", elem.getV1(), var5);
|
||||
throw new ScriptUtils.UncategorizedScriptException("Error while executing init script: " + elem.getV2(), var5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.xiaojukeji.know.streaming.test.container.mysql;
|
||||
|
||||
import com.xiaojukeji.know.streaming.km.persistence.utils.LoadSQLUtil;
|
||||
import com.xiaojukeji.know.streaming.test.container.BaseTestContainer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.testcontainers.lifecycle.Startables;
|
||||
@@ -8,7 +9,12 @@ import org.testcontainers.utility.DockerImageName;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class MySQLTestContainer extends BaseTestContainer {
|
||||
private static final String DB_PROPERTY = "?useUnicode=true" +
|
||||
private static final String DB_USERNAME = "root";
|
||||
private static final String DB_PASSWORD = "1234567890";
|
||||
|
||||
private static final String DATABASE_NAME = "know_streaming";
|
||||
|
||||
private static final String DB_PROPERTY = "?useUnicode=true" +
|
||||
"&characterEncoding=utf8" +
|
||||
"&jdbcCompliantTruncation=true" +
|
||||
"&allowMultiQueries=true" +
|
||||
@@ -18,19 +24,34 @@ public class MySQLTestContainer extends BaseTestContainer {
|
||||
"&allowPublicKeyRetrieval=true";
|
||||
|
||||
private static final KSMySQLContainer<?> MYSQL_CONTAINER = new KSMySQLContainer<>(
|
||||
DockerImageName.parse("knowstreaming/knowstreaming-mysql:latest").asCompatibleSubstituteFor("mysql")
|
||||
DockerImageName.parse("mysql:5.7").asCompatibleSubstituteFor("mysql")
|
||||
)
|
||||
.withEnv("MYSQL_ROOT_HOST", "%")
|
||||
.withEnv("TZ", "Asia/Shanghai")
|
||||
.withDatabaseName("know_streaming")
|
||||
.withUsername("root")
|
||||
.withPassword("mysql_pass");
|
||||
.withDatabaseName(DATABASE_NAME)
|
||||
.withUsername(DB_USERNAME)
|
||||
.withPassword(DB_PASSWORD)
|
||||
.addInitScriptPathAndContent(LoadSQLUtil.SQL_DDL_KS_KM, String.format("use %s;\n%s", DATABASE_NAME, LoadSQLUtil.loadSQL(LoadSQLUtil.SQL_DDL_KS_KM)))
|
||||
.addInitScriptPathAndContent(LoadSQLUtil.SQL_DDL_LOGI_JOB, String.format("use %s;\n%s", DATABASE_NAME, LoadSQLUtil.loadSQL(LoadSQLUtil.SQL_DDL_LOGI_JOB)))
|
||||
.addInitScriptPathAndContent(LoadSQLUtil.SQL_DDL_LOGI_SECURITY, String.format("use %s;\n%s", DATABASE_NAME, LoadSQLUtil.loadSQL(LoadSQLUtil.SQL_DDL_LOGI_SECURITY)))
|
||||
.addInitScriptPathAndContent(LoadSQLUtil.SQL_DML_KS_KM, String.format("use %s;\n%s", DATABASE_NAME, LoadSQLUtil.loadSQL(LoadSQLUtil.SQL_DML_KS_KM)))
|
||||
.addInitScriptPathAndContent(LoadSQLUtil.SQL_DML_LOGI, String.format("use %s;\n%s", DATABASE_NAME, LoadSQLUtil.loadSQL(LoadSQLUtil.SQL_DML_LOGI)))
|
||||
;
|
||||
|
||||
@NotNull
|
||||
public Supplier<Object> jdbcUsername() {
|
||||
return () -> DB_USERNAME;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Supplier<Object> jdbcPassword() {
|
||||
return () -> DB_PASSWORD;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Supplier<Object> jdbcUrl() {
|
||||
return () -> "jdbc:mariadb://"
|
||||
+ MYSQL_CONTAINER.getHost() + ":" + MYSQL_CONTAINER.getMappedPort(3306)
|
||||
+ "/know_streaming" + DB_PROPERTY;
|
||||
+ "/" + DATABASE_NAME + DB_PROPERTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user