Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
apollo
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
apollo
Commits
7bc734b7
Commit
7bc734b7
authored
Mar 25, 2016
by
Jason Song
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #41 from yiming187/portal_update
Integrate service discovery in portal
parents
605dd2aa
1af04408
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
384 additions
and
371 deletions
+384
-371
AppConfigVO.java
...main/java/com/ctrip/apollo/portal/entity/AppConfigVO.java
+1
-1
Env.java
...rtal/src/main/java/com/ctrip/apollo/portal/enums/Env.java
+0
-33
ConfigService.java
...n/java/com/ctrip/apollo/portal/service/ConfigService.java
+267
-9
ServiceLocator.java
.../java/com/ctrip/apollo/portal/service/ServiceLocator.java
+54
-0
ConfigServiceImpl.java
...m/ctrip/apollo/portal/service/impl/ConfigServiceImpl.java
+0
-270
ConfigServiceTest.java
...va/com/ctrip/apollo/portal/service/ConfigServiceTest.java
+61
-57
pom.xml
pom.xml
+1
-1
No files found.
apollo-portal/src/main/java/com/ctrip/apollo/portal/entity/AppConfigVO.java
View file @
7bc734b7
package
com
.
ctrip
.
apollo
.
portal
.
entity
;
import
com.ctrip.apollo.Apollo.Env
;
import
com.ctrip.apollo.core.dto.ConfigItemDTO
;
import
com.ctrip.apollo.portal.enums.Env
;
import
java.util.LinkedList
;
import
java.util.List
;
...
...
apollo-portal/src/main/java/com/ctrip/apollo/portal/enums/Env.java
deleted
100644 → 0
View file @
605dd2aa
package
com
.
ctrip
.
apollo
.
portal
.
enums
;
public
enum
Env
{
DEV
(
"dev"
),
FWS
(
"fws"
),
FAT
(
"fat"
),
UAT
(
"uat"
),
LPT
(
"lpt"
),
PROD
(
"prod"
),
TOOLS
(
"tools"
),
UN_KNOW
(
""
);
private
String
value
;
Env
(
String
value
)
{
this
.
value
=
value
;
}
public
static
Env
valueFrom
(
String
env
)
{
if
(
env
==
null
||
""
.
equals
(
env
))
{
return
UN_KNOW
;
}
else
if
(
"dev"
.
equals
(
env
))
{
return
DEV
;
}
else
if
(
"fws"
.
equals
(
env
))
{
return
FWS
;
}
else
if
(
"fat"
.
equals
(
env
))
{
return
FAT
;
}
else
if
(
"uat"
.
equals
(
env
))
{
return
UAT
;
}
else
if
(
"prod"
.
equals
(
env
))
{
return
PROD
;
}
else
if
(
"tools"
.
equals
(
env
))
{
return
TOOLS
;
}
else
{
return
UN_KNOW
;
}
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/ConfigService.java
View file @
7bc734b7
package
com
.
ctrip
.
apollo
.
portal
.
service
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
com.ctrip.apollo.Apollo.Env
;
import
com.ctrip.apollo.core.Constants
;
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.constants.PortalConstants
;
import
com.ctrip.apollo.portal.entity.AppConfigVO
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.google.common.collect.Maps
;
public
interface
ConfigService
{
@Service
public
class
ConfigService
{
/**
* load config info by appId and versionId
*/
AppConfigVO
loadReleaseConfig
(
long
appId
,
long
versionId
);
@Autowired
private
ServiceLocator
serviceLocator
;
private
ObjectMapper
objectMapper
=
new
ObjectMapper
();
public
String
getAdminServiceUrl
()
{
List
<
ApolloService
>
services
=
serviceLocator
.
getAdminServices
(
Env
.
DEV
);
if
(
services
.
size
()
==
0
)
{
throw
new
RuntimeException
(
"No available admin service"
);
}
return
services
.
get
(
0
).
getHomepageUrl
();
}
public
AppConfigVO
loadReleaseConfig
(
long
appId
,
long
versionId
)
{
if
(
appId
<=
0
||
versionId
<=
0
)
{
return
null
;
}
long
releaseId
=
getReleaseIdFromVersionId
(
versionId
);
String
serviceHost
=
getAdminServiceUrl
();
ReleaseSnapshotDTO
[]
releaseSnapShots
=
RestUtils
.
exchangeInGET
(
serviceHost
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
);
if
(
releaseSnapShots
==
null
||
releaseSnapShots
.
length
==
0
)
{
return
null
;
}
AppConfigVO
appConfigVO
=
AppConfigVO
.
newInstance
(
appId
,
versionId
);
for
(
ReleaseSnapshotDTO
snapShot
:
releaseSnapShots
)
{
// default cluster
if
(
Constants
.
DEFAULT_CLUSTER_NAME
.
equals
(
snapShot
.
getClusterName
()))
{
collectDefaultClusterConfigs
(
appId
,
snapShot
,
appConfigVO
);
}
else
{
// cluster special configs
collectSpecialClusterConfigs
(
appId
,
snapShot
,
appConfigVO
);
}
}
return
appConfigVO
;
}
private
long
getReleaseIdFromVersionId
(
long
versionId
)
{
String
serviceHost
=
getAdminServiceUrl
();
VersionDTO
version
=
RestUtils
.
exchangeInGET
(
serviceHost
+
"/version/"
+
versionId
,
VersionDTO
.
class
);
if
(
version
==
null
)
{
return
-
1
;
}
return
version
.
getReleaseId
();
}
private
void
collectDefaultClusterConfigs
(
long
appId
,
ReleaseSnapshotDTO
snapShot
,
AppConfigVO
appConfigVO
)
{
Map
<
Long
,
List
<
ConfigItemDTO
>>
groupedConfigs
=
groupConfigsByApp
(
snapShot
.
getConfigurations
());
List
<
AppConfigVO
.
OverrideAppConfig
>
overrideAppConfigs
=
appConfigVO
.
getOverrideAppConfigs
();
for
(
Map
.
Entry
<
Long
,
List
<
ConfigItemDTO
>>
entry
:
groupedConfigs
.
entrySet
())
{
long
configAppId
=
entry
.
getKey
();
List
<
ConfigItemDTO
>
kvs
=
entry
.
getValue
();
if
(
configAppId
==
appId
)
{
appConfigVO
.
setDefaultClusterConfigs
(
kvs
);
}
else
{
AppConfigVO
.
OverrideAppConfig
overrideAppConfig
=
new
AppConfigVO
.
OverrideAppConfig
();
overrideAppConfig
.
setAppId
(
configAppId
);
overrideAppConfig
.
setConfigs
(
kvs
);
overrideAppConfigs
.
add
(
overrideAppConfig
);
}
}
}
/**
*
* @param appId
* @return
* appId -> List<KV>
*/
AppConfigVO
loadLatestConfig
(
long
appId
);
private
Map
<
Long
,
List
<
ConfigItemDTO
>>
groupConfigsByApp
(
String
configJson
)
{
if
(
configJson
==
null
||
""
.
equals
(
configJson
))
{
return
Maps
.
newHashMap
();
}
Map
<
Long
,
List
<
ConfigItemDTO
>>
appIdMapKVs
=
new
HashMap
<>();
String
key
;
Object
value
;
Map
<
String
,
String
>
kvMaps
=
null
;
try
{
kvMaps
=
objectMapper
.
readValue
(
configJson
,
Map
.
class
);
}
catch
(
IOException
e
)
{
// todo log
}
for
(
Map
.
Entry
<
String
,
String
>
entry
:
kvMaps
.
entrySet
())
{
key
=
entry
.
getKey
();
value
=
entry
.
getValue
();
Long
appId
=
getAppIdFromKey
(
key
);
List
<
ConfigItemDTO
>
kvs
=
appIdMapKVs
.
get
(
appId
);
if
(
kvs
==
null
)
{
kvs
=
new
LinkedList
<>();
appIdMapKVs
.
put
(
appId
,
kvs
);
}
kvs
.
add
(
new
ConfigItemDTO
(
key
,
value
.
toString
()));
}
return
appIdMapKVs
;
}
private
Long
getAppIdFromKey
(
String
key
)
{
return
Long
.
valueOf
(
key
.
substring
(
0
,
key
.
indexOf
(
"."
)));
}
private
void
collectSpecialClusterConfigs
(
long
appId
,
ReleaseSnapshotDTO
snapShot
,
AppConfigVO
appConfigVO
)
{
List
<
AppConfigVO
.
OverrideClusterConfig
>
overrideClusterConfigs
=
appConfigVO
.
getOverrideClusterConfigs
();
AppConfigVO
.
OverrideClusterConfig
overrideClusterConfig
=
new
AppConfigVO
.
OverrideClusterConfig
();
overrideClusterConfig
.
setClusterName
(
snapShot
.
getClusterName
());
// todo step1: cluster special config can't override other app config
overrideClusterConfig
.
setConfigs
(
groupConfigsByApp
(
snapShot
.
getConfigurations
()).
get
(
appId
));
overrideClusterConfigs
.
add
(
overrideClusterConfig
);
}
public
AppConfigVO
loadLatestConfig
(
long
appId
)
{
if
(
appId
<=
0
)
{
return
null
;
}
String
serviceHost
=
getAdminServiceUrl
();
ClusterDTO
[]
clusters
=
RestUtils
.
exchangeInGET
(
serviceHost
+
"/cluster/app/"
+
appId
,
ClusterDTO
[].
class
);
if
(
clusters
==
null
||
clusters
.
length
==
0
)
{
return
null
;
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
ClusterDTO
cluster
:
clusters
)
{
sb
.
append
(
cluster
.
getId
()).
append
(
","
);
}
ConfigItemDTO
[]
configItems
=
RestUtils
.
exchangeInGET
(
serviceHost
+
"/configs/latest?clusterIds="
+
sb
.
substring
(
0
,
sb
.
length
()
-
1
),
ConfigItemDTO
[].
class
);
return
buildAPPConfigVO
(
appId
,
Arrays
.
asList
(
configItems
));
}
private
AppConfigVO
buildAPPConfigVO
(
long
appId
,
List
<
ConfigItemDTO
>
configItems
)
{
if
(
configItems
==
null
||
configItems
.
size
()
==
0
)
{
return
null
;
}
Map
<
String
,
List
<
ConfigItemDTO
>>
groupedClusterConfigs
=
groupConfigByCluster
(
configItems
);
AppConfigVO
appConfigVO
=
AppConfigVO
.
newInstance
(
appId
,
PortalConstants
.
LASTEST_VERSION_ID
);
groupConfigByAppAndEnrichDTO
(
groupedClusterConfigs
,
appConfigVO
);
return
appConfigVO
;
}
private
Map
<
String
,
List
<
ConfigItemDTO
>>
groupConfigByCluster
(
List
<
ConfigItemDTO
>
configItems
)
{
Map
<
String
,
List
<
ConfigItemDTO
>>
groupedClusterConfigs
=
new
HashMap
<>();
String
clusterName
;
for
(
ConfigItemDTO
configItem
:
configItems
)
{
clusterName
=
configItem
.
getClusterName
();
List
<
ConfigItemDTO
>
clusterConfigs
=
groupedClusterConfigs
.
get
(
clusterName
);
if
(
clusterConfigs
==
null
)
{
clusterConfigs
=
new
LinkedList
<>();
groupedClusterConfigs
.
put
(
clusterName
,
clusterConfigs
);
}
clusterConfigs
.
add
(
configItem
);
}
return
groupedClusterConfigs
;
}
private
void
groupConfigByAppAndEnrichDTO
(
Map
<
String
,
List
<
ConfigItemDTO
>>
groupedClusterConfigs
,
AppConfigVO
appConfigVO
)
{
long
appId
=
appConfigVO
.
getAppId
();
List
<
ConfigItemDTO
>
defaultClusterConfigs
=
appConfigVO
.
getDefaultClusterConfigs
();
List
<
AppConfigVO
.
OverrideAppConfig
>
overrideAppConfigs
=
appConfigVO
.
getOverrideAppConfigs
();
List
<
AppConfigVO
.
OverrideClusterConfig
>
overrideClusterConfigs
=
appConfigVO
.
getOverrideClusterConfigs
();
String
clusterName
;
List
<
ConfigItemDTO
>
clusterConfigs
;
for
(
Map
.
Entry
<
String
,
List
<
ConfigItemDTO
>>
entry
:
groupedClusterConfigs
.
entrySet
())
{
clusterName
=
entry
.
getKey
();
clusterConfigs
=
entry
.
getValue
();
if
(
Constants
.
DEFAULT_CLUSTER_NAME
.
equals
(
clusterName
))
{
// default cluster configs
collectDefaultClusterConfigs
(
appId
,
clusterConfigs
,
defaultClusterConfigs
,
overrideAppConfigs
);
}
else
{
// override cluster configs
collectSpecialClusterConfigs
(
clusterName
,
clusterConfigs
,
overrideClusterConfigs
);
}
}
}
private
void
collectDefaultClusterConfigs
(
long
appId
,
List
<
ConfigItemDTO
>
clusterConfigs
,
List
<
ConfigItemDTO
>
defaultClusterConfigs
,
List
<
AppConfigVO
.
OverrideAppConfig
>
overrideAppConfigs
)
{
Map
<
Long
,
AppConfigVO
.
OverrideAppConfig
>
appIdMapOverrideAppConfig
=
null
;
for
(
ConfigItemDTO
config
:
clusterConfigs
)
{
long
targetAppId
=
config
.
getAppId
();
if
(
appId
==
targetAppId
)
{
// app self's configs
defaultClusterConfigs
.
add
(
config
);
}
else
{
// override other app configs
if
(
appIdMapOverrideAppConfig
==
null
)
{
appIdMapOverrideAppConfig
=
new
HashMap
<>();
}
AppConfigVO
.
OverrideAppConfig
overrideAppConfig
=
appIdMapOverrideAppConfig
.
get
(
targetAppId
);
if
(
overrideAppConfig
==
null
)
{
overrideAppConfig
=
new
AppConfigVO
.
OverrideAppConfig
();
appIdMapOverrideAppConfig
.
put
(
targetAppId
,
overrideAppConfig
);
overrideAppConfigs
.
add
(
overrideAppConfig
);
}
overrideAppConfig
.
setAppId
(
targetAppId
);
overrideAppConfig
.
addConfig
(
config
);
}
}
}
private
void
collectSpecialClusterConfigs
(
String
clusterName
,
List
<
ConfigItemDTO
>
clusterConfigs
,
List
<
AppConfigVO
.
OverrideClusterConfig
>
overrideClusterConfigs
)
{
AppConfigVO
.
OverrideClusterConfig
overrideClusterConfig
=
new
AppConfigVO
.
OverrideClusterConfig
();
overrideClusterConfig
.
setClusterName
(
clusterName
);
overrideClusterConfig
.
setConfigs
(
clusterConfigs
);
overrideClusterConfigs
.
add
(
overrideClusterConfig
);
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/ServiceLocator.java
0 → 100644
View file @
7bc734b7
package
com
.
ctrip
.
apollo
.
portal
.
service
;
import
java.net.URI
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
import
com.ctrip.apollo.Apollo.Env
;
import
com.ctrip.apollo.core.MetaDomainConsts
;
import
com.ctrip.apollo.core.serivce.ApolloService
;
/**
*
* @author liuym
*
*/
@Service
public
class
ServiceLocator
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
ServiceLocator
.
class
);
private
RestTemplate
restTemplate
=
new
RestTemplate
();
private
List
<
ApolloService
>
serviceCaches
=
new
ArrayList
<>();
public
List
<
ApolloService
>
getAdminServices
(
Env
env
)
{
return
getServices
(
env
,
"admin"
);
}
public
List
<
ApolloService
>
getConfigServices
(
Env
env
)
{
return
getServices
(
env
,
"config"
);
}
private
List
<
ApolloService
>
getServices
(
Env
env
,
String
serviceUrl
)
{
String
domainName
=
MetaDomainConsts
.
getDomain
(
env
);
String
url
=
domainName
+
"/services/"
+
serviceUrl
;
try
{
ApolloService
[]
services
=
restTemplate
.
getForObject
(
new
URI
(
url
),
ApolloService
[].
class
);
if
(
services
!=
null
&&
services
.
length
>
0
)
{
serviceCaches
.
clear
();
for
(
ApolloService
service
:
services
)
{
serviceCaches
.
add
(
service
);
}
}
}
catch
(
Exception
ex
)
{
logger
.
warn
(
ex
.
getMessage
());
}
return
serviceCaches
;
}
}
apollo-portal/src/main/java/com/ctrip/apollo/portal/service/impl/ConfigServiceImpl.java
deleted
100644 → 0
View file @
605dd2aa
package
com
.
ctrip
.
apollo
.
portal
.
service
.
impl
;
import
com.google.common.collect.Maps
;
import
com.ctrip.apollo.core.Constants
;
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.portal.RestUtils
;
import
com.ctrip.apollo.portal.constants.PortalConstants
;
import
com.ctrip.apollo.portal.entity.AppConfigVO
;
import
com.ctrip.apollo.portal.service.ConfigService
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.springframework.stereotype.Service
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
@Service
public
class
ConfigServiceImpl
implements
ConfigService
{
public
static
final
String
ADMIN_SERVICE_HOST
=
"http://localhost:8090"
;
private
ObjectMapper
objectMapper
=
new
ObjectMapper
();
@Override
public
AppConfigVO
loadReleaseConfig
(
long
appId
,
long
versionId
)
{
if
(
appId
<=
0
||
versionId
<=
0
)
{
return
null
;
}
long
releaseId
=
getReleaseIdFromVersionId
(
versionId
);
ReleaseSnapshotDTO
[]
releaseSnapShots
=
RestUtils
.
exchangeInGET
(
ADMIN_SERVICE_HOST
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
);
if
(
releaseSnapShots
==
null
||
releaseSnapShots
.
length
==
0
)
{
return
null
;
}
AppConfigVO
appConfigVO
=
AppConfigVO
.
newInstance
(
appId
,
versionId
);
for
(
ReleaseSnapshotDTO
snapShot
:
releaseSnapShots
)
{
//default cluster
if
(
Constants
.
DEFAULT_CLUSTER_NAME
.
equals
(
snapShot
.
getClusterName
()))
{
collectDefaultClusterConfigs
(
appId
,
snapShot
,
appConfigVO
);
}
else
{
//cluster special configs
collectSpecialClusterConfigs
(
appId
,
snapShot
,
appConfigVO
);
}
}
return
appConfigVO
;
}
private
long
getReleaseIdFromVersionId
(
long
versionId
)
{
VersionDTO
version
=
RestUtils
.
exchangeInGET
(
ADMIN_SERVICE_HOST
+
"/version/"
+
versionId
,
VersionDTO
.
class
);
if
(
version
==
null
)
{
return
-
1
;
}
return
version
.
getReleaseId
();
}
private
void
collectDefaultClusterConfigs
(
long
appId
,
ReleaseSnapshotDTO
snapShot
,
AppConfigVO
appConfigVO
)
{
Map
<
Long
,
List
<
ConfigItemDTO
>>
groupedConfigs
=
groupConfigsByApp
(
snapShot
.
getConfigurations
());
List
<
AppConfigVO
.
OverrideAppConfig
>
overrideAppConfigs
=
appConfigVO
.
getOverrideAppConfigs
();
for
(
Map
.
Entry
<
Long
,
List
<
ConfigItemDTO
>>
entry
:
groupedConfigs
.
entrySet
())
{
long
configAppId
=
entry
.
getKey
();
List
<
ConfigItemDTO
>
kvs
=
entry
.
getValue
();
if
(
configAppId
==
appId
)
{
appConfigVO
.
setDefaultClusterConfigs
(
kvs
);
}
else
{
AppConfigVO
.
OverrideAppConfig
overrideAppConfig
=
new
AppConfigVO
.
OverrideAppConfig
();
overrideAppConfig
.
setAppId
(
configAppId
);
overrideAppConfig
.
setConfigs
(
kvs
);
overrideAppConfigs
.
add
(
overrideAppConfig
);
}
}
}
/**
* appId -> List<KV>
*/
private
Map
<
Long
,
List
<
ConfigItemDTO
>>
groupConfigsByApp
(
String
configJson
)
{
if
(
configJson
==
null
||
""
.
equals
(
configJson
))
{
return
Maps
.
newHashMap
();
}
Map
<
Long
,
List
<
ConfigItemDTO
>>
appIdMapKVs
=
new
HashMap
<>();
String
key
;
Object
value
;
Map
<
String
,
String
>
kvMaps
=
null
;
try
{
kvMaps
=
objectMapper
.
readValue
(
configJson
,
Map
.
class
);
}
catch
(
IOException
e
)
{
//todo log
}
for
(
Map
.
Entry
<
String
,
String
>
entry
:
kvMaps
.
entrySet
())
{
key
=
entry
.
getKey
();
value
=
entry
.
getValue
();
Long
appId
=
getAppIdFromKey
(
key
);
List
<
ConfigItemDTO
>
kvs
=
appIdMapKVs
.
get
(
appId
);
if
(
kvs
==
null
)
{
kvs
=
new
LinkedList
<>();
appIdMapKVs
.
put
(
appId
,
kvs
);
}
kvs
.
add
(
new
ConfigItemDTO
(
key
,
value
.
toString
()));
}
return
appIdMapKVs
;
}
private
Long
getAppIdFromKey
(
String
key
)
{
return
Long
.
valueOf
(
key
.
substring
(
0
,
key
.
indexOf
(
"."
)));
}
private
void
collectSpecialClusterConfigs
(
long
appId
,
ReleaseSnapshotDTO
snapShot
,
AppConfigVO
appConfigVO
)
{
List
<
AppConfigVO
.
OverrideClusterConfig
>
overrideClusterConfigs
=
appConfigVO
.
getOverrideClusterConfigs
();
AppConfigVO
.
OverrideClusterConfig
overrideClusterConfig
=
new
AppConfigVO
.
OverrideClusterConfig
();
overrideClusterConfig
.
setClusterName
(
snapShot
.
getClusterName
());
//todo step1: cluster special config can't override other app config
overrideClusterConfig
.
setConfigs
(
groupConfigsByApp
(
snapShot
.
getConfigurations
()).
get
(
appId
));
overrideClusterConfigs
.
add
(
overrideClusterConfig
);
}
@Override
public
AppConfigVO
loadLatestConfig
(
long
appId
)
{
if
(
appId
<=
0
)
{
return
null
;
}
ClusterDTO
[]
clusters
=
RestUtils
.
exchangeInGET
(
ADMIN_SERVICE_HOST
+
"/cluster/app/"
+
appId
,
ClusterDTO
[].
class
);
if
(
clusters
==
null
||
clusters
.
length
==
0
)
{
return
null
;
}
StringBuilder
sb
=
new
StringBuilder
();
for
(
ClusterDTO
cluster
:
clusters
)
{
sb
.
append
(
cluster
.
getId
()).
append
(
","
);
}
ConfigItemDTO
[]
configItems
=
RestUtils
.
exchangeInGET
(
ADMIN_SERVICE_HOST
+
"/configs/latest?clusterIds="
+
sb
.
substring
(
0
,
sb
.
length
()
-
1
),
ConfigItemDTO
[].
class
);
return
buildAPPConfigVO
(
appId
,
Arrays
.
asList
(
configItems
));
}
private
AppConfigVO
buildAPPConfigVO
(
long
appId
,
List
<
ConfigItemDTO
>
configItems
)
{
if
(
configItems
==
null
||
configItems
.
size
()
==
0
)
{
return
null
;
}
Map
<
String
,
List
<
ConfigItemDTO
>>
groupedClusterConfigs
=
groupConfigByCluster
(
configItems
);
AppConfigVO
appConfigVO
=
AppConfigVO
.
newInstance
(
appId
,
PortalConstants
.
LASTEST_VERSION_ID
);
groupConfigByAppAndEnrichDTO
(
groupedClusterConfigs
,
appConfigVO
);
return
appConfigVO
;
}
private
Map
<
String
,
List
<
ConfigItemDTO
>>
groupConfigByCluster
(
List
<
ConfigItemDTO
>
configItems
)
{
Map
<
String
,
List
<
ConfigItemDTO
>>
groupedClusterConfigs
=
new
HashMap
<>();
String
clusterName
;
for
(
ConfigItemDTO
configItem
:
configItems
)
{
clusterName
=
configItem
.
getClusterName
();
List
<
ConfigItemDTO
>
clusterConfigs
=
groupedClusterConfigs
.
get
(
clusterName
);
if
(
clusterConfigs
==
null
)
{
clusterConfigs
=
new
LinkedList
<>();
groupedClusterConfigs
.
put
(
clusterName
,
clusterConfigs
);
}
clusterConfigs
.
add
(
configItem
);
}
return
groupedClusterConfigs
;
}
private
void
groupConfigByAppAndEnrichDTO
(
Map
<
String
,
List
<
ConfigItemDTO
>>
groupedClusterConfigs
,
AppConfigVO
appConfigVO
)
{
long
appId
=
appConfigVO
.
getAppId
();
List
<
ConfigItemDTO
>
defaultClusterConfigs
=
appConfigVO
.
getDefaultClusterConfigs
();
List
<
AppConfigVO
.
OverrideAppConfig
>
overrideAppConfigs
=
appConfigVO
.
getOverrideAppConfigs
();
List
<
AppConfigVO
.
OverrideClusterConfig
>
overrideClusterConfigs
=
appConfigVO
.
getOverrideClusterConfigs
();
String
clusterName
;
List
<
ConfigItemDTO
>
clusterConfigs
;
for
(
Map
.
Entry
<
String
,
List
<
ConfigItemDTO
>>
entry
:
groupedClusterConfigs
.
entrySet
())
{
clusterName
=
entry
.
getKey
();
clusterConfigs
=
entry
.
getValue
();
if
(
Constants
.
DEFAULT_CLUSTER_NAME
.
equals
(
clusterName
))
{
//default cluster configs
collectDefaultClusterConfigs
(
appId
,
clusterConfigs
,
defaultClusterConfigs
,
overrideAppConfigs
);
}
else
{
//override cluster configs
collectSpecialClusterConfigs
(
clusterName
,
clusterConfigs
,
overrideClusterConfigs
);
}
}
}
private
void
collectDefaultClusterConfigs
(
long
appId
,
List
<
ConfigItemDTO
>
clusterConfigs
,
List
<
ConfigItemDTO
>
defaultClusterConfigs
,
List
<
AppConfigVO
.
OverrideAppConfig
>
overrideAppConfigs
)
{
Map
<
Long
,
AppConfigVO
.
OverrideAppConfig
>
appIdMapOverrideAppConfig
=
null
;
for
(
ConfigItemDTO
config
:
clusterConfigs
)
{
long
targetAppId
=
config
.
getAppId
();
if
(
appId
==
targetAppId
)
{
//app self's configs
defaultClusterConfigs
.
add
(
config
);
}
else
{
//override other app configs
if
(
appIdMapOverrideAppConfig
==
null
)
{
appIdMapOverrideAppConfig
=
new
HashMap
<>();
}
AppConfigVO
.
OverrideAppConfig
overrideAppConfig
=
appIdMapOverrideAppConfig
.
get
(
targetAppId
);
if
(
overrideAppConfig
==
null
)
{
overrideAppConfig
=
new
AppConfigVO
.
OverrideAppConfig
();
appIdMapOverrideAppConfig
.
put
(
targetAppId
,
overrideAppConfig
);
overrideAppConfigs
.
add
(
overrideAppConfig
);
}
overrideAppConfig
.
setAppId
(
targetAppId
);
overrideAppConfig
.
addConfig
(
config
);
}
}
}
private
void
collectSpecialClusterConfigs
(
String
clusterName
,
List
<
ConfigItemDTO
>
clusterConfigs
,
List
<
AppConfigVO
.
OverrideClusterConfig
>
overrideClusterConfigs
)
{
AppConfigVO
.
OverrideClusterConfig
overrideClusterConfig
=
new
AppConfigVO
.
OverrideClusterConfig
();
overrideClusterConfig
.
setClusterName
(
clusterName
);
overrideClusterConfig
.
setConfigs
(
clusterConfigs
);
overrideClusterConfigs
.
add
(
overrideClusterConfig
);
}
}
apollo-portal/src/test/java/com/ctrip/apollo/portal/service/ConfigServiceTest.java
View file @
7bc734b7
package
com
.
ctrip
.
apollo
.
portal
.
service
;
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.portal.RestUtils
;
import
com.ctrip.apollo.portal.constants.PortalConstants
;
import
com.ctrip.apollo.portal.entity.AppConfigVO
;
import
com.ctrip.apollo.portal.service.impl.ConfigServiceImpl
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.InjectMocks
;
import
org.mockito.Mock
;
import
org.mockito.Mockito
;
import
org.mockito.Spy
;
import
org.mockito.runners.MockitoJUnitRunner
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -21,10 +24,15 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.test.util.ReflectionTestUtils
;
import
org.springframework.web.client.RestTemplate
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
com.ctrip.apollo.Apollo.Env
;
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.constants.PortalConstants
;
import
com.ctrip.apollo.portal.entity.AppConfigVO
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
ConfigServiceTest
{
...
...
@@ -40,12 +48,20 @@ public class ConfigServiceTest {
@Mock
private
ResponseEntity
configItemResponse
;
private
ConfigServiceImpl
configService
;
@InjectMocks
private
ConfigService
configService
;
@Spy
private
ServiceLocator
serviceLocator
;
@Before
public
void
setUp
()
{
ReflectionTestUtils
.
setField
(
RestUtils
.
class
,
"restTemplate"
,
restTemplate
);
configService
=
new
ConfigServiceImpl
();
ApolloService
defaultAdminService
=
new
ApolloService
();
defaultAdminService
.
setHomepageUrl
(
"http://localhost:8090"
);
List
<
ApolloService
>
services
=
new
ArrayList
<>();
services
.
add
(
defaultAdminService
);
Mockito
.
doReturn
(
services
).
when
(
serviceLocator
).
getAdminServices
(
Env
.
DEV
);
}
@Test
...
...
@@ -57,11 +73,11 @@ public class ConfigServiceTest {
VersionDTO
someVersion
=
assembleVersion
(
appId
,
"1.0"
,
releaseId
);
ReleaseSnapshotDTO
[]
someReleaseSnapShots
=
assembleReleaseSnapShots
();
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
someReleaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
someReleaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
AppConfigVO
appConfigVO
=
configService
.
loadReleaseConfig
(
appId
,
versionId
);
...
...
@@ -80,15 +96,14 @@ public class ConfigServiceTest {
VersionDTO
someVersion
=
assembleVersion
(
appId
,
"1.0"
,
releaseId
);
ReleaseSnapshotDTO
[]
releaseSnapShots
=
new
ReleaseSnapshotDTO
[
1
];
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
releaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
releaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
AppConfigVO
appConfigVO
=
configService
.
loadReleaseConfig
(
appId
,
versionId
);
...
...
@@ -106,15 +121,14 @@ public class ConfigServiceTest {
long
releaseId
=
11111
;
VersionDTO
someVersion
=
assembleVersion
(
appId
,
"1.0"
,
releaseId
);
ReleaseSnapshotDTO
[]
releaseSnapShots
=
new
ReleaseSnapshotDTO
[
1
];
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\", \"5555.bar\":\"demo2\", \"22.bar\":\"demo2\"}"
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
releaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
releaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
AppConfigVO
appConfigVO
=
configService
.
loadReleaseConfig
(
appId
,
versionId
);
...
...
@@ -132,18 +146,16 @@ public class ConfigServiceTest {
long
releaseId
=
11111
;
VersionDTO
someVersion
=
assembleVersion
(
appId
,
"1.0"
,
releaseId
);
ReleaseSnapshotDTO
[]
releaseSnapShots
=
new
ReleaseSnapshotDTO
[
2
];
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"
);
releaseSnapShots
[
1
]
=
assembleReleaseSnapShot
(
11112
,
"cluster1"
,
releaseSnapShots
[
1
]
=
assembleReleaseSnapShot
(
11112
,
"cluster1"
,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\"}"
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
releaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/configs/release/"
+
releaseId
,
ReleaseSnapshotDTO
[].
class
,
releaseSnapShots
,
releaseSnapShotResponse
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/version/"
+
versionId
,
VersionDTO
.
class
,
someVersion
,
versionResponse
);
AppConfigVO
appConfigVO
=
configService
.
loadReleaseConfig
(
appId
,
versionId
);
...
...
@@ -160,12 +172,11 @@ public class ConfigServiceTest {
ClusterDTO
[]
someClusters
=
assembleClusters
();
ConfigItemDTO
[]
someConfigItem
=
assembleConfigItems
();
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/cluster/app/"
+
appId
,
ClusterDTO
[].
class
,
someClusters
,
clusterResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/cluster/app/"
+
appId
,
ClusterDTO
[].
class
,
someClusters
,
clusterResponse
);
restInvoke
(
ConfigServiceImpl
.
ADMIN_SERVICE_HOST
+
"/configs/latest?clusterIds=100,101"
,
ConfigItemDTO
[].
class
,
someConfigItem
,
configItemResponse
);
restInvoke
(
configService
.
getAdminServiceUrl
()
+
"/configs/latest?clusterIds=100,101"
,
ConfigItemDTO
[].
class
,
someConfigItem
,
configItemResponse
);
AppConfigVO
appConfigVO
=
configService
.
loadLatestConfig
(
appId
);
...
...
@@ -195,13 +206,10 @@ public class ConfigServiceTest {
private
ReleaseSnapshotDTO
[]
assembleReleaseSnapShots
()
{
ReleaseSnapshotDTO
[]
releaseSnapShots
=
new
ReleaseSnapshotDTO
[
3
];
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
releaseSnapShots
[
0
]
=
assembleReleaseSnapShot
(
11111
,
"default-cluster-name"
,
"{\"6666.foo\":\"demo1\", \"6666.bar\":\"demo2\",\"3333.foo\":\"1008\",\"4444.bar\":\"99901\"}"
);
releaseSnapShots
[
1
]
=
assembleReleaseSnapShot
(
11111
,
"cluster1"
,
"{\"6666.foo\":\"demo1\"}"
);
releaseSnapShots
[
2
]
=
assembleReleaseSnapShot
(
11111
,
"cluster2"
,
"{\"6666.bar\":\"bar2222\"}"
);
releaseSnapShots
[
1
]
=
assembleReleaseSnapShot
(
11111
,
"cluster1"
,
"{\"6666.foo\":\"demo1\"}"
);
releaseSnapShots
[
2
]
=
assembleReleaseSnapShot
(
11111
,
"cluster2"
,
"{\"6666.bar\":\"bar2222\"}"
);
return
releaseSnapShots
;
}
...
...
@@ -231,14 +239,10 @@ public class ConfigServiceTest {
private
ConfigItemDTO
[]
assembleConfigItems
()
{
ConfigItemDTO
[]
configItems
=
new
ConfigItemDTO
[
5
];
configItems
[
0
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
6666
,
"6666.k1"
,
"6666.v1"
);
configItems
[
1
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
6666
,
"6666.k2"
,
"6666.v2"
);
configItems
[
2
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
6666
,
"6666.k3"
,
"6666.v3"
);
configItems
[
3
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
5555
,
"5555.k1"
,
"5555.v1"
);
configItems
[
0
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
6666
,
"6666.k1"
,
"6666.v1"
);
configItems
[
1
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
6666
,
"6666.k2"
,
"6666.v2"
);
configItems
[
2
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
6666
,
"6666.k3"
,
"6666.v3"
);
configItems
[
3
]
=
assembleConfigItem
(
100
,
"default-cluster-name"
,
5555
,
"5555.k1"
,
"5555.v1"
);
configItems
[
4
]
=
assembleConfigItem
(
101
,
"cluster1"
,
6666
,
"6666.k1"
,
"6666.v1"
);
return
configItems
;
}
...
...
pom.xml
View file @
7bc734b7
...
...
@@ -164,7 +164,7 @@
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-war-plugin
</artifactId>
<version>
2.
3
</version>
<version>
2.
6
</version>
<configuration>
<failOnMissingWebXml>
false
</failOnMissingWebXml>
</configuration>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment