Skip to content

Commit c6d1cc5

Browse files
authored
add appName for config center (#51)
1 parent 41d1db6 commit c6d1cc5

File tree

11 files changed

+89
-27
lines changed

11 files changed

+89
-27
lines changed

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/ConfigWatch.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,12 @@ public class ConfigWatch implements ApplicationEventPublisherAware, SmartLifecyc
6060

6161
private RefreshRecord refreshRecord;
6262

63-
public ConfigWatch(ServiceCombConfigProperties serviceCombConfigProperties,
64-
ServiceCombConfigClient serviceCombConfigClient, ContextRefresher contextRefresher, RefreshRecord refreshRecord) {
65-
this.serviceCombConfigProperties = serviceCombConfigProperties;
66-
this.serviceCombConfigClient = serviceCombConfigClient;
63+
private String project;
64+
65+
public ConfigWatch() {
6766
ThreadPoolTaskScheduler threadPool = new ThreadPoolTaskScheduler();
6867
threadPool.initialize();
6968
taskScheduler = threadPool;
70-
this.contextRefresher = contextRefresher;
71-
this.refreshRecord = refreshRecord;
7269
}
7370

7471
@Override
@@ -89,8 +86,9 @@ private void watch() {
8986
Map<String, String> remoteConfig = null;
9087
if (ready.get()) {
9188
try {
92-
remoteConfig = serviceCombConfigClient.loadAll("price" + ConfigConstants.DEFAULT_SEPARATOR
93-
+ ConfigConstants.DEFAULT_PROJECT);
89+
remoteConfig = serviceCombConfigClient.loadAll(
90+
serviceCombConfigProperties.getServiceName() + ConfigConstants.DEFAULT_SEPARATOR
91+
+ serviceCombConfigProperties.getAppName(), project);
9492
} catch (RemoteOperationException e) {
9593
LOGGER.warn(e.getMessage());
9694
}
@@ -143,4 +141,31 @@ public void stop(Runnable callback) {
143141
public int getPhase() {
144142
return 0;
145143
}
144+
145+
146+
public void setServiceCombConfigProperties(
147+
ServiceCombConfigProperties serviceCombConfigProperties) {
148+
this.serviceCombConfigProperties = serviceCombConfigProperties;
149+
}
150+
151+
public void setServiceCombConfigClient(
152+
ServiceCombConfigClient serviceCombConfigClient) {
153+
this.serviceCombConfigClient = serviceCombConfigClient;
154+
}
155+
156+
public void setWatchScheduledFuture(ScheduledFuture<?> watchScheduledFuture) {
157+
this.watchScheduledFuture = watchScheduledFuture;
158+
}
159+
160+
public void setContextRefresher(ContextRefresher contextRefresher) {
161+
this.contextRefresher = contextRefresher;
162+
}
163+
164+
public void setRefreshRecord(RefreshRecord refreshRecord) {
165+
this.refreshRecord = refreshRecord;
166+
}
167+
168+
public void setProject(String project) {
169+
this.project = project;
170+
}
146171
}

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/ServiceCombConfigAutoConfiguration.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.cloud.common.transport.ServiceCombSSLProperties;
2526
import org.springframework.cloud.context.refresh.ContextRefresher;
2627
import org.springframework.cloud.huawei.config.client.RefreshRecord;
2728
import org.springframework.cloud.huawei.config.client.ServiceCombConfigClient;
@@ -63,8 +64,15 @@ public RefreshRecord refreshRecord() {
6364
matchIfMissing = true)
6465
public ConfigWatch configWatch(ServiceCombConfigProperties serviceCombConfigProperties,
6566
ServiceCombConfigClient serviceCombConfigClient,
66-
ContextRefresher contextRefresher, RefreshRecord refreshRecord) {
67-
return new ConfigWatch(serviceCombConfigProperties, serviceCombConfigClient, contextRefresher, refreshRecord);
67+
ContextRefresher contextRefresher, RefreshRecord refreshRecord,
68+
ServiceCombSSLProperties serviceCombSSLProperties) {
69+
ConfigWatch watch = new ConfigWatch();
70+
watch.setProject(serviceCombSSLProperties.getProject());
71+
watch.setContextRefresher(contextRefresher);
72+
watch.setServiceCombConfigClient(serviceCombConfigClient);
73+
watch.setServiceCombConfigProperties(serviceCombConfigProperties);
74+
watch.setRefreshRecord(refreshRecord);
75+
return watch;
6876
}
6977
}
7078

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/ServiceCombConfigBootstrapConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public ServiceCombConfigClient serviceCombConfigClient(ServiceCombConfigProperti
6565
@Bean
6666
public ServiceCombPropertySourceLocator serviceCombPropertySourceLocator(
6767
ServiceCombConfigProperties serviceCombConfigProperties,
68-
ServiceCombConfigClient serviceCombConfigClient) {
69-
return new ServiceCombPropertySourceLocator(serviceCombConfigProperties, serviceCombConfigClient);
68+
ServiceCombConfigClient serviceCombConfigClient, ServiceCombSSLProperties serviceCombSSLProperties) {
69+
return new ServiceCombPropertySourceLocator(serviceCombConfigProperties, serviceCombConfigClient,
70+
serviceCombSSLProperties.getProject());
7071
}
7172
}

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/ServiceCombConfigProperties.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.springframework.cloud.huawei.config;
1919

20+
import org.springframework.beans.factory.annotation.Value;
2021
import org.springframework.boot.context.properties.ConfigurationProperties;
2122
import org.springframework.stereotype.Component;
2223

@@ -30,6 +31,12 @@ public class ServiceCombConfigProperties {
3031

3132
private boolean enable = true;
3233

34+
@Value("${spring.cloud.servicecomb.discovery.serviceName:${spring.application.name}}")
35+
private String serviceName;
36+
37+
@Value("${spring.cloud.servicecomb.discovery.appName:default}")
38+
private String appName;
39+
3340
private String serverAddr;
3441

3542
private Watch watch = new Watch();
@@ -68,6 +75,22 @@ public void setWatch(Watch watch) {
6875
this.watch = watch;
6976
}
7077

78+
public String getServiceName() {
79+
return serviceName;
80+
}
81+
82+
public void setServiceName(String serviceName) {
83+
this.serviceName = serviceName;
84+
}
85+
86+
public String getAppName() {
87+
return appName;
88+
}
89+
90+
public void setAppName(String appName) {
91+
this.appName = appName;
92+
}
93+
7194
public static class Watch {
7295
private boolean enable;
7396

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/ServiceCombConfigPropertySource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ public ServiceCombConfigPropertySource(String name,
4545
}
4646

4747
@Retryable(interceptor = "serviceCombConfigRetryInterceptor")
48-
public Map<String, String> loadAllRemoteConfig(String serviceName) throws RemoteOperationException {
49-
Map<String, String> remoteConfig = serviceCombConfigClient.loadAll(serviceName + ConfigConstants.DEFAULT_SEPARATOR
50-
+ ConfigConstants.DEFAULT_PROJECT);
48+
public Map<String, String> loadAllRemoteConfig(String serviceName, String appName, String project)
49+
throws RemoteOperationException {
50+
Map<String, String> remoteConfig = serviceCombConfigClient
51+
.loadAll(serviceName + ConfigConstants.DEFAULT_SEPARATOR + appName, project);
5152
properties.putAll(remoteConfig);
5253
return remoteConfig;
5354
}

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/ServiceCombPropertySourceLocator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,28 @@ public class ServiceCombPropertySourceLocator implements PropertySourceLocator {
3737

3838
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceCombPropertySourceLocator.class);
3939

40+
private String project;
4041

4142
private ServiceCombConfigProperties serviceCombConfigProperties;
4243

4344
private ServiceCombConfigClient serviceCombConfigClient;
4445

4546
public ServiceCombPropertySourceLocator(ServiceCombConfigProperties serviceCombConfigProperties,
46-
ServiceCombConfigClient serviceCombConfigClient) {
47+
ServiceCombConfigClient serviceCombConfigClient, String project) {
4748
this.serviceCombConfigClient = serviceCombConfigClient;
4849
this.serviceCombConfigProperties = serviceCombConfigProperties;
50+
this.project = project;
4951
}
5052

5153
@Override
5254
public PropertySource<?> locate(Environment environment) {
5355
ServiceCombConfigPropertySource serviceCombConfigPropertySource = new ServiceCombConfigPropertySource(
5456
ConfigConstants.PROPERTYSOURCE_NAME,
5557
serviceCombConfigClient);
56-
String serviceName = environment.getProperty(ConfigConstants.SERVICE_NAME);
5758
try {
58-
serviceCombConfigPropertySource.loadAllRemoteConfig(serviceName);
59+
serviceCombConfigPropertySource
60+
.loadAllRemoteConfig(serviceCombConfigProperties.getServiceName(), serviceCombConfigProperties.getAppName(),
61+
project);
5962
} catch (RemoteOperationException e) {
6063
LOGGER.error(e.getMessage(), e);
6164
}

spring-cloud-huawei-config/src/main/java/org/springframework/cloud/huawei/config/client/ServiceCombConfigClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ public ServiceCombConfigClient(String url, HttpTransport httpTransport) {
5454
* @return
5555
* @throws RemoteOperationException
5656
*/
57-
public Map<String, String> loadAll(String dimensionsInfo) throws RemoteOperationException {
57+
public Map<String, String> loadAll(String dimensionsInfo, String project) throws RemoteOperationException {
5858
Response response = null;
5959
Map<String, String> result = new HashMap<>();
6060
try {
61+
project = project != null && !project.isEmpty() ? project : ConfigConstants.DEFAULT_PROJECT;
6162
response = httpTransport.sendGetRequest(
62-
url + "/" + ConfigConstants.DEFAULT_API_VERSION + "/" + ConfigConstants.DEFAULT_PROJECT
63-
+ "/configuration/items?dimensionsInfo=" + dimensionsInfo);
63+
url + "/" + ConfigConstants.DEFAULT_API_VERSION + "/" + project + "/configuration/items?dimensionsInfo="
64+
+ dimensionsInfo);
6465
if (response == null) {
6566
return result;
6667
}

spring-cloud-huawei-config/src/test/java/org/springframework/cloud/huawei/config/ConfigWatchTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public class ConfigWatchTest {
1313

1414
@Test
1515
public void isRunning() {
16-
ConfigWatch configWatch = new ConfigWatch(null, null, null, null);
16+
ConfigWatch configWatch = new ConfigWatch();
1717
assertEquals(configWatch.isRunning(), false);
1818
}
1919

2020
@Test
2121
public void isAutoStartup() {
22-
ConfigWatch configWatch = new ConfigWatch(null, null, null, null);
22+
ConfigWatch configWatch = new ConfigWatch();
2323
assertEquals(configWatch.isAutoStartup(), true);
2424
}
2525
}

spring-cloud-huawei-config/src/test/java/org/springframework/cloud/huawei/config/ServiceCombConfigPropertySourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public void loadAllRemoteConfig(@Injectable String name, @Injectable ServiceComb
3333
map.put("d", "r");
3434
new Expectations() {
3535
{
36-
source.loadAll(anyString);
36+
source.loadAll(anyString, anyString);
3737
result = map;
3838
}
3939
};
4040
ServiceCombConfigPropertySource serviceCombConfigPropertySource = new ServiceCombConfigPropertySource(name, source);
4141
Deencapsulation.setField(serviceCombConfigPropertySource, properties);
42-
Map<String, String> result = serviceCombConfigPropertySource.loadAllRemoteConfig("");
42+
Map<String, String> result = serviceCombConfigPropertySource.loadAllRemoteConfig("", "", "");
4343
Assert.assertEquals(result.size(), 2);
4444
}
4545

spring-cloud-huawei-config/src/test/java/org/springframework/cloud/huawei/config/ServiceCombPropertySourceLocatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void locate(
3535
Assert.assertEquals(watch.getDelay(), 10);
3636
serviceCombConfigProperties.setWatch(watch);
3737
ServiceCombPropertySourceLocator serviceCombPropertySourceLocator = new ServiceCombPropertySourceLocator(
38-
serviceCombConfigProperties, serviceCombConfigClient);
38+
serviceCombConfigProperties, serviceCombConfigClient, "default");
3939
PropertySource<?> result = serviceCombPropertySourceLocator.locate(environment);
4040
Assert.assertEquals(result.getName(), ConfigConstants.PROPERTYSOURCE_NAME);
4141
}

0 commit comments

Comments
 (0)