mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 Spring Cloud Zookeeper 配置中心入门
This commit is contained in:
56
labx-26/labx-26-sc-zookeeper-config-auto-refresh/pom.xml
Normal file
56
labx-26/labx-26-sc-zookeeper-config-auto-refresh/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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>labx-26</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-26-sc-zookeeper-config-auto-refresh</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Zookeeper Config 相关依赖,将 Zookeeper 作为配置中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -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<String, Object> test02() {
|
||||
Map<String, Object> results = new HashMap<String, Object>();
|
||||
results.put("payTimeoutSeconds", payTimeoutSeconds);
|
||||
results.put("createFrequencySeconds", createFrequencySeconds);
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,实现实时监听配置的更新
|
||||
@@ -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,实现实时监听配置的更新
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
<modules>
|
||||
<module>labx-26-sc-zookeeper-config-demo</module>
|
||||
<module>labx-26-sc-zookeeper-config-auto-refresh</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user