增加 Consul 入门

This commit is contained in:
YunaiV
2020-06-11 22:17:36 +08:00
parent 1ed2283f8d
commit 3b58d9ebc1
8 changed files with 201 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>labx-27</artifactId>
<packaging>pom</packaging>
<modules>
<module>labx-27-sc-consul-discovery-demo01-provider</module>

View 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-28</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>labx-28-sc-consul-config-demo</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 Consul Config 相关依赖,将 Consul 作为配置中心,并实现对其的自动配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -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);
}
}

View File

@@ -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;
// }
}

View File

@@ -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<String, Object> test02() {
Map<String, Object> results = new HashMap<String, Object>();
results.put("payTimeoutSeconds", payTimeoutSeconds);
results.put("createFrequencySeconds", createFrequencySeconds);
return results;
}
}

View File

@@ -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: ',' # 多环境目录分隔符,默认为 ,

19
labx-28/pom.xml Normal file
View File

@@ -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>labx-28</artifactId>
<packaging>pom</packaging>
<modules>
<module>labx-28-sc-consul-config-demo</module>
</modules>
</project>

View File

@@ -104,6 +104,7 @@
<!-- <module>labx-26</module>-->
<module>labx-27</module>
<module>labx-28</module>
</modules>
</project>