学习nacos配置中心
This commit is contained in:
parent
fc6d30766d
commit
b0a42b8603
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
|
|
@ -17,4 +17,12 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- 添加nacos配置中心依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
<!-- <version>${latest.version}</version>-->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -2,6 +2,9 @@ package org.example;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
|
|
@ -10,8 +13,23 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
@SpringBootApplication
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
SpringApplication.run(App.class,args);
|
||||
public static void main( String[] args ) throws InterruptedException {
|
||||
ConfigurableApplicationContext applicationContent = SpringApplication.run(App.class, args);
|
||||
while (true){
|
||||
String name = applicationContent.getEnvironment().getProperty("name");
|
||||
String password = applicationContent.getEnvironment().getProperty("password");
|
||||
String text = applicationContent.getEnvironment().getProperty("text");
|
||||
String common = applicationContent.getEnvironment().getProperty("common");
|
||||
System.out.println("------name--------:"+name);
|
||||
System.out.println("------password--------:"+password);
|
||||
System.out.println("------common--------:"+common);
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
}
|
||||
|
||||
|
||||
// nacos客户端 每10ms根据MD5去注册中心进行判断,如果发生变化会重新拉取
|
||||
// 但出现客户端一致拉取的情况,原因可能是 1、客户端的nacos与服务端的nacos版本不一致,2、设置了命名空间
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
package org.example.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/config")
|
||||
@RefreshScope
|
||||
public class ConfigController {
|
||||
|
||||
@Value("${name}")
|
||||
private String name;
|
||||
|
||||
@RequestMapping("/get")
|
||||
public String get() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
server:
|
||||
port: 8081
|
||||
# cloud:
|
||||
# nacos:
|
||||
# discovery:
|
||||
# server-addr: 127.0.0.1:8848
|
||||
#management:
|
||||
# endpoints:
|
||||
# web:
|
||||
# exposure:
|
||||
# include: *
|
||||
|
|
@ -1,14 +1,3 @@
|
|||
server:
|
||||
port: 8081
|
||||
spring:
|
||||
application:
|
||||
name: nacos-producer
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
#management:
|
||||
# endpoints:
|
||||
# web:
|
||||
# exposure:
|
||||
# include: *
|
||||
profiles:
|
||||
active: prod
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
# bootstrap文件是spring cloud扩展的配置文件,关于nacos的配置信息可以放置在这里
|
||||
|
||||
spring:
|
||||
application:
|
||||
# 会自动根据服务名拉去 dataid 对应的配置文件(服务名与dataid一致)
|
||||
# 如果dataid与服务名不一致,则需要手动置顶,跟服务名一致的的dataid文件称为默认的配置文件
|
||||
# 默认的配置文件支持profiles配置,即:nacos-producer-dev,nacos-producer-prod
|
||||
# ${spring.application.name}-${profile}. ${file-extension:properties}
|
||||
name: nacos-producer
|
||||
cloud:
|
||||
nacos:
|
||||
# 如果服务端开启了权限管理,则必须填写用户信息和分组
|
||||
username: nacos
|
||||
password: nacos
|
||||
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848
|
||||
config:
|
||||
namespace: 64b4b4b4-c07d-4049-a3d5-34e08abcb093
|
||||
group: test
|
||||
server-addr: 127.0.0.1:8848
|
||||
file-extension: yaml # nacos配置中心的配置文件默认是properties格式的(只针对默认配置文件和profile),如果改为yaml文件需要添加声明,除了默认的配置文件都需要加上文件后缀
|
||||
#refresh-enable: false #禁止更新,一般不用
|
||||
shared-configs: #读取公共配置
|
||||
- data-id: producer-common.properties #下标[0]
|
||||
group: test # 默认是DEFAULT_GROUP
|
||||
refresh: true
|
||||
- data-id: producer-common2.properties #下标[1]
|
||||
group: test # 默认是DEFAULT_GROUP
|
||||
refresh: true
|
||||
extension-configs[0]:
|
||||
data-id: producer-common.properties
|
||||
group: test # 默认是DEFAULT_GROUP
|
||||
refresh: true
|
||||
|
||||
# 配置文件的优先级(优先级大的会覆盖优先级晓得,并且会形成互补)
|
||||
# profile > 默认配置文件 > extension-configs > shared-configs(下标越大优先级就越大)
|
||||
Loading…
Reference in New Issue