Skip to content

Commit

Permalink
[#1095] nacos support cross group invoking (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyouling authored Nov 17, 2023
1 parent 522f0c8 commit b23f926
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
~ Licensed 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.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.huaweicloud</groupId>
<artifactId>discovery-nacos-tests</artifactId>
<version>1.12.0-2022.0.x-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<name>Integration Tests::Discovery Nacos Tests::Order Consumer Test</name>
<artifactId>nacos-cross-group-consumer-test</artifactId>

<dependencies>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>discovery-tests-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>spring-cloud-starter-huawei-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>docker</id>
<properties>
<server.port>9001</server.port>
</properties>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.commonjava.maven.plugins</groupId>
<artifactId>directory-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.odavid.maven.plugins</groupId>
<artifactId>mixin-maven-plugin</artifactId>
<configuration>
<mixins>
<mixin>
<groupId>com.huaweicloud</groupId>
<artifactId>docker-build-config</artifactId>
<version>${project.version}</version>
</mixin>
</mixins>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* Licensed 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.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class CrossGroupApplication {

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

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* Licensed 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.sample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CrossGroupController {

private final RestTemplate restTemplate;

@Autowired
public CrossGroupController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@RequestMapping("/cross-group")
public String crossGroup(@RequestParam("id") String id) {
String callServiceResult = restTemplate.getForObject("http://price/price?id=" + id, String.class);
return callServiceResult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
## ---------------------------------------------------------------------------
##
## Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
##
## Licensed 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: 9001

spring:
application:
name: cross-group-consumer
cloud:
servicecomb:
cross-group:
enabled: true
service-group-mappings:
price: DEFAULT_GROUP
nacos:
discovery:
enabled: true
server-addr: http://127.0.0.1:8848
service: ${spring.application.name}
metadata:
x-test: value
x-test2: value2
group: CROSS_GROUP
config:
group: CROSS_GROUP
enabled: true
server-addr: http://127.0.0.1:8848
service: ${spring.application.name}

test:
value:
bootstrapOverride: "orderBootstrapOverride"
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,37 @@
</run>
</image>

<image>
<name>nacos-cross-group-consumer-test:${project.version}</name>
<alias>nacos-cross-group-consumer-test</alias>
<run>
<env>
<JAVA_OPTS>
-Dspring.cloud.nacos.discovery.server-addr=http://nacos-server:8848 -Dspring.cloud.nacos.config.server-addr=http://nacos-server:8848
</JAVA_OPTS>
<JAR_PATH>/maven/nacos-cross-group-consumer-test-${project.version}.jar</JAR_PATH>
</env>
<links>
<link>nacos-server:nacos-server</link>
</links>
<wait>
<log>Started PriceApplication</log>
<tcp>
<ports>
<port>9001</port>
</ports>
</tcp>
<time>120000</time>
</wait>
<ports>
<port>9001:9001</port>
</ports>
<dependsOn>
<container>nacos-server</container>
</dependsOn>
</run>
</image>

<image>
<name>nacos-discovery-gateway-test:${project.version}</name>
<alias>nacos-discovery-gateway-test</alias>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* Licensed 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.sample;


import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate;

public class CrossGroupProviderControllerIT {
final String url = "http://127.0.0.1:9001";

final RestTemplate template = new RestTemplate();

@Test
public void testCrossGroup() {
String result = template.getForObject(url + "/cross-group?id=hello", String.class);
assertThat(result).isEqualTo("hello");
}
}
1 change: 1 addition & 0 deletions integration-tests/discovery-nacos-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<module>gateway</module>
<module>price-provider</module>
<module>order-consumer</module>
<module>cross-group-consumer</module>
<module>account-provider</module>
<module>webflux-provider</module>
<module>discovery-tests-client</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2020-2022 Huawei Technologies Co., Ltd. All rights reserved.
* Licensed 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.nacos.discovery;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;

@Configuration
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureBefore(NacosDiscoveryAutoConfiguration.class)
@ConditionalOnProperty(value = "spring.cloud.servicecomb.cross-group.enabled", havingValue = "true")
public class NacosCrossGroupAutoConfiguration {
@Bean
public NacosServiceCrossGroupDiscovery nacosServiceCrossGroupDiscovery(NacosDiscoveryProperties discoveryProperties,
NacosServiceManager nacosServiceManager, NacosCrossGroupServiceConfig nacosCrossGroupServiceConfig) {
return new NacosServiceCrossGroupDiscovery(discoveryProperties, nacosServiceManager, nacosCrossGroupServiceConfig);
}

@Bean
public NacosCrossGroupServiceConfig nacosCrossGroupServiceConfig() {
return new NacosCrossGroupServiceConfig();
}
}
Loading

0 comments on commit b23f926

Please sign in to comment.