Skip to content

Commit

Permalink
nacos 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jan 21, 2020
1 parent 2b5f10a commit 5f5e8a2
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lab-44/lab-44-nacos-config-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-44-nacos-config-demo</artifactId>

<dependencies>
<!-- Spring Boot Starter 基础依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- 实现对 Nacos 作为配置中心的自动化配置 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.4</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cn.iocoder.springboot.lab44.nacosdemo;

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.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
// @NacosPropertySource(dataId = "example", type = ConfigType.YAML)
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Component
public class OrderPropertiesCommandLineRunner implements CommandLineRunner {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private OrderProperties orderProperties;

@Override
public void run(String... args) {
logger.info("payTimeoutSeconds:" + orderProperties.getPayTimeoutSeconds());
logger.info("createFrequencySeconds:" + orderProperties.getCreateFrequencySeconds());
}

}

@Component
public class ValueCommandLineRunner implements CommandLineRunner {

private final Logger logger = LoggerFactory.getLogger(getClass());

// @NacosValue(value = "${order.pay-timeout-seconds}")
@Value(value = "${order.pay-timeout-seconds}")
private Integer payTimeoutSeconds;

// @NacosValue(value = "${order.pay-timeout-seconds}")
@Value(value = "${order.pay-timeout-seconds}")
private Integer createFrequencySeconds;

@Override
public void run(String... args) {
logger.info("payTimeoutSeconds:" + payTimeoutSeconds);
logger.info("createFrequencySeconds:" + createFrequencySeconds);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cn.iocoder.springboot.lab44.nacosdemo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
//@NacosConfigurationProperties(prefix = "order", dataId = "${nacos.config.data-id}", type = ConfigType.YAML)
@ConfigurationProperties(prefix = "order")
public class OrderProperties {

/**
* 订单支付超时时长,单位:秒。
*/
private Integer payTimeoutSeconds;

/**
* 订单创建频率,单位:秒
*/
private Integer createFrequencySeconds;

// /**
// * 配置描述
// */
// private String desc;

public Integer getPayTimeoutSeconds() {
return payTimeoutSeconds;
}

public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
this.payTimeoutSeconds = payTimeoutSeconds;
return this;
}

public Integer getCreateFrequencySeconds() {
return createFrequencySeconds;
}

public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
this.createFrequencySeconds = createFrequencySeconds;
return this;
}

// public String getDesc() {
// return desc;
// }
//
// public OrderProperties setDesc(String desc) {
// this.desc = desc;
// return this;
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Nacos 配置项,对应 NacosConfigProperties 配置类
nacos:
config:
server-addr: 127.0.0.1:18848 # Nacos 服务器地址
data-id: example # 使用的 Nacos 配置集的 dataId
type: YAML # 使用的 Nacos 配置集的配置格式
bootstrap:
enable: true # 是否开启 Nacos 配置预加载功能
19 changes: 19 additions & 0 deletions lab-44/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>labs-parent</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-44</artifactId>
<packaging>pom</packaging>
<modules>
<module>lab-44-nacos-config-demo</module>
</modules>


</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<module>lab-41</module>
<module>lab-42</module>
<module>lab-43</module>
<module>lab-44</module>
</modules>


Expand Down

0 comments on commit 5f5e8a2

Please sign in to comment.