mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
以2.2.1.BUILD-SNAPSHOT 为基准,适配Greenwich版本的Spring Cloud
This commit is contained in:
@@ -1,11 +1,28 @@
|
||||
/*
|
||||
* Copyright 2013-2018 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.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import com.alibaba.cloud.nacos.NacosConfigProperties;
|
||||
import com.alibaba.cloud.nacos.NacosConfigManager;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -14,7 +31,9 @@ import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -24,9 +43,57 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UserConfig userConfig() {
|
||||
return new UserConfig();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ConfigurationProperties(prefix = "user")
|
||||
class UserConfig {
|
||||
|
||||
private int age;
|
||||
|
||||
private String name;
|
||||
|
||||
private Map<String, Object> map;
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Map<String, Object> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map<String, Object> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserConfig{" + "age=" + age + ", name='" + name + '\'' + ", map=" + map
|
||||
+ '}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
@@ -39,14 +106,14 @@ class SampleRunner implements ApplicationRunner {
|
||||
int userAge;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
System.out.println(
|
||||
String.format("Initial username=%s, userAge=%d", userName, userAge));
|
||||
|
||||
nacosConfigProperties.configServiceInstance().addListener(
|
||||
nacosConfigManager.getConfigService().addListener(
|
||||
"nacos-config-example.properties", "DEFAULT_GROUP", new Listener() {
|
||||
|
||||
/**
|
||||
@@ -55,9 +122,8 @@ class SampleRunner implements ApplicationRunner {
|
||||
* For example, config data in Nacos is:
|
||||
*
|
||||
* user.name=Nacos user.age=25
|
||||
*
|
||||
* @param configInfo latest config data for specific dataId in Nacos
|
||||
* server
|
||||
* server
|
||||
*/
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
@@ -77,20 +143,34 @@ class SampleRunner implements ApplicationRunner {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RestController
|
||||
@RefreshScope
|
||||
class SampleController {
|
||||
|
||||
@Autowired
|
||||
UserConfig userConfig;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
@Value("${user.name}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age:25}")
|
||||
int age;
|
||||
Integer age;
|
||||
|
||||
@RequestMapping("/user")
|
||||
public String simple() {
|
||||
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
|
||||
return "Hello Nacos Config!" + "Hello " + userName + " " + age + " [UserConfig]: "
|
||||
+ userConfig + "!" + nacosConfigManager.getConfigService();
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/bool")
|
||||
public boolean bool() {
|
||||
return (Boolean) (userConfig.getMap().get("2"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,2 +1,3 @@
|
||||
server.port=18084
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.endpoint.health.show-details=always
|
||||
|
@@ -1,8 +1,25 @@
|
||||
spring.application.name=nacos-config-example
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
||||
# nacos auth
|
||||
|
||||
#nacos certification information
|
||||
spring.cloud.nacos.username=nacos
|
||||
spring.cloud.nacos.password=nacos
|
||||
|
||||
spring.cloud.nacos.config.shared-data-ids=base-common.properties,common.properties
|
||||
spring.cloud.nacos.config.refreshable-dataids=common.properties
|
||||
#spring.cloud.nacos.config.refreshable-dataids=common.properties
|
||||
#spring.cloud.nacos.config.shared-data-ids=common.properties,base-common.properties
|
||||
spring.cloud.nacos.config.shared-configs[0]= common333.properties
|
||||
spring.cloud.nacos.config.shared-configs[1].data-id= common111.properties
|
||||
spring.cloud.nacos.config.shared-configs[1].group= GROUP_APP1
|
||||
spring.cloud.nacos.config.shared-configs[1].refresh= true
|
||||
spring.cloud.nacos.config.shared-configs[2]= common222.properties
|
||||
|
||||
#spring.cloud.nacos.config.ext-config[0]=ext.properties
|
||||
spring.cloud.nacos.config.extension-configs[0].data-id= extension1.properties
|
||||
spring.cloud.nacos.config.extension-configs[0].refresh= true
|
||||
spring.cloud.nacos.config.extension-configs[1]= extension2.properties
|
||||
spring.cloud.nacos.config.extension-configs[2].data-id= extension3.json
|
||||
|
||||
|
||||
|
||||
#spring.cloud.nacos.config.refresh-enabled=true
|
||||
|
||||
|
Reference in New Issue
Block a user