Skip to content

Commit 4f4a8f7

Browse files
authored
[Multi-Database Support][h2] Support run on h2 (#4851)
* [Multi-Database Support][h2] add h2 init sql * fix init method profile * fix * fix review problem
1 parent a4df1a3 commit 4f4a8f7

File tree

7 files changed

+151
-17
lines changed

7 files changed

+151
-17
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Apollo 2.2.0
2626
* [Add a configuration config-service.cache.key.ignore-case to control whether the cache key is case-sensitive](https://github.com/apolloconfig/apollo/pull/4820)
2727
* [feat: check port use by another process or not when startup](https://github.com/apolloconfig/apollo/pull/4656)
2828
* [Bump springboot version from 2.7.9 to 2.7.11](https://github.com/apolloconfig/apollo/pull/4828)
29+
* [[Multi-Database Support][h2] Support run on h2](https://github.com/apolloconfig/apollo/pull/4851)
2930

3031
------------------
3132
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1)

apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/BizDBPropertySource.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.core.env.Environment;
32+
import org.springframework.core.env.Profiles;
33+
import org.springframework.core.io.ClassPathResource;
34+
import org.springframework.core.io.Resource;
35+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
36+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
3137
import org.springframework.stereotype.Component;
3238

39+
import javax.annotation.PostConstruct;
40+
import javax.sql.DataSource;
3341
import java.util.Map;
3442
import java.util.Objects;
3543

@@ -43,17 +51,27 @@ public class BizDBPropertySource extends RefreshablePropertySource {
4351

4452
private final ServerConfigRepository serverConfigRepository;
4553

46-
public BizDBPropertySource(final String name,
47-
final Map<String, Object> source,
48-
final ServerConfigRepository serverConfigRepository) {
49-
super(name, source);
50-
this.serverConfigRepository = serverConfigRepository;
51-
}
54+
private final DataSource dataSource;
55+
56+
private final Environment env;
5257

5358
@Autowired
54-
public BizDBPropertySource(final ServerConfigRepository serverConfigRepository) {
59+
public BizDBPropertySource(final ServerConfigRepository serverConfigRepository, DataSource dataSource,
60+
final Environment env) {
5561
super("DBConfig", Maps.newConcurrentMap());
5662
this.serverConfigRepository = serverConfigRepository;
63+
this.dataSource = dataSource;
64+
this.env = env;
65+
}
66+
67+
@PostConstruct
68+
public void runSqlScript() throws Exception {
69+
if (env.acceptsProfiles(Profiles.of("h2"))) {
70+
Resource resource = new ClassPathResource("jpa/init.h2.sql");
71+
if (resource.exists()) {
72+
DatabasePopulatorUtils.execute(new ResourceDatabasePopulator(resource), dataSource);
73+
}
74+
}
5775
}
5876

5977
String getCurrentDataCenter() {

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/config/BizConfigTest.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import org.mockito.Mock;
2525
import org.mockito.junit.MockitoJUnitRunner;
2626
import org.springframework.core.env.ConfigurableEnvironment;
27+
import org.springframework.core.env.Environment;
2728
import org.springframework.test.util.ReflectionTestUtils;
2829

30+
import javax.sql.DataSource;
31+
2932
import static org.junit.Assert.assertEquals;
3033
import static org.junit.Assert.assertFalse;
3134
import static org.junit.Assert.assertTrue;
@@ -42,11 +45,14 @@ public class BizConfigTest {
4245
@Mock
4346
private ServerConfigRepository serverConfigRepository;
4447

48+
@Mock
49+
private DataSource dataSource;
50+
4551
private BizConfig bizConfig;
4652

4753
@Before
4854
public void setUp() throws Exception {
49-
bizConfig = new BizConfig(new BizDBPropertySource(serverConfigRepository));
55+
bizConfig = new BizConfig(new BizDBPropertySource(serverConfigRepository, dataSource, environment));
5056
ReflectionTestUtils.setField(bizConfig, "environment", environment);
5157
}
5258

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/BizDBPropertySourceTest.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
import org.junit.Before;
2929
import org.junit.Test;
3030
import org.mockito.Mock;
31+
import org.springframework.core.env.Environment;
3132

33+
import javax.sql.DataSource;
3234
import java.util.List;
3335

3436
import static org.junit.Assert.assertEquals;
@@ -43,6 +45,13 @@ public class BizDBPropertySourceTest extends AbstractUnitTest {
4345

4446
@Mock
4547
private ServerConfigRepository serverConfigRepository;
48+
49+
@Mock
50+
private DataSource dataSource;
51+
52+
@Mock
53+
private Environment environment;
54+
4655
private BizDBPropertySource propertySource;
4756

4857
private String clusterConfigKey = "clusterKey";
@@ -54,7 +63,7 @@ public class BizDBPropertySourceTest extends AbstractUnitTest {
5463

5564
@Before
5665
public void initTestData() {
57-
propertySource = spy(new BizDBPropertySource(serverConfigRepository));
66+
propertySource = spy(new BizDBPropertySource(serverConfigRepository, dataSource, environment));
5867

5968
List<ServerConfig> configs = Lists.newLinkedList();
6069

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--
2+
-- Copyright 2023 Apollo Authors
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
INSERT INTO "ServerConfig" ("Key", "Cluster", "Value", "Comment", "DataChange_CreatedBy", "DataChange_CreatedTime")
17+
VALUES
18+
('eureka.service.url', 'default', 'http://localhost:8080/eureka/', 'Eureka服务Url,多个service以英文逗号分隔', 'default', '1970-01-01 00:00:00'),
19+
('namespace.lock.switch', 'default', 'false', '一次发布只能有一个人修改开关', 'default', '1970-01-01 00:00:00'),
20+
('item.key.length.limit', 'default', '128', 'item key 最大长度限制', 'default', '1970-01-01 00:00:00'),
21+
('item.value.length.limit', 'default', '20000', 'item value最大长度限制', 'default', '1970-01-01 00:00:00'),
22+
('config-service.cache.enabled', 'default', 'false', 'ConfigService是否开启缓存,开启后能提高性能,但是会增大内存消耗!', 'default', '1970-01-01 00:00:00');
23+
CREATE ALIAS IF NOT EXISTS UNIX_TIMESTAMP FOR "com.ctrip.framework.apollo.common.jpa.H2Function.unixTimestamp";

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/PortalDBPropertySource.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
2727
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.core.env.Environment;
29+
import org.springframework.core.env.Profiles;
30+
import org.springframework.core.io.ClassPathResource;
31+
import org.springframework.core.io.Resource;
32+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
33+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2834
import org.springframework.stereotype.Component;
2935

30-
import java.util.Map;
36+
import javax.annotation.PostConstruct;
37+
import javax.sql.DataSource;
3138
import java.util.Objects;
3239

3340

@@ -40,16 +47,27 @@ public class PortalDBPropertySource extends RefreshablePropertySource {
4047

4148
private final ServerConfigRepository serverConfigRepository;
4249

43-
public PortalDBPropertySource(final String name,
44-
final Map<String, Object> source,
45-
final ServerConfigRepository serverConfigRepository) {
46-
super(name, source);
47-
this.serverConfigRepository = serverConfigRepository;
48-
}
50+
private final DataSource dataSource;
51+
52+
private final Environment env;
53+
4954
@Autowired
50-
public PortalDBPropertySource(final ServerConfigRepository serverConfigRepository) {
55+
public PortalDBPropertySource(final ServerConfigRepository serverConfigRepository, DataSource dataSource,
56+
final Environment env) {
5157
super("DBConfig", Maps.newConcurrentMap());
5258
this.serverConfigRepository = serverConfigRepository;
59+
this.dataSource = dataSource;
60+
this.env = env;
61+
}
62+
63+
@PostConstruct
64+
public void runSqlScript() throws Exception {
65+
if (env.acceptsProfiles(Profiles.of("h2"))) {
66+
Resource resource = new ClassPathResource("jpa/init.h2.sql");
67+
if (resource.exists()) {
68+
DatabasePopulatorUtils.execute(new ResourceDatabasePopulator(resource), dataSource);
69+
}
70+
}
5371
}
5472

5573
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
--
2+
-- Copyright 2023 Apollo Authors
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- http://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
CREATE TABLE SPRING_SESSION
17+
(
18+
PRIMARY_ID VARCHAR(255) NOT NULL,
19+
SESSION_ID VARCHAR(255) NOT NULL,
20+
CREATION_TIME BIGINT NOT NULL,
21+
LAST_ACCESS_TIME BIGINT NOT NULL,
22+
MAX_INACTIVE_INTERVAL INT NOT NULL,
23+
EXPIRY_TIME BIGINT NOT NULL,
24+
PRINCIPAL_NAME VARCHAR(100),
25+
CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
26+
);
27+
28+
CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
29+
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
30+
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);
31+
32+
CREATE TABLE SPRING_SESSION_ATTRIBUTES
33+
(
34+
SESSION_PRIMARY_ID VARCHAR(255) NOT NULL,
35+
ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
36+
ATTRIBUTE_BYTES BLOB NOT NULL,
37+
CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
38+
CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION (PRIMARY_ID) ON DELETE CASCADE
39+
);
40+
41+
INSERT INTO "ServerConfig" ("Key", "Value", "Comment", "DataChange_CreatedBy", "DataChange_CreatedTime")
42+
VALUES
43+
('apollo.portal.envs', 'dev', '可支持的环境列表', 'default', '1970-01-01 00:00:00'),
44+
('organizations', '[{"orgId":"TEST1","orgName":"样例部门1"},{"orgId":"TEST2","orgName":"样例部门2"}]', '部门列表', 'default', '1970-01-01 00:00:00'),
45+
('superAdmin', 'apollo', 'Portal超级管理员', 'default', '1970-01-01 00:00:00'),
46+
('api.readTimeout', '10000', 'http接口read timeout', 'default', '1970-01-01 00:00:00'),
47+
('consumer.token.salt', 'someSalt', 'consumer token salt', 'default', '1970-01-01 00:00:00'),
48+
('admin.createPrivateNamespace.switch', 'true', '是否允许项目管理员创建私有namespace', 'default', '1970-01-01 00:00:00'),
49+
('configView.memberOnly.envs', 'pro', '只对项目成员显示配置信息的环境列表,多个env以英文逗号分隔', 'default', '1970-01-01 00:00:00'),
50+
('apollo.portal.meta.servers', '{}', '各环境Meta Service列表', 'default', '1970-01-01 00:00:00');
51+
52+
INSERT INTO "Users" ("Username", "Password", "UserDisplayName", "Email", "Enabled")
53+
VALUES
54+
('apollo', '$2a$10$7r20uS.BQ9uBpf3Baj3uQOZvMVvB1RN3PYoKE94gtz2.WAOuiiwXS', 'apollo', '[email protected]', 1);
55+
56+
INSERT INTO "Authorities" ("Username", "Authority")
57+
VALUES
58+
('apollo', 'ROLE_user');
59+
CREATE ALIAS IF NOT EXISTS UNIX_TIMESTAMP FOR "com.ctrip.framework.apollo.common.jpa.H2Function.unixTimestamp";

0 commit comments

Comments
 (0)