test: add examples for config refresh

pull/1516/head
evelynwei 6 months ago
parent 472e5d5655
commit 02852cd1fa

@ -0,0 +1,62 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.tencent.cloud.quickstart.callee;
import com.tencent.cloud.quickstart.callee.config.ConfigurationPropertiesSample;
import com.tencent.cloud.quickstart.callee.config.RefreshScopeSample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
public class ConfigController {
private static final Logger LOG = LoggerFactory.getLogger(ConfigController.class);
@Value("${spring.application.name:}")
private String applicationName;
@Value("${spring.cloud.client.ip-address:127.0.0.1}")
private String ip;
@Autowired
private ConfigurationPropertiesSample configurationPropertiesSample;
@Autowired
private RefreshScopeSample refreshScopeSample;
@RequestMapping(value = "/config/a", method = RequestMethod.GET)
public String a() {
String result = String.format("from application:%s, host-ip: %s, refreshScopeSample: %s",
applicationName, ip, refreshScopeSample.getName());
LOG.info(result);
return result;
}
@RequestMapping(value = "/config/b", method = RequestMethod.GET)
public String b() {
String result = String.format("from application:%s, host-ip: %s, configurationPropertiesSample: %s",
applicationName, ip, configurationPropertiesSample.getName());
LOG.info(result);
return result;
}
}

@ -0,0 +1,36 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.tencent.cloud.quickstart.callee.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
//@RefreshScope //如果使用反射模式,则不需要加这个注解
@ConfigurationProperties(prefix = "properties")
public class ConfigurationPropertiesSample {
private String name = "properties-default-name";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,45 @@
package com.tencent.cloud.quickstart.callee.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentChangeEventListener {
private static final Logger LOG = LoggerFactory.getLogger(EnvironmentChangeEventListener.class);
private final Environment environment;
public EnvironmentChangeEventListener(Environment environment) {
this.environment = environment;
}
@EventListener
public void handleConfigChange(EnvironmentChangeEvent event) {
if (event == null) {
LOG.warn("Received null environment change event");
return;
}
if (event.getKeys().isEmpty()) {
LOG.warn("Received empty keys in environment change event. Event details: {}", event);
LOG.info("Current environment properties, Active: {}, Default:{}", environment.getActiveProfiles(),
environment.getDefaultProfiles());
return;
}
StringBuilder changes = new StringBuilder();
changes.append("Environment configuration changes:\n");
event.getKeys().forEach(key -> {
String value = environment.getProperty(key);
changes.append(String.format(" %s = %s%n", key, value));
});
LOG.info(changes.toString());
}
}

@ -0,0 +1,20 @@
package com.tencent.cloud.quickstart.callee.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
@RefreshScope
@Configuration
public class RefreshScopeSample {
@Value("${refresh.name:refresh-default-name}")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Loading…
Cancel
Save