Add listening configs log info.

pull/2418/head
Freeman Lau 3 years ago
parent ed6a9949db
commit f45a9fb28f

@ -1,63 +0,0 @@
/*
* 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");
}
}

@ -21,29 +21,36 @@ import java.io.IOException;
import com.alibaba.cloud.imports.examples.model.UserConfig; import com.alibaba.cloud.imports.examples.model.UserConfig;
import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.testsupport.HasDockerAndItEnabled; import com.alibaba.cloud.testsupport.HasDockerAndItEnabled;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.GenericContainer;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort; import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import static com.alibaba.cloud.imports.examples.ConfigRefreshTest.PushConfigConfiguration;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/** /**
* <strong>NOTE:</strong> The official nacos image temporarily does not support the mac m1 * Nacos dynamic refresh config function test.
* chip (ARM architecture).
* *
* @author freeman * @author freeman
*/ */
@HasDockerAndItEnabled @HasDockerAndItEnabled
@SpringBootTest(webEnvironment = RANDOM_PORT) @SpringBootTest(webEnvironment = RANDOM_PORT)
public class AppTest { @Import(PushConfigConfiguration.class)
public class ConfigRefreshTest {
@LocalServerPort @LocalServerPort
private int port; private int port;
@ -58,7 +65,13 @@ public class AppTest {
private static final String serverAddr; private static final String serverAddr;
static { static {
nacos.start(); try {
nacos.start();
// make sure nacos server is ready !
Thread.sleep(2000L);
}
catch (InterruptedException ignored) {
}
serverAddr = "127.0.0.1:" + nacos.getMappedPort(8848); serverAddr = "127.0.0.1:" + nacos.getMappedPort(8848);
System.setProperty("spring.cloud.nacos.config.server-addr", serverAddr); System.setProperty("spring.cloud.nacos.config.server-addr", serverAddr);
@ -73,8 +86,6 @@ public class AppTest {
@Test @Test
public void testRefreshConfig() throws IOException, NacosException, InterruptedException { 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); ResponseEntity<String> response = restTemplate.getForEntity("http://127.0.0.1:" + port, String.class);
UserConfig userConfig = objectMapper.readValue(response.getBody(), UserConfig.class); UserConfig userConfig = objectMapper.readValue(response.getBody(), UserConfig.class);
@ -123,4 +134,36 @@ public class AppTest {
" age: 18", "yaml"); " age: 18", "yaml");
} }
static class PushConfigConfiguration {
@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");
}
}
} }

@ -102,6 +102,7 @@ public class NacosContextRefresher
} }
String dataId = propertySource.getDataId(); String dataId = propertySource.getDataId();
registerNacosListener(propertySource.getGroup(), dataId); registerNacosListener(propertySource.getGroup(), dataId);
log.info("listening config: dataId={}, group={}", dataId, propertySource.getGroup());
} }
} }
} }

Loading…
Cancel
Save