添加配置分析
1.添加配置项
2.对应了ItemController
@PreAuthorize(value = "@permissionValidator.hasModifyNamespacePermission(#appId, #namespaceName, #env)")
@PostMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/item")
public ItemDTO createItem(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String namespaceName,
@RequestBody ItemDTO item) {
checkModel(isValidItem(item)); // 判断参数是否合法
//protect
item.setLineNum(0);
item.setId(0);
String userId = userInfoHolder.getUser().getUserId();
item.setDataChangeCreatedBy(userId);
item.setDataChangeLastModifiedBy(userId);
item.setDataChangeCreatedTime(null);
item.setDataChangeLastModifiedTime(null);
// 生成item对象
// 调用configService去创建item
return configService.createItem(appId, Env.valueOf(env), clusterName, namespaceName, item);
}
3.createItem
public ItemDTO createItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException(
"namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
}
item.setNamespaceId(namespace.getId());
// 核心是itemApi
ItemDTO itemDTO = itemAPI.createItem(appId, env, clusterName, namespaceName, item);
Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE, String.format("%s+%s+%s+%s", appId, env, clusterName, namespaceName));
return itemDTO;
}
4.itemApi去创建item
AdminServiceAPI
public ItemDTO createItem(String appId, Env env, String clusterName, String namespace, ItemDTO item) {
return restTemplate.post(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items",
item, ItemDTO.class, appId, clusterName, namespace);// 发现是post了一个请求给AdminService,到指定url,然后那边去继续创建item的逻辑
}
5.AdminService接受到请求,去执行createItem逻辑
@PreAcquireNamespaceLock
@PostMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
public ItemDTO create(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
Item entity = BeanUtils.transform(Item.class, dto);
ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
throw new BadRequestException("item already exists");
} else {
entity = itemService.save(entity);
builder.createItem(entity);
}
dto = BeanUtils.transform(ItemDTO.class, entity);
Commit commit = new Commit();
commit.setAppId(appId);
commit.setClusterName(clusterName);
commit.setNamespaceName(namespaceName);
commit.setChangeSets(builder.build());
commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
commitService.save(commit);
return dto;
}
6.可以发现adminService是将配置项item序列化为json落到config配置库的Commit表里
MariaDB [ApolloPortalDB]> use ApolloConfigTestDb
ERROR 1049 (42000): Unknown database 'ApolloConfigTestDb'
MariaDB [ApolloPortalDB]> use ApolloConfigTestDB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [ApolloConfigTestDB]> show tables;
+------------------------------+
| Tables_in_ApolloConfigTestDB |
+------------------------------+
| App |
| AppNamespace |
| Audit |
| Cluster |
| Commit |
| GrayReleaseRule |
| Instance |
| InstanceConfig |
| Item |
| Namespace |
| NamespaceLock |
| Release |
| ReleaseHistory |
| ReleaseMessage |
| ServerConfig |
+------------------------------+
15 rows in set (0.000 sec)
MariaDB [ApolloConfigTestDB]> select * from Commit;
+----+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+-------------+---------------+---------+-----------+----------------------+------------------------+---------------------------+---------------------+
| Id | ChangeSets | AppId | ClusterName | NamespaceName | Comment | IsDeleted | DataChange_CreatedBy | DataChange_CreatedTime | DataChange_LastModifiedBy | DataChange_LastTime |
+----+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+-------------+---------------+---------+-----------+----------------------+------------------------+---------------------------+---------------------+
| 1 | {"createItems":[{"namespaceId":4,"key":"dubbo.registry","value":"zk://zk1.od.com","comment":"zk://zk1.od.com","lineNum":1,"id":1,"isDeleted":false,"dataChangeCreatedBy":"apollo","dataChangeCreatedTime":"2022-04-14 19:26:41","dataChangeLastModifiedBy":"apollo","dataChangeLastModifiedTime":"2022-04-14 19:26:41"}],"updateItems":[],"deleteItems":[]} | dubbo-demo-service2 | default | application | NULL | | apollo | 2022-04-14 19:26:41 | apollo | 2022-04-14 19:26:41 |
+----+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+-------------+---------------+---------+-----------+----------------------+------------------------+---------------------------+---------------------+
1 row in set (0.001 sec)
原创:做时间的朋友