forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jan 21, 2020
1 parent
2b5f10a
commit 5f5e8a2
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
56 changes: 56 additions & 0 deletions
56
...44-nacos-config-demo/src/main/java/cn/iocoder/springboot/lab44/nacosdemo/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...acos-config-demo/src/main/java/cn/iocoder/springboot/lab44/nacosdemo/OrderProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
// } | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
lab-44/lab-44-nacos-config-demo/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 配置预加载功能 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters