Upgrade nacos to 1.4.3.

pull/2403/head
Freeman Lau 3 years ago
parent fe3c1a822d
commit 9d09a6df64

@ -93,15 +93,16 @@
<rocketmq.version>4.6.1</rocketmq.version>
<!-- Maven Plugin Versions -->
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-source-plugin.version>2.2.1</maven-source-plugin.version>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<flatten-maven-plugin.version>1.1.0</flatten-maven-plugin.version>
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
<jacoco.version>0.8.3</jacoco.version>
<jacoco.version>0.8.5</jacoco.version>
</properties>
<modules>

@ -21,7 +21,7 @@
<revision>2022.0-SNAPSHOT</revision>
<sentinel.version>1.8.1</sentinel.version>
<seata.version>1.3.0</seata.version>
<nacos.client.version>2.0.3</nacos.client.version>
<nacos.client.version>1.4.3</nacos.client.version>
<nacos.config.version>0.8.0</nacos.config.version>
<spring.context.support.version>1.0.11</spring.context.support.version>

@ -25,6 +25,12 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- Testing -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-test-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

@ -13,6 +13,3 @@ spring:
- optional:nacos:test.yml
- optional:nacos:test01.yml?group=group_02
- optional:nacos:test02.yml?group=group_03&refreshEnabled=false
logging:
level:
com.alibaba.nacos: debug

@ -0,0 +1,141 @@
/*
* 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.imports.examples;
import java.io.IOException;
import com.alibaba.cloud.imports.examples.model.UserConfig;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.testsupport.HasDockerAndItEnabled;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.condition.OS.MAC;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* The nacos image temporarily does not support the mac m1 chip (ARM).
* <p>
* If you test locally, you can use the image
* <a href="https://hub.docker.com/r/zhusaidong/nacos-server-m1">zhusaidong/nacos-server-m1:2.0.3</a>
*
* @author freeman
*/
@HasDockerAndItEnabled
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class AppTest {
@LocalServerPort
private int port;
@Container
private static final GenericContainer nacos;
private static final String serverAddr;
static {
String image = getCurrentOs() == MAC
? "zhusaidong/nacos-server-m1:2.0.3"
: "nacos/nacos-server:1.4.2";
nacos = new GenericContainer(image).withExposedPorts(8848)
.withEnv("MODE", "standalone")
.withEnv("JVM_XMS", "256m")
.withEnv("JVM_XMX", "256m")
.withEnv("JVM_XMN", "128m");
nacos.start();
serverAddr = "127.0.0.1:" + nacos.getMappedPort(8848);
System.setProperty("spring.cloud.nacos.config.server-addr", serverAddr);
}
private static OS getCurrentOs() {
return ReflectionTestUtils.invokeMethod(OS.class, "determineCurrentOs");
}
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private NacosConfigManager nacosConfigManager;
private final ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testRefreshConfig() throws IOException, NacosException, InterruptedException {
// make sure nacos server is ready !
Thread.sleep(1500L);
ResponseEntity<String> response = restTemplate.getForEntity("http://127.0.0.1:" + port, String.class);
UserConfig userConfig = objectMapper.readValue(response.getBody(), UserConfig.class);
System.out.println("====== before refresh ======");
System.out.println(objectMapper.writeValueAsString(userConfig));
assertThat(userConfig.getAge()).isEqualTo(21);
assertThat(userConfig.getName()).isEqualTo("freeman");
assertThat(userConfig.getMap().size()).isEqualTo(2);
assertThat(userConfig.getUsers().size()).isEqualTo(2);
// update config
updateConfig();
// wait config refresh
Thread.sleep(2000L);
response = restTemplate.getForEntity("http://127.0.0.1:" + port, String.class);
userConfig = objectMapper.readValue(response.getBody(), UserConfig.class);
System.out.println("====== after refresh ======");
System.out.println(objectMapper.writeValueAsString(userConfig));
assertThat(userConfig.getAge()).isEqualTo(22);
assertThat(userConfig.getName()).isEqualTo("freeman1123");
assertThat(userConfig.getMap().size()).isEqualTo(3);
assertThat(userConfig.getUsers().size()).isEqualTo(2);
}
private void updateConfig() throws NacosException {
nacosConfigManager.getConfigService().publishConfig("test.yml", "DEFAULT_GROUP",
"configdata:\n" +
" user:\n" +
" age: 22\n" +
" name: freeman1123\n" +
" map:\n" +
" hobbies:\n" +
" - art\n" +
" - programming\n" +
" - movie\n" +
" intro: Hello, I'm freeman\n" +
" extra: yo~\n" +
" users:\n" +
" - name: dad\n" +
" age: 20\n" +
" - name: mom\n" +
" age: 18", "yaml");
}
}

@ -0,0 +1,63 @@
/*
* 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.imports.examples;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
/**
* @author freeman
*/
@Configuration
public class Config {
@Autowired
private NacosConfigManager nacosConfigManager;
@EventListener(ApplicationReadyEvent.class)
@Order(Ordered.HIGHEST_PRECEDENCE)
public void applicationReadyEventApplicationListener() throws NacosException {
pushConfig2Nacos(nacosConfigManager.getConfigService());
}
private static void pushConfig2Nacos(ConfigService configService)
throws NacosException {
configService.publishConfig("test.yml", "DEFAULT_GROUP",
"configdata:\n" +
" user:\n" +
" age: 21\n" +
" name: freeman\n" +
" map:\n" +
" hobbies:\n" +
" - art\n" +
" - programming\n" +
" intro: Hello, I'm freeman\n" +
" users:\n" +
" - name: dad\n" +
" age: 20\n" +
" - name: mom\n" +
" age: 18", "yaml");
}
}

@ -25,6 +25,10 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

@ -3,6 +3,7 @@
## 项目说明
本项目演示如何使用 Nacos Config Starter 完成 Spring Cloud 应用的配置管理。
注意: 适用于 spring boot 版本低于 2.4.0
[Nacos](https://github.com/alibaba/Nacos) 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

@ -3,6 +3,7 @@
## Project Instruction
This example illustrates how to use Nacos Config Starter implement externalized configuration for Spring Cloud applications.
Note: Applicable to spring boot version lower than 2.4.0
[Nacos](https://github.com/alibaba/Nacos) an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.

@ -16,6 +16,10 @@
<name>Spring Cloud Alibaba Examples</name>
<description>Example showing how to use features of Spring Cloud Alibaba</description>
<properties>
<testcontainer.version>1.16.3</testcontainer.version>
</properties>
<modules>
<module>sentinel-example/sentinel-core-example</module>
<module>sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example</module>
@ -40,8 +44,26 @@
<module>spring-cloud-alibaba-dubbo-examples</module>
<module>spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example</module>
<module>spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-consul-example</module>
<module>spring-cloud-alibaba-test-support</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-test-support</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainer.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-alibaba-test-support</artifactId>
<name>Spring Cloud Alibaba Test Support</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,50 @@
/*
* Copyright 2012-2020 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.testsupport;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* Disables test execution if Docker is unavailable.
* <p>
* We don't want to run integration tests on local machine, but give a change to run it.
* <p>
* Set system property
* {@link HasDockerAndItEnabledCondition#RUN_INTEGRATION_TESTS_PROPERTY} to 'true'
* <p>
* general usage: {@code mvn -Dit.enabled=true test}
* <p>
* <strong>NOTE:</strong> it means integration test
*
* @author Andy Wilkinson
* @author Phillip Webb
* @author freeman
* @since 2022
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ExtendWith(HasDockerAndItEnabledCondition.class)
public @interface HasDockerAndItEnabled {
}

@ -0,0 +1,64 @@
/*
* Copyright 2012-2020 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.testsupport;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.DockerClientFactory;
/**
* An {@link ExecutionCondition} that disables execution if Docker is unavailable.
*
* @author Andy Wilkinson
* @author Phillip Webb
* @author freeman
* @since 2022
*/
class HasDockerAndItEnabledCondition implements ExecutionCondition {
private static final String RUN_INTEGRATION_TESTS_PROPERTY = "it.enabled";
private static final ConditionEvaluationResult ENABLED = ConditionEvaluationResult
.enabled("Docker available.");
private static final ConditionEvaluationResult DISABLED = ConditionEvaluationResult
.disabled("Default not run integration tests, you can set '"
+ RUN_INTEGRATION_TESTS_PROPERTY + "=true' to enable.");
private static final ConditionEvaluationResult DOCKER_DISABLED = ConditionEvaluationResult
.disabled("Docker unavailable.");
@Override
public ConditionEvaluationResult evaluateExecutionCondition(
ExtensionContext context) {
try {
if (Boolean
.parseBoolean(System.getProperty(RUN_INTEGRATION_TESTS_PROPERTY))) {
DockerClientFactory.instance().client();
return ENABLED;
}
else {
return DISABLED;
}
}
catch (Throwable ignored) {
return DOCKER_DISABLED;
}
}
}

@ -166,7 +166,7 @@ public class NacosConfigDataLocationResolverTest {
void testSetCommonPropertiesIsOK() {
environment.setProperty("spring.cloud.nacos.username", "root");
environment.setProperty("spring.cloud.nacos.password", "root");
environment.setProperty("spring.cloud.nacos.server-addr", "localhost:8888");
environment.setProperty("spring.cloud.nacos.server-addr", "127.0.0.1:8888");
String locationUri = "nacos:test.yml";
List<NacosConfigDataResource> resources = testUri(locationUri);
@ -174,7 +174,7 @@ public class NacosConfigDataLocationResolverTest {
NacosConfigDataResource resource = resources.get(0);
assertThat(resource.getProperties().getUsername()).isEqualTo("root");
assertThat(resource.getProperties().getPassword()).isEqualTo("root");
assertThat(resource.getProperties().getServerAddr()).isEqualTo("localhost:8888");
assertThat(resource.getProperties().getServerAddr()).isEqualTo("127.0.0.1:8888");
}
@Test
@ -182,9 +182,9 @@ public class NacosConfigDataLocationResolverTest {
environment.setProperty("spring.cloud.nacos.username", "root");
environment.setProperty("spring.cloud.nacos.password", "root");
environment.setProperty("spring.cloud.nacos.config.password", "not_root");
environment.setProperty("spring.cloud.nacos.server-addr", "localhost:8888");
environment.setProperty("spring.cloud.nacos.server-addr", "127.0.0.1:8888");
environment.setProperty("spring.cloud.nacos.config.server-addr",
"localhost:9999");
"127.0.0.1:9999");
String locationUri = "nacos:test.yml";
List<NacosConfigDataResource> resources = testUri(locationUri);
@ -192,7 +192,7 @@ public class NacosConfigDataLocationResolverTest {
NacosConfigDataResource resource = resources.get(0);
assertThat(resource.getProperties().getUsername()).isEqualTo("root");
assertThat(resource.getProperties().getPassword()).isEqualTo("not_root");
assertThat(resource.getProperties().getServerAddr()).isEqualTo("localhost:9999");
assertThat(resource.getProperties().getServerAddr()).isEqualTo("127.0.0.1:9999");
}
private List<NacosConfigDataResource> testUri(String locationUri,

@ -17,6 +17,7 @@
package com.alibaba.cloud.nacos.discovery;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -32,6 +33,7 @@ public class NacosDiscoveryAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class));
@Test

@ -16,6 +16,7 @@
package com.alibaba.cloud.nacos.discovery;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -35,6 +36,7 @@ public class NacosDiscoveryClientConfigurationTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class,
NacosDiscoveryClientConfiguration.class, this.getClass()));

@ -16,6 +16,7 @@
package com.alibaba.cloud.nacos.discovery.reactive;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import org.junit.jupiter.api.Test;
@ -34,6 +35,7 @@ public class NacosReactiveDiscoveryClientConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(UtilAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class,
NacosServiceAutoConfiguration.class,
NacosReactiveDiscoveryClientConfiguration.class));
@Test

Loading…
Cancel
Save