From 3b58d9ebc1dfae456dfce01ce378350fb29271ac Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Thu, 11 Jun 2020 22:17:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Consul=20=E5=85=A5?= =?UTF-8?q?=E9=97=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- labx-27/pom.xml | 1 + labx-28/labx-28-sc-consul-config-demo/pom.xml | 56 +++++++++++++++++++ .../labx28/consuldemo/DemoApplication.java | 13 +++++ .../consuldemo/config/OrderProperties.java | 52 +++++++++++++++++ .../consuldemo/controller/DemoController.java | 44 +++++++++++++++ .../src/main/resources/bootstrap.yaml | 15 +++++ labx-28/pom.xml | 19 +++++++ pom.xml | 1 + 8 files changed, 201 insertions(+) create mode 100644 labx-28/labx-28-sc-consul-config-demo/pom.xml create mode 100644 labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/DemoApplication.java create mode 100644 labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/config/OrderProperties.java create mode 100644 labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/controller/DemoController.java create mode 100644 labx-28/labx-28-sc-consul-config-demo/src/main/resources/bootstrap.yaml create mode 100644 labx-28/pom.xml diff --git a/labx-27/pom.xml b/labx-27/pom.xml index 0e65b75c..291a242a 100644 --- a/labx-27/pom.xml +++ b/labx-27/pom.xml @@ -10,6 +10,7 @@ 4.0.0 labx-27 + pom labx-27-sc-consul-discovery-demo01-provider diff --git a/labx-28/labx-28-sc-consul-config-demo/pom.xml b/labx-28/labx-28-sc-consul-config-demo/pom.xml new file mode 100644 index 00000000..4539900c --- /dev/null +++ b/labx-28/labx-28-sc-consul-config-demo/pom.xml @@ -0,0 +1,56 @@ + + + + labx-28 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-28-sc-consul-config-demo + + + 2.2.4.RELEASE + Hoxton.SR1 + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.cloud + spring-cloud-starter-consul-config + + + + diff --git a/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/DemoApplication.java b/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/DemoApplication.java new file mode 100644 index 00000000..85ded424 --- /dev/null +++ b/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/DemoApplication.java @@ -0,0 +1,13 @@ +package cn.iocoder.springcloud.labx28.consuldemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class); + } + +} diff --git a/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/config/OrderProperties.java b/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/config/OrderProperties.java new file mode 100644 index 00000000..eb819291 --- /dev/null +++ b/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/config/OrderProperties.java @@ -0,0 +1,52 @@ +package cn.iocoder.springcloud.labx28.consuldemo.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@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; +// } + +} diff --git a/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/controller/DemoController.java b/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/controller/DemoController.java new file mode 100644 index 00000000..57a4603d --- /dev/null +++ b/labx-28/labx-28-sc-consul-config-demo/src/main/java/cn/iocoder/springcloud/labx28/consuldemo/controller/DemoController.java @@ -0,0 +1,44 @@ +package cn.iocoder.springcloud.labx28.consuldemo.controller; + +import cn.iocoder.springcloud.labx28.consuldemo.config.OrderProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/demo") +public class DemoController { + + @Autowired + private OrderProperties orderProperties; + + /** + * 测试 @ConfigurationProperties 注解的配置属性类 + */ + @GetMapping("/test01") + public OrderProperties test01() { + return orderProperties; + } + + @Value(value = "${order.pay-timeout-seconds}") // @NacosValue(value = "${order.pay-timeout-seconds}") + private Integer payTimeoutSeconds; + @Value(value = "${order.create-frequency-seconds}") // @NacosValue(value = "${order.create-frequency-seconds}") + private Integer createFrequencySeconds; + + /** + * 测试 @Value 注解的属性 + */ + @GetMapping("/test02") + public Map test02() { + Map results = new HashMap(); + results.put("payTimeoutSeconds", payTimeoutSeconds); + results.put("createFrequencySeconds", createFrequencySeconds); + return results; + } + +} diff --git a/labx-28/labx-28-sc-consul-config-demo/src/main/resources/bootstrap.yaml b/labx-28/labx-28-sc-consul-config-demo/src/main/resources/bootstrap.yaml new file mode 100644 index 00000000..8f8cb136 --- /dev/null +++ b/labx-28/labx-28-sc-consul-config-demo/src/main/resources/bootstrap.yaml @@ -0,0 +1,15 @@ +spring: + application: + name: demo-application + + cloud: + # Spring Cloud Consul 通用配置项,对应 ConsulProperties 类 + consul: + host: 127.0.0.1 # Consul 主机 + port: 8500 # Consul 端口 + # Spring Cloud Consul Config 配置项,对应 ConsulConfigProperties 类 + config: + format: YAML # Consul 配置的格式 + prefix: config # Consul 配置的目录 + data-key: data # Consul 配置的文件 + profile-separator: ',' # 多环境目录分隔符,默认为 , diff --git a/labx-28/pom.xml b/labx-28/pom.xml new file mode 100644 index 00000000..a603bb36 --- /dev/null +++ b/labx-28/pom.xml @@ -0,0 +1,19 @@ + + + + labs-parent + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-28 + pom + + + labx-28-sc-consul-config-demo + + + diff --git a/pom.xml b/pom.xml index fe4ba1c5..70cc6e50 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,7 @@ labx-27 + labx-28