2、在‘配置管理’-》‘配置列表’下选中tenantIdA命名空间,在下边创建配置:
填写Data Id:appA_A、Group:APPA_GROUP、选中文件格式:yaml,填写配置内容:
name: xx
age: 23
保存并发布。
3、springcloud项目中使用
3.1、引入pom配置
<properties>
<java.version>8</java.version>
<!-- spring platform -->
<spring.boot.version>2.3.4.RELEASE</spring.boot.version>
<spring.cloud.version>Hoxton.SR3</spring.cloud.version>
<spring.data.version>Moore-SR5</spring.data.version>
<alibaba.version>2.2.0.RELEASE</alibaba.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringFramework把需要升级版本的组件放到前面 -->
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring data -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>${spring.data.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- spring alibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.2、在boostrap.yml中添加配置
spring:
cloud:
nacos:
config:
enabled: true
server-addr: 127.0.0.1:8848
file-extension: yaml
namespace: tenantA
group: APPA_GROUP
prefix: appA_A
data-id: appA_A
refresh.enabled: true
encode: UTF-8
timeout: 10000
context-path: /nacos
username: nacos
password: nacos
注意:
nacos配置中心配置内容必须写在bootstrap.yml文件中,如果写入application-*.yml或者application.yml中均不生效。
这个和springboot启动时SpringApplicationContext父子容器有关系。
3.3、项目中使用配置
定义参数实体类NacosTestConfig.java
@Configuration(proxyBeanMethods = false)
public class NacosTestConfig {
@Value("${name}")
private String name;
@Value("${age}")
private int age;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public int getAge() {
return age;
public void setAge(int age) {
this.age = age;
4、启用服务器发现,并验证是否配置生效
@SpringBootApplication(scanBasePackages = {"com.xx.xx"}, proxyBeanMethods = false)
@EnableDiscoveryClient
public class Main {
* 主程序入口(jar格式)
* @param args 命令行参数
* @throws Exception 执行异常
public static void main(String[] args) throws Exception {
new SpringApplication().run(Main.class, args);
String userName = applicationContext.getEnvironment().getProperty("name");
String userAge = applicationContext.getEnvironment().getProperty("age");
System.out.println("my name:" + userName + "; age:" + userAge);
final NacosTestConfig nacosTestConfig = applicationContext.getBean(NacosTestConfig.class);
System.out.println("my name:" + nacosTestConfig.getName() + "; age:" + nacosTestConfig.getAge());;
启动输出:
2021-04-01 22:02:32.106 INFO 21759 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)
2021-04-01 22:02:33.507 INFO 21759 --- [ restartedMain] c.a.n.c.c.impl.LocalConfigInfoProcessor : LOCAL_SNAPSHOT_PATH:/Users/dz/nacos/config
2021-04-01 22:02:33.519 INFO 21759 --- [ restartedMain] c.a.nacos.client.config.utils.JvmUtil : isMultiInstance:false
2021-04-01 22:02:33.534 WARN 21759 --- [ restartedMain] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[appA_A.yaml] & group[APPA_GROUP]
2021-04-01 22:02:33.539 WARN 21759 --- [ restartedMain] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[appA_A-uat.yaml] & group[APPA_GROUP]
2021-04-01 22:02:33.540 INFO 21759 --- [ restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-appA_A-uat.yaml,APPA_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-appA_A.yaml,APPA_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-appA_A,APPA_GROUP'}]
2021-04-01 22:02:33.543 INFO 21759 --- [ restartedMain] com.dx.App : The following profiles are active: uat
2021-04-01 22:02:33.976 WARN 21759 --- [ restartedMain] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2021-04-01 22:02:34.029 INFO 21759 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=b6d01e0a-0ee2-36c7-9603-d720f3ac0aa5
2021-04-01 22:02:34.306 INFO 21759 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 12003 (http)
2021-04-01 22:02:34.311 INFO 21759 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-04-01 22:02:34.312 INFO 21759 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2021-04-01 22:02:34.370 INFO 21759 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-04-01 22:02:34.371 INFO 21759 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 817 ms
2021-04-01 22:02:34.462 WARN 21759 --- [ restartedMain] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID 'nacos-config' contains invalid characters, please migrate to a valid format.
2021-04-01 22:02:34.463 WARN 21759 --- [ restartedMain] o.s.boot.actuate.endpoint.EndpointId : Endpoint ID 'nacos-discovery' contains invalid characters, please migrate to a valid format.
2021-04-01 22:02:34.513 WARN 21759 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2021-04-01 22:02:34.513 INFO 21759 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2021-04-01 22:02:34.516 WARN 21759 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2021-04-01 22:02:34.516 INFO 21759 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2021-04-01 22:02:34.660 INFO 21759 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-01 22:02:35.744 INFO 21759 --- [ restartedMain] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2021-04-01 22:02:35.750 INFO 21759 --- [ restartedMain] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'Nacso-Watch-Task-Scheduler'
2021-04-01 22:02:35.755 INFO 21759 --- [ restartedMain] com.alibaba.nacos.client.naming : initializer namespace from System Property :null
2021-04-01 22:02:35.755 INFO 21759 --- [ restartedMain] com.alibaba.nacos.client.naming : initializer namespace from System Environment :null
2021-04-01 22:02:35.755 INFO 21759 --- [ restartedMain] com.alibaba.nacos.client.naming : initializer namespace from System Property :null
2021-04-01 22:02:35.923 INFO 21759 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-04-01 22:02:36.929 INFO 21759 --- [ restartedMain] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2021-04-01 22:02:36.953 INFO 21759 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-04-01 22:02:37.025 INFO 21759 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 12003 (http) with context path ''
2021-04-01 22:02:37.027 INFO 21759 --- [ restartedMain] com.alibaba.nacos.client.naming : [BEAT] adding beat: BeatInfo{port=12003, ip='192.168.0.100', weight=1.0, serviceName='APPA_GROUP@@nacos-payment-provider', cluster='DEFAULT', metadata={preserved.register.source=SPRING_CLOUD}, scheduled=false, period=5000, stopped=false} to beat map.
2021-04-01 22:02:37.028 INFO 21759 --- [ restartedMain] com.alibaba.nacos.client.naming : [REGISTER-SERVICE] tenantA registering service APPA_GROUP@@nacos-payment-provider with instance: Instance{instanceId='null', ip='192.168.0.100', port=12003, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName='DEFAULT', serviceName='null', metadata={preserved.register.source=SPRING_CLOUD}}
2021-04-01 22:02:37.034 INFO 21759 --- [ restartedMain] c.a.c.n.registry.NacosServiceRegistry : nacos registry, APPA_GROUP nacos-payment-provider 192.168.0.100:12003 register finished
2021-04-01 22:02:38.038 INFO 21759 --- [ restartedMain] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2021-04-01 22:02:38.053 INFO 21759 --- [ restartedMain] com.dx.App : Started App in 7.261 seconds (JVM running for 14.713)
2021-04-01 22:02:38.059 INFO 21759 --- [ restartedMain] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-tenantA] [subscribe] appA_A+APPA_GROUP+tenantA
2021-04-01 22:02:38.060 INFO 21759 --- [ restartedMain] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848-tenantA] [add-listener] ok, tenant=tenantA, dataId=appA_A, group=APPA_GROUP, cnt=1
2021-04-01 22:02:38.060 INFO 21759 --- [ restartedMain] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-tenantA] [subscribe] appA_A.yaml+APPA_GROUP+tenantA
2021-04-01 22:02:38.060 INFO 21759 --- [ restartedMain] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848-tenantA] [add-listener] ok, tenant=tenantA, dataId=appA_A.yaml, group=APPA_GROUP, cnt=1
2021-04-01 22:02:38.060 INFO 21759 --- [ restartedMain] c.a.n.client.config.impl.ClientWorker : [fixed-127.0.0.1_8848-tenantA] [subscribe] appA_A-uat.yaml+APPA_GROUP+tenantA
2021-04-01 22:02:38.060 INFO 21759 --- [ restartedMain] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848-tenantA] [add-listener] ok, tenant=tenantA, dataId=appA_A-uat.yaml, group=APPA_GROUP, cnt=1
my name:kdk; age:3322
my name:kdk; age:3322
2021-04-01 22:02:38.391 INFO 21759 --- [)-192.168.0.100] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-01 22:02:38.391 INFO 21759 --- [)-192.168.0.100] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-04-01 22:02:38.396 INFO 21759 --- [)-192.168.0.100] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
【+加关注】。