diff --git a/labx-26/labx-26-sc-zookeeper-config-auto-refresh/pom.xml b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/pom.xml new file mode 100644 index 00000000..46e21c5e --- /dev/null +++ b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/pom.xml @@ -0,0 +1,56 @@ + + + + labx-26 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-26-sc-zookeeper-config-auto-refresh + + + 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-zookeeper-config + + + + diff --git a/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/DemoApplication.java b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/DemoApplication.java new file mode 100644 index 00000000..c5953230 --- /dev/null +++ b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/DemoApplication.java @@ -0,0 +1,13 @@ +package cn.iocoder.springcloud.labx26.zookeeperdemo; + +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-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/config/OrderProperties.java b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/config/OrderProperties.java new file mode 100644 index 00000000..10a303b7 --- /dev/null +++ b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/config/OrderProperties.java @@ -0,0 +1,52 @@ +package cn.iocoder.springcloud.labx26.zookeeperdemo.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-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/controller/DemoController.java b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/controller/DemoController.java new file mode 100644 index 00000000..e45a0442 --- /dev/null +++ b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/java/cn/iocoder/springcloud/labx26/zookeeperdemo/controller/DemoController.java @@ -0,0 +1,44 @@ +package cn.iocoder.springcloud.labx26.zookeeperdemo.controller; + +import cn.iocoder.springcloud.labx26.zookeeperdemo.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-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/resources/bootstrap.yaml b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/resources/bootstrap.yaml new file mode 100644 index 00000000..20b37238 --- /dev/null +++ b/labx-26/labx-26-sc-zookeeper-config-auto-refresh/src/main/resources/bootstrap.yaml @@ -0,0 +1,14 @@ +spring: + application: + name: demo-application + + cloud: + zookeeper: + connect-string: 127.0.0.1:2181 + # Zookeeper 作为配置中心的配置项,对应 ZookeeperConfigProperties 配置类 + config: + root: /config # Zookeeper 数据存储的根节点,默认为 /config + default-context: application # 读取 Zookeeper 配置的默认目录,默认为 application + profile-separator: ',' # 多环境目录分隔符,默认为 , + watcher: + enabled: true # 是否开启 Watch 功能,监听 Zookeeper 数据的变化。默认为 true,实现实时监听配置的更新 diff --git a/labx-26/labx-26-sc-zookeeper-config-demo/src/main/resources/bootstrap.yaml b/labx-26/labx-26-sc-zookeeper-config-demo/src/main/resources/bootstrap.yaml index bd59d751..20b37238 100644 --- a/labx-26/labx-26-sc-zookeeper-config-demo/src/main/resources/bootstrap.yaml +++ b/labx-26/labx-26-sc-zookeeper-config-demo/src/main/resources/bootstrap.yaml @@ -9,6 +9,6 @@ spring: config: root: /config # Zookeeper 数据存储的根节点,默认为 /config default-context: application # 读取 Zookeeper 配置的默认目录,默认为 application - profile-separator: \, # 多环境目录分隔符,默认为 , + profile-separator: ',' # 多环境目录分隔符,默认为 , watcher: enabled: true # 是否开启 Watch 功能,监听 Zookeeper 数据的变化。默认为 true,实现实时监听配置的更新 diff --git a/labx-26/pom.xml b/labx-26/pom.xml index 2f3c32d6..b5e3a44a 100644 --- a/labx-26/pom.xml +++ b/labx-26/pom.xml @@ -13,6 +13,7 @@ labx-26-sc-zookeeper-config-demo + labx-26-sc-zookeeper-config-auto-refresh