Commit 8563b8b9 by Jason Song

Merge pull request #47 from lepdou/portal_optimize

重构portal api
parents babb3c6a ff0364c1
......@@ -77,6 +77,9 @@ public class AdminConfigServiceImpl implements AdminConfigService {
return null;
}
Version version = versionRepository.findById(versionId);
if (version == null){
return null;
}
return version.toDTO();
}
......
......@@ -15,12 +15,12 @@ INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations)
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('cluster2', 0, 11111, '{"6666.bar":"bar2222"}');
INSERT INTO RELEASESNAPSHOT (ClusterName, IsDeleted, ReleaseId, Configurations) VALUES ('default', 0, 11112, '{"6666.foo":"verson2.0", "6666.bar":"verson2.0","3333.foo":"1008","4444.bar":"99901"}');
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastModifiedTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k1', '6666.v1', 'comment1', 'lepdou', '2016-03-23 12:00:00', '王五', NOW(), 0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k1', '6666.v1', 'comment1', 'lepdou', '2016-03-23 12:00:00', '王五', NOW(), 0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastModifiedTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k2', '6666.v2', 'xxxx', 'lepdou', '2016-03-23 12:00:00', '王五1', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k2', '6666.v2', 'xxxx', 'lepdou', '2016-03-23 12:00:00', '王五1', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastModifiedTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k3', '6666.v3', 'yyyy', 'lepdou', '2016-03-23 12:00:00', '王五2', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 6666, '6666.k3', '6666.v3', 'yyyy', 'lepdou', '2016-03-23 12:00:00', '王五2', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastModifiedTime, IsDeleted) VALUES (100, 'default', 5555, '5555.k1', '5555.v11', 'zzzz', 'lepdou', '2016-03-23 12:00:00', '王五3', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (100, 'default', 5555, '5555.k1', '5555.v11', 'zzzz', 'lepdou', '2016-03-23 12:00:00', '王五3', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastModifiedTime, IsDeleted) VALUES (101, 'cluster1', 6666, '6666.k1', '6666.v122', 'qqqqq', 'lepdou', '2016-03-23 12:00:00', '王五4', NOW(),0);
INSERT INTO ConfigItem(ClusterId, ClusterName, AppId, Key, Value, comment, DataChange_CreatedBy, DataChange_CreatedTime, DataChange_LastModifiedBy, DataChange_LastTime, IsDeleted) VALUES (101, 'cluster1', 6666, '6666.k1', '6666.v122', 'qqqqq', 'lepdou', '2016-03-23 12:00:00', '王五4', NOW(),0);
package com.ctrip.apollo.portal;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestUtils {
private static RestTemplate restTemplate = new RestTemplate();
public static <T> T exchangeInGET(String url, Class<T> responseType) {
ResponseEntity<T> response =
restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Void>((Void) null), responseType);
return response.getBody();
}
}
package com.ctrip.apollo.portal.api;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.portal.service.ServiceLocator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
public class API {
@Autowired
protected ServiceLocator serviceLocator;
protected RestTemplate restTemplate = new RestTemplate();
public String getAdminServiceHost(Apollo.Env env){
//本地测试用
// return "http://localhost:8090";
return serviceLocator.getAdminService(env);
}
}
package com.ctrip.apollo.portal.api;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AdminServiceAPI {
@Service
public static class ConfigAPI extends API {
public static String CONFIG_RELEASE_API = "/configs/release/";
public ReleaseSnapshotDTO[] getConfigByReleaseId(Apollo.Env env, long releaseId) {
if (releaseId <= 0) {
return null;
}
return restTemplate.getForObject(getAdminServiceHost(env) + CONFIG_RELEASE_API + releaseId,
ReleaseSnapshotDTO[].class);
}
public ConfigItemDTO[] getLatestConfigItemsByClusters(Apollo.Env env, List<Long> clusterIds) {
if (clusterIds == null || clusterIds.size() == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
for (long clusterId : clusterIds) {
sb.append(clusterId).append(",");
}
return restTemplate.getForObject(getAdminServiceHost(env) + "/configs/latest?clusterIds=" + sb
.substring(0, sb.length() - 1), ConfigItemDTO[].class);
}
}
@Service
public static class ClusterAPI extends API {
public static String CLUSTER_APP_API = "/cluster/app/";
public ClusterDTO[] getClustersByApp(Apollo.Env env, long appId) {
if (appId <= 0) {
return null;
}
return restTemplate
.getForObject(getAdminServiceHost(env) + CLUSTER_APP_API + appId, ClusterDTO[].class);
}
}
@Service
public static class VersionAPI extends API{
public static String VERSION_API = "/version/";
public static String VERSION_APP_API = "/version/app/";
public VersionDTO getVersionById(Apollo.Env env, long versionId){
if (versionId <= 0){
return null;
}
return restTemplate.getForObject(getAdminServiceHost(env) + VERSION_API + versionId, VersionDTO.class);
}
public VersionDTO[] getVersionsByApp(Apollo.Env env, long appId){
if (appId <= 0){
return null;
}
return restTemplate.getForObject(getAdminServiceHost(env) + VERSION_APP_API + appId,
VersionDTO[].class);
}
}
}
package com.ctrip.apollo.portal.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -16,33 +19,38 @@ import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.dto.ConfigItemDTO;
import com.ctrip.apollo.core.dto.ReleaseSnapshotDTO;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.core.serivce.ApolloService;
import com.ctrip.apollo.portal.RestUtils;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.constants.PortalConstants;
import com.ctrip.apollo.portal.entity.AppConfigVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
@Service
public class ConfigService {
private Logger logger = LoggerFactory.getLogger(ConfigService.class);
@Autowired
private AdminServiceAPI.ConfigAPI configAPI;
@Autowired
private AdminServiceAPI.ClusterAPI clusterAPI;
@Autowired
private ServiceLocator serviceLocator;
private AdminServiceAPI.VersionAPI versionAPI;
private ObjectMapper objectMapper = new ObjectMapper();
public AppConfigVO loadReleaseConfig(Env env, long appId, long versionId) {
if (appId <= 0 || versionId <= 0) {
return null;
}
long releaseId = getReleaseIdFromVersionId(env, versionId);
if (releaseId == -1) {
return null;
}
String serviceHost = serviceLocator.getAdminService(env);
ReleaseSnapshotDTO[] releaseSnapShots = RestUtils
.exchangeInGET(serviceHost + "/configs/release/" + releaseId, ReleaseSnapshotDTO[].class);
ReleaseSnapshotDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(Env.DEV, releaseId);
if (releaseSnapShots == null || releaseSnapShots.length == 0) {
return null;
}
......@@ -63,9 +71,7 @@ public class ConfigService {
}
private long getReleaseIdFromVersionId(Env env, long versionId) {
String serviceHost = serviceLocator.getAdminService(env);
VersionDTO version =
RestUtils.exchangeInGET(serviceHost + "/version/" + versionId, VersionDTO.class);
VersionDTO version = versionAPI.getVersionById(env, versionId);
if (version == null) {
return -1;
}
......@@ -73,7 +79,7 @@ public class ConfigService {
}
private void collectDefaultClusterConfigs(long appId, ReleaseSnapshotDTO snapShot,
AppConfigVO appConfigVO) {
AppConfigVO appConfigVO) {
Map<Long, List<ConfigItemDTO>> groupedConfigs = groupConfigsByApp(snapShot.getConfigurations());
......@@ -136,7 +142,7 @@ public class ConfigService {
}
private void collectSpecialClusterConfigs(long appId, ReleaseSnapshotDTO snapShot,
AppConfigVO appConfigVO) {
AppConfigVO appConfigVO) {
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs =
appConfigVO.getOverrideClusterConfigs();
AppConfigVO.OverrideClusterConfig overrideClusterConfig =
......@@ -152,21 +158,17 @@ public class ConfigService {
return null;
}
String serviceHost = serviceLocator.getAdminService(env);
ClusterDTO[] clusters =
RestUtils.exchangeInGET(serviceHost + "/cluster/app/" + appId, ClusterDTO[].class);
ClusterDTO[] clusters = clusterAPI.getClustersByApp(env, appId);
if (clusters == null || clusters.length == 0) {
return null;
}
StringBuilder sb = new StringBuilder();
List<Long> clusterIds = new ArrayList<>(clusters.length);
for (ClusterDTO cluster : clusters) {
sb.append(cluster.getId()).append(",");
clusterIds.add(cluster.getId());
}
ConfigItemDTO[] configItems = RestUtils.exchangeInGET(
serviceHost + "/configs/latest?clusterIds=" + sb.substring(0, sb.length() - 1),
ConfigItemDTO[].class);
ConfigItemDTO[] configItems = configAPI.getLatestConfigItemsByClusters(env, clusterIds);
return buildAPPConfigVO(appId, Arrays.asList(configItems));
}
......@@ -203,7 +205,7 @@ public class ConfigService {
}
private void groupConfigByAppAndEnrichDTO(Map<String, List<ConfigItemDTO>> groupedClusterConfigs,
AppConfigVO appConfigVO) {
AppConfigVO appConfigVO) {
long appId = appConfigVO.getAppId();
List<ConfigItemDTO> defaultClusterConfigs = appConfigVO.getDefaultClusterConfigs();
......@@ -222,7 +224,7 @@ public class ConfigService {
if (Constants.DEFAULT_CLUSTER_NAME.equals(clusterName)) {
// default cluster configs
collectDefaultClusterConfigs(appId, clusterConfigs, defaultClusterConfigs,
overrideAppConfigs);
overrideAppConfigs);
} else {
// override cluster configs
collectSpecialClusterConfigs(clusterName, clusterConfigs, overrideClusterConfigs);
......@@ -231,8 +233,8 @@ public class ConfigService {
}
private void collectDefaultClusterConfigs(long appId, List<ConfigItemDTO> clusterConfigs,
List<ConfigItemDTO> defaultClusterConfigs,
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs) {
List<ConfigItemDTO> defaultClusterConfigs,
List<AppConfigVO.OverrideAppConfig> overrideAppConfigs) {
Map<Long, AppConfigVO.OverrideAppConfig> appIdMapOverrideAppConfig = null;
......@@ -261,7 +263,7 @@ public class ConfigService {
}
private void collectSpecialClusterConfigs(String clusterName, List<ConfigItemDTO> clusterConfigs,
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs) {
List<AppConfigVO.OverrideClusterConfig> overrideClusterConfigs) {
AppConfigVO.OverrideClusterConfig overrideClusterConfig =
new AppConfigVO.OverrideClusterConfig();
overrideClusterConfig.setClusterName(clusterName);
......
......@@ -30,8 +30,6 @@ public class ServiceLocator {
}
public String getAdminService(Env env) {
//本地测试用
// return "http://localhost:8090";
List<ApolloService> services = getAdminServices(env);
if (services.size() == 0) {
throw new RuntimeException("No available admin service");
......
......@@ -2,22 +2,28 @@ package com.ctrip.apollo.portal.service;
import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.RestUtils;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Service
public class VersionService {
@Autowired
private ServiceLocator serviceLocator;
private AdminServiceAPI.VersionAPI versionAPI;
public List<VersionDTO> findVersionsByApp(Apollo.Env env, long appId) {
return RestUtils
.exchangeInGET(serviceLocator.getAdminService(env) + "/version/app/" + appId, List.class);
VersionDTO[] versions = versionAPI.getVersionsByApp(env, appId);
if (versions == null || versions.length == 0){
return Collections.EMPTY_LIST;
}
return Arrays.asList(versions);
}
}
......@@ -13,7 +13,7 @@ spring:
logging:
level:
org.springframework.cloud: 'DEBUG'
file: /opt/logs/${ctrip.appid}/apollo-adminservice.log
file: /opt/logs/${ctrip.appid}/apollo-portal.log
ctrip:
appid: 100003173
......@@ -7,6 +7,10 @@ body {
padding-bottom: 50px;
}
a{
cursor: pointer;
}
.container {
width: 100%;
min-height: 550px;
......
......@@ -3,7 +3,7 @@
<!--环境nav-->
<ul class="nav nav-pills nav-justified">
<li ng-repeat="env in envs" ng-class="{active:configLocation.env == env}">
<a href="#" ng-click="switchEnv(env)">{{env}}</a>
<a ng-click="switchEnv(env)">{{env}}</a>
</li>
</ul>
......@@ -14,12 +14,12 @@
<ul class="nav nav-pills nav-stacked">
<li class="dropdown-header">未发布</li>
<li role="presentation" ng-class="{active:configLocation.versionId == -1}"
ng-click="switchVersion(-1)"><a href="#">latest</a></li>
ng-click="switchVersion(-1)"><a>latest</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">已发布</li>
<li ng-repeat="version in releaseVersions"
ng-class="{active:configLocation.versionId == version.id}">
<a href="#" ng-click="switchVersion(version.id)">{{version.name}}</a>
<a ng-click="switchVersion(version.id)">{{version.name}}</a>
</li>
</ul>
</div>
......@@ -31,13 +31,13 @@
<div class="col-md-6">项目的基本配置</div>
<div class="col-md-6" ng-show="!currentVersionIsRelease">
<p class="text-right">
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top" title="下载配置">
<span class="glyphicon glyphicon-save"
aria-hidden="true"></span>
</a>
&nbsp;
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top" title="添加配置">
<span class="glyphicon glyphicon-plus"
aria-hidden="true"></span>
......@@ -93,12 +93,12 @@
</td>
<td ng-show="!currentVersionIsRelease">
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top" title="修改">
<span class="glyphicon glyphicon-edit"
aria-hidden="true"></span>
</a>
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top" title="删除">
<span class="glyphicon glyphicon-remove"
aria-hidden="true"></span>
......@@ -110,17 +110,17 @@
<nav class="text-right">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<a aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a >1</a></li>
<li><a >2</a></li>
<li><a >3</a></li>
<li><a >4</a></li>
<li><a >5</a></li>
<li>
<a href="#" aria-label="Next">
<a aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
......@@ -135,18 +135,18 @@
<header class="panel-heading">
<div class="row">
<div class="col-md-6">重写<a
href="#">{{overrideAppConfig.appId}}</a>的配置
>{{overrideAppConfig.appId}}</a>的配置
</div>
<div class="col-md-6" ng-show="!currentVersionIsRelease">
<p class="text-right">
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top"
title="下载配置">
<span class="glyphicon glyphicon-save"
aria-hidden="true"></span>
</a>
&nbsp;
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top"
title="添加配置">
<span class="glyphicon glyphicon-plus"
......@@ -202,13 +202,13 @@
</td>
<td ng-show="!currentVersionIsRelease">
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top"
title="修改">
<span class="glyphicon glyphicon-edit"
aria-hidden="true"></span>
</a>
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top"
title="删除">
<span class="glyphicon glyphicon-remove"
......@@ -247,13 +247,13 @@
</div>
<div class="col-md-6" ng-show="!currentVersionIsRelease">
<p class="text-right">
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top" title="下载配置">
<span class="glyphicon glyphicon-save"
aria-hidden="true"></span>
</a>
&nbsp;
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top" title="添加配置">
<span class="glyphicon glyphicon-plus"
aria-hidden="true"></span>
......@@ -308,13 +308,13 @@
</td>
<td ng-show="!currentVersionIsRelease">
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top"
title="修改">
<span class="glyphicon glyphicon-edit"
aria-hidden="true"></span>
</a>
<a href="#" data-toggle="tooltip"
<a data-toggle="tooltip"
data-placement="top"
title="删除">
<span class="glyphicon glyphicon-remove"
......
server:
port: 8080
port: 8070
spring:
spring:
application:
name: apollo-portal
datasource:
url: jdbc:h2:file:~/fxapolloportaldb
username: sa
password: sa
url: jdbc:h2:mem:~/fxapolloportaldb
jpa:
show-sql: true
hibernate:
naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
logging:
level:
org.springframework.cloud: 'DEBUG'
file: /opt/logs/${ctrip.appid}/apollo-portal.log
ctrip:
appid: 100003173
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment