-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Portal-UI adds serverconfig configuration management of ApolloConfigDB (
#4680) * add tech-support-qq-4.png * Update README.md * Enhance the user experience in the scenario of submitting duplicate keys * Modify the key-value conflict exception prompt, adjust the code style * feat(apollo-portal): Added serverconfig configuration management for ApolloConfigDB * doc(CHANGES.md): update CHANGES.md * Fix(CI): Fix CI check license failures * feat(apollo-biz): Additional test cases Co-authored-by: Jason Song <[email protected]>
- Loading branch information
Showing
25 changed files
with
984 additions
and
588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../main/java/com/ctrip/framework/apollo/adminservice/controller/ServerConfigController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2022 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.adminservice.controller; | ||
|
||
import com.ctrip.framework.apollo.biz.entity.ServerConfig; | ||
import com.ctrip.framework.apollo.biz.service.ServerConfigService; | ||
import java.util.List; | ||
import javax.validation.Valid; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author kl (http://kailing.pub) | ||
* @since 2022/12/13 | ||
*/ | ||
@RestController | ||
public class ServerConfigController { | ||
private final ServerConfigService serverConfigService; | ||
public ServerConfigController(ServerConfigService serverConfigService) { | ||
this.serverConfigService = serverConfigService; | ||
} | ||
@GetMapping("/server/config/find-all-config") | ||
public List<ServerConfig> findAllServerConfig() { | ||
return serverConfigService.findAll(); | ||
} | ||
|
||
@PostMapping("/server/config") | ||
public ServerConfig createOrUpdatePortalDBConfig(@Valid @RequestBody ServerConfig serverConfig) { | ||
return serverConfigService.createOrUpdateConfig(serverConfig); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...t/java/com/ctrip/framework/apollo/adminservice/controller/ServerConfigControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2022 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.adminservice.controller; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import com.ctrip.framework.apollo.biz.entity.ServerConfig; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import org.springframework.test.context.jdbc.Sql.ExecutionPhase; | ||
|
||
/** | ||
* @author kl (http://kailing.pub) | ||
* @since 2022/12/14 | ||
*/ | ||
class ServerConfigControllerTest extends AbstractControllerTest { | ||
|
||
@Test | ||
@Sql(scripts = "/controller/test-server-config.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) | ||
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) | ||
void findAllServerConfig() { | ||
ServerConfig[] serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class); | ||
assertNotNull(serverConfigs); | ||
assertEquals(1, serverConfigs.length); | ||
assertEquals("name", serverConfigs[0].getKey()); | ||
assertEquals("kl", serverConfigs[0].getValue()); | ||
} | ||
|
||
@Test | ||
@Sql(scripts = "/controller/test-server-config.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD) | ||
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD) | ||
void createOrUpdatePortalDBConfig() { | ||
ServerConfig serverConfig = new ServerConfig(); | ||
serverConfig.setKey("name"); | ||
serverConfig.setValue("ckl"); | ||
ServerConfig response = restTemplate.postForObject(url("/server/config"), serverConfig, ServerConfig.class); | ||
assertNotNull(response); | ||
|
||
ServerConfig[] serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class); | ||
assertNotNull(serverConfigs); | ||
assertEquals(1, serverConfigs.length); | ||
assertEquals("name", serverConfigs[0].getKey()); | ||
assertEquals("ckl", serverConfigs[0].getValue()); | ||
|
||
serverConfig = new ServerConfig(); | ||
serverConfig.setKey("age"); | ||
serverConfig.setValue("30"); | ||
response = restTemplate.postForObject(url("/server/config"), serverConfig, ServerConfig.class); | ||
assertNotNull(response); | ||
|
||
serverConfigs = restTemplate.getForObject(url("/server/config/find-all-config"), ServerConfig[].class); | ||
assertNotNull(serverConfigs); | ||
assertEquals(2, serverConfigs.length); | ||
|
||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
apollo-adminservice/src/test/resources/controller/test-server-config.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- | ||
-- Copyright 2022 Apollo Authors | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
INSERT INTO `ServerConfig` (`Key`, `Cluster`, `Value`) | ||
VALUES | ||
('name', 'default', 'kl'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/ServerConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2022 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.biz.service; | ||
|
||
import com.ctrip.framework.apollo.biz.entity.ServerConfig; | ||
import com.ctrip.framework.apollo.biz.repository.ServerConfigRepository; | ||
import com.google.common.collect.Lists; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import javax.transaction.Transactional; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* @author kl (http://kailing.pub) | ||
* @since 2022/12/13 | ||
*/ | ||
@Service | ||
public class ServerConfigService { | ||
|
||
private final ServerConfigRepository serverConfigRepository; | ||
|
||
public ServerConfigService(ServerConfigRepository serverConfigRepository) { | ||
this.serverConfigRepository = serverConfigRepository; | ||
} | ||
|
||
public List<ServerConfig> findAll() { | ||
Iterable<ServerConfig> serverConfigs = serverConfigRepository.findAll(); | ||
return Lists.newArrayList(serverConfigs); | ||
} | ||
|
||
@Transactional | ||
public ServerConfig createOrUpdateConfig(ServerConfig serverConfig) { | ||
|
||
ServerConfig storedConfig = serverConfigRepository.findByKey(serverConfig.getKey()); | ||
|
||
if (Objects.isNull(storedConfig)) {//create | ||
serverConfig.setId(0L);//为空,设置ID 为0,jpa执行新增操作 | ||
if(Objects.isNull(serverConfig.getCluster())){ | ||
serverConfig.setCluster("default"); | ||
} | ||
return serverConfigRepository.save(serverConfig); | ||
} | ||
|
||
//update | ||
storedConfig.setComment(serverConfig.getComment()); | ||
storedConfig.setDataChangeLastModifiedBy(serverConfig.getDataChangeLastModifiedBy()); | ||
storedConfig.setValue(serverConfig.getValue()); | ||
|
||
return serverConfigRepository.save(storedConfig); | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/ServerConfigServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2022 Apollo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package com.ctrip.framework.apollo.biz.service; | ||
|
||
import com.ctrip.framework.apollo.biz.AbstractIntegrationTest; | ||
import com.ctrip.framework.apollo.biz.entity.ServerConfig; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
/** | ||
* @author kl (http://kailing.pub) | ||
* @since 2022/12/14 | ||
*/ | ||
public class ServerConfigServiceTest extends AbstractIntegrationTest { | ||
|
||
@Autowired | ||
private ServerConfigService serverConfigService; | ||
|
||
@Test | ||
public void findAll() { | ||
List<ServerConfig> serverConfigs = serverConfigService.findAll(); | ||
assertThat(serverConfigs).isNotNull(); | ||
assertThat(serverConfigs.size()).isGreaterThanOrEqualTo(0); | ||
} | ||
|
||
@Test | ||
public void createOrUpdateConfig() { | ||
ServerConfig serverConfig = new ServerConfig(); | ||
serverConfig.setKey("name"); | ||
serverConfig.setValue("kl"); | ||
serverConfigService.createOrUpdateConfig(serverConfig); | ||
|
||
List<ServerConfig> serverConfigs = serverConfigService.findAll(); | ||
assertThat(serverConfigs).isNotNull(); | ||
assertThat(serverConfigs.get(0).getValue()).isEqualTo("kl"); | ||
assertThat(serverConfigs.get(0).getCluster()).isEqualTo("default"); | ||
assertThat(serverConfigs.get(0).getKey()).isEqualTo("name"); | ||
|
||
|
||
serverConfig.setValue("kl2"); | ||
serverConfigService.createOrUpdateConfig(serverConfig); | ||
|
||
serverConfigs = serverConfigService.findAll(); | ||
assertThat(serverConfigs).isNotNull(); | ||
assertThat(serverConfigs.size()).isEqualTo(1); | ||
assertThat(serverConfigs.get(0).getValue()).isEqualTo("kl2"); | ||
assertThat(serverConfigs.get(0).getKey()).isEqualTo("name"); | ||
|
||
serverConfig = new ServerConfig(); | ||
serverConfig.setKey("name2"); | ||
serverConfig.setValue("kl2"); | ||
serverConfigService.createOrUpdateConfig(serverConfig); | ||
|
||
serverConfigs = serverConfigService.findAll(); | ||
assertThat(serverConfigs).isNotNull(); | ||
assertThat(serverConfigs.size()).isEqualTo(2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.