Skip to content

Commit

Permalink
[huaweicloud#48][huaweicloud#47][huaweicloud#50]解决服务配置和spring value 默…
Browse files Browse the repository at this point in the history
…认值不生效的问题和初始化顺序问题
  • Loading branch information
liubao68 committed Oct 29, 2020
1 parent 5341e7d commit 6b9e785
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ConfigCenterClient(AddressManager addressManager, HttpTransport httpTrans

@Override
public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request) {
String dimensionsInfo = buildDimensionsInfo(request);
String dimensionsInfo = buildDimensionsInfo(request, true);
QueryConfigurationsResponse queryConfigurationsResponse = new QueryConfigurationsResponse();

Map<String, Object> configurations = new HashMap<>();
Expand All @@ -79,7 +79,7 @@ public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsReques
configurations.putAll(allConfigMap.get(APPLICATION_CONFIG));
}

if (allConfigMap.get(request.getServiceName()) != null) {
if (allConfigMap.get(buildDimensionsInfo(request, false)) != null) {
configurations.putAll(allConfigMap.get(request.getServiceName()));
}

Expand Down Expand Up @@ -109,11 +109,11 @@ public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsReques
}
}

private String buildDimensionsInfo(QueryConfigurationsRequest request) {
private String buildDimensionsInfo(QueryConfigurationsRequest request, boolean withVersion) {
String result =
request.getServiceName() + DEFAULT_APP_SEPARATOR
+ request.getApplication();
if (!StringUtils.isEmpty(request.getVersion())) {
if (withVersion && !StringUtils.isEmpty(request.getVersion())) {
result = result + DEFAULT_SERVICE_SEPARATOR + request
.getVersion();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.servicecomb.config.center.client.AddressManager;
import org.apache.servicecomb.config.center.client.ConfigCenterClient;
Expand Down Expand Up @@ -109,6 +110,22 @@ public void onConfigurationChangedEvent(ConfigurationChangedEvent event) {
notifyGovernanceDataChange(event.getConfigurations());
}

@Override
protected Properties mergeProperties() throws IOException {
Properties properties = super.mergeProperties();
properties.putAll(this.sources);
return properties;
}

@Override
protected String resolvePlaceholder(String placeholder, Properties props) {
String propertyValue = super.resolvePlaceholder(placeholder, props);
if (propertyValue == null) {
return this.sources.get(placeholder) == null ? null : this.sources.get(placeholder).toString();
}
return propertyValue;
}

@Subscribe
public void onRegistrationReadyEvent(RegistrationReadyEvent event) {
// 注册完成发送一次配置变更, 保证订阅者能够读取到配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.registry.NotifyListener;
Expand Down Expand Up @@ -164,7 +164,7 @@ public void shutdown() {

@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if (applicationEvent instanceof ContextRefreshedEvent) {
if (applicationEvent instanceof ContextStartedEvent) {
try {
AddressManager addressManager = ServiceCenterConfiguration.createAddressManager();
SSLProperties sslProperties = CommonConfiguration.createSSLProperties();
Expand Down
13 changes: 13 additions & 0 deletions integration-tests/discovery-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
运行测试的步骤:

1. 安装本地微服务引擎并启动
2. 运行 PriceApplication
3. 运行 OrderApplication
4. 配置中心下发配置项:

[全局配置] dubbo.servicecomb.test.configuration: peizhi
[服务配置:price-provider] dubbo.servicecomb.test.configurationService: peizhi_service
[全局配置] dubbo.servicecomb.governance: {"providerInfos":[{"serviceName":"price-provider","schemaInfos":[{"schemaId":"com.huaweicloud.it.price.PriceService","parameters":{"timeout":5000}}]}]}

5. 运行 PortalApplication 查看测试结果, 如果成功,输出 `running all test cases successfully`

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public interface PriceService {
String sayHello(String name);

CompletableFuture<String> sayHelloAsync(String name);

String testConfiguration(String value);

String testConfigurationService(String value);
}
83 changes: 82 additions & 1 deletion integration-tests/discovery-tests/discovery-tests-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,93 @@
<version>1.1.2-SNAPSHOT</version>
</parent>

<properties>
<spring-boot.version>2.1.6.RELEASE</spring-boot.version>
<spring.version>5.1.14.RELEASE</spring.version>
<!-- TODO: CI 环境使用 1.3.2, 但是 spring cloud huawei 有一个 bug ,暂时没修复, 运行测试用例的时候,需要编译最新版本 -->
<!-- <spring-cloud-huawei.version>1.3.2</spring-cloud-huawei.version>-->
<spring-cloud-huawei.version>1.3.3-Hoxton-SNAPSHOT</spring-cloud-huawei.version>
</properties>

<modelVersion>4.0.0</modelVersion>

<artifactId>discovery-tests-client</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-huawei-dependencies</artifactId>
<version>${spring-cloud-huawei.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>dubbo-servicecomb-dependencies</artifactId>
<version>${project.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.24</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>com.huaweicloud.dubbo-servicecomb</groupId>
<artifactId>discovery-common-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-servicecomb-discovery</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.huaweicloud.it.portal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@Component
public class PortalApplication {
private static PortalController portalController;

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}

@Autowired
void setPortalController(PortalController portalController) {
PortalApplication.portalController = portalController;
}


public static void main(String[] args) throws Exception {
try {
SpringApplication.run(PortalApplication.class);
} catch (Throwable e) {
e.printStackTrace();
}

System.out.println("running all test cases");
portalController.testAll();
System.out.println("running all test cases successfully");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.huaweicloud.it.portal;

import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping(path = "/portal")
public class PortalController {
@Autowired
private RestTemplate restTemplate;

@GetMapping(path = "testAll")
public void testAll() {

String result = restTemplate.getForObject("http://order-consumer/price/sayHello?name={name}", String.class, "hello world");
Assert.assertEquals(result, "hello world");

result = restTemplate.getForObject("http://order-consumer/price/sayHello?name={name}", String.class, "timeout");
Assert.assertEquals(result, "timeout");

result = restTemplate.getForObject("http://order-consumer/price/testConfiguration", String.class);
Assert.assertEquals(result, "peizhi");

result = restTemplate.getForObject("http://order-consumer/price/testConfigurationService", String.class);
Assert.assertEquals(result, "peizhi_service");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You 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.
## ---------------------------------------------------------------------------

server:
port: 8088

spring:
application:
name: portal-consumer
cloud:
servicecomb:
discovery:
enabled: true
address: http://127.0.0.1:30100
appName: discovery
serviceName: portal-consumer
version: 0.0.1
4 changes: 4 additions & 0 deletions integration-tests/discovery-tests/order-consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-rpc-rest</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-remoting-netty4</artifactId>
Expand Down
Loading

0 comments on commit 6b9e785

Please sign in to comment.