Add Nacos discovery integration tests.

pull/2421/head
Freeman Lau 3 years ago
parent b88690a1d6
commit 0397ec9036

@ -21,9 +21,14 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-test-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.alibaba.cloud.tests.nacos;
package com.alibaba.cloud.tests.nacos.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.alibaba.cloud.tests.nacos;
package com.alibaba.cloud.tests.nacos.config;
import java.util.List;
import java.util.Map;

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.alibaba.cloud.tests.nacos;
package com.alibaba.cloud.tests.nacos.config;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.testsupport.ContainerStarter;
@ -33,7 +33,7 @@ import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import static com.alibaba.cloud.tests.nacos.NacosConfigRefreshTest.PushConfigConfiguration;
import static com.alibaba.cloud.tests.nacos.config.NacosConfigRefreshTest.PushConfigConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@ -33,6 +33,14 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-test-support</artifactId>

@ -0,0 +1,99 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* 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
*
* https://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.alibaba.cloud.tests.nacos.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
*
* @author freeman
*/
@SpringBootApplication
@EnableFeignClients
public class NacosDiscoveryTestApp {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryTestApp.class, args);
}
@Bean
@LoadBalanced
@Profile("service-1")
RestTemplate restTemplate() {
return new RestTemplate();
}
@RestController
@Profile("service-2")
static class Controller {
int port;
@GetMapping
public Object get() {
return port;
}
@GetMapping("/{ok}")
public Object pass(@PathVariable Boolean ok) {
if (ok) {
return "ok";
}
throw new RuntimeException("not ok!");
}
@EventListener
public void onApplicationEvent(ServletWebServerInitializedEvent event) {
port = event.getWebServer().getPort();
}
}
@FeignClient(value = "service-2", fallback = Fallback.class)
interface Service2Client {
@GetMapping
Object get();
@GetMapping("/{ok}")
String pass(@PathVariable Boolean ok);
}
@Component
static class Fallback implements Service2Client {
@Override
public Object get() {
return "shouldn't use this !";
}
@Override
public String pass(Boolean ok) {
return "fallback";
}
}
}

@ -0,0 +1,176 @@
/*
* Copyright 2013-2022 the original author or authors.
*
* 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
*
* https://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.alibaba.cloud.tests.nacos.discovery;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import com.alibaba.cloud.testsupport.ContainerStarter;
import com.alibaba.cloud.testsupport.HasDockerAndItEnabled;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static com.alibaba.cloud.tests.nacos.discovery.NacosDiscoveryTestApp.Service2Client;
import static com.alibaba.cloud.testsupport.Tester.justDo;
import static com.alibaba.cloud.testsupport.Tester.testFunction;
import static java.util.function.Predicate.isEqual;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
*
*
* @author freeman
*/
@HasDockerAndItEnabled
public class NacosDiscoveryTest {
static GenericContainer nacos;
static {
nacos = ContainerStarter.startNacos();
// wait nacos started
justDo(() -> Thread.sleep(4000L));
}
static {
String serverAddr = "localhost:" + nacos.getMappedPort(8848);
System.setProperty("spring.cloud.nacos.discovery.server-addr", serverAddr);
}
static volatile ConfigurableApplicationContext service1 = new SpringApplicationBuilder(
NacosDiscoveryTestApp.class).profiles("service-1").run();
static volatile ConfigurableApplicationContext service2_0 = new SpringApplicationBuilder(
NacosDiscoveryTestApp.class).profiles("service-2").run();
static volatile ConfigurableApplicationContext service2_1 = new SpringApplicationBuilder(
NacosDiscoveryTestApp.class).profiles("service-2").run();
@AfterAll
static void stop() {
service1.stop();
service2_0.stop();
service2_1.stop();
}
@Test
public void testServiceDiscovery() {
testFunction("Service discovery", () -> {
DiscoveryClient discoveryClient = service1.getBean(DiscoveryClient.class);
List<ServiceInstance> instances = discoveryClient.getInstances("service-2");
assertThat(instances).hasSize(2);
});
}
@Test
public void testServiceDiscoveryWithRestTemplate() {
testFunction("Service discovery with RestTemplate", () -> {
RestTemplate restTemplate = service1.getBean(RestTemplate.class);
assertThatThrownBy(() -> service2_0.getBean(RestTemplate.class));
// default using RR
List<Object> list1 = new ArrayList<>();
List<Object> list2 = new ArrayList<>();
for (int i = 0; i < 20; i++) {
ResponseEntity<String> response = restTemplate
.getForEntity("http://service-2", String.class);
if (i % 2 != 0) {
list1.add(response.getBody());
}
else {
list2.add(response.getBody());
}
}
assertThat(list1).hasSize(10);
assertThat(list2).hasSize(10);
});
}
@Test
public void testServiceDiscoveryWithFeignClient() {
testFunction("Service discovery with FeignClient", () -> {
Service2Client service2Client = service1.getBean(Service2Client.class);
// default using RR
List<Object> list1 = new ArrayList<>();
List<Object> list2 = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Object result = service2Client.get();
if (i % 2 != 0) {
list1.add(result);
}
else {
list2.add(result);
}
}
assertThat(list1).hasSize(10);
assertThat(list2).hasSize(10);
});
}
@Test
public void testServiceDiscoveryWithFeignClientUsingSentinelCircuitBreaker() {
testFunction("Service discovery with FeignClient using Sentinel Circuit Breaker",
() -> {
Service2Client service2Client = service1
.getBean(Service2Client.class);
List<Object> passResult = new ArrayList<>();
List<Object> fallbackResult = new ArrayList<>();
for (int i = 0; i < 10; i++) {
passResult.add(service2Client.pass(true));
fallbackResult.add(service2Client.pass(false));
}
assertThat(passResult.stream().allMatch(isEqual("ok"))).isTrue();
assertThat(fallbackResult.stream().allMatch(isEqual("fallback")))
.isTrue();
});
}
@Test
public void testServiceDiscoveryActuatorEndpoint() {
testFunction("Service discovery actuator endpoint", () -> {
WebServerApplicationContext webServerApplicationContext = (WebServerApplicationContext) service1;
int port = webServerApplicationContext.getWebServer().getPort();
String response = new RestTemplate().getForEntity(
String.format("http://127.0.0.1:%d/actuator/nacosdiscovery", port),
String.class).getBody();
LinkedHashMap map = new ObjectMapper().readValue(response,
LinkedHashMap.class);
assertThat(map.containsKey("subscribe")).isTrue();
assertThat(map.containsKey("NacosDiscoveryProperties")).isTrue();
});
}
}

@ -0,0 +1,23 @@
server:
port: 0
tomcat:
threads:
min-spare: 2
max: 10
spring:
application:
name: service-1
cloud:
nacos:
discovery:
server-addr: localhost:8848
feign:
circuitbreaker:
enabled: true
management:
endpoints:
web:
exposure:
include: '*'

@ -0,0 +1,13 @@
server:
port: 0
tomcat:
threads:
min-spare: 4
max: 10
spring:
application:
name: service-2
cloud:
nacos:
discovery:
server-addr: localhost:8848

@ -38,4 +38,17 @@
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -23,10 +23,6 @@
<artifactId>junit-jupiter</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

@ -109,9 +109,4 @@ public class ContainerStarter {
}
}
public static void main(String[] args) throws IOException, InterruptedException {
loadHostIp2BrokerConf();
// startRocketmq("4.9.2").start();
}
}

Loading…
Cancel
Save