Skip to content

Commit 3b58d9e

Browse files
author
YunaiV
committedJun 11, 2020
增加 Consul 入门
1 parent 1ed2283 commit 3b58d9e

File tree

8 files changed

+201
-0
lines changed

8 files changed

+201
-0
lines changed
 

‎labx-27/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>labx-27</artifactId>
13+
<packaging>pom</packaging>
1314

1415
<modules>
1516
<module>labx-27-sc-consul-discovery-demo01-provider</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-28</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-28-sc-consul-config-demo</artifactId>
13+
14+
<properties>
15+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
16+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
17+
</properties>
18+
19+
<!--
20+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
21+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
22+
-->
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-parent</artifactId>
28+
<version>${spring.boot.version}</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-dependencies</artifactId>
35+
<version>${spring.cloud.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
43+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-web</artifactId>
47+
</dependency>
48+
49+
<!-- 引入 Spring Cloud Consul Config 相关依赖,将 Consul 作为配置中心,并实现对其的自动配置 -->
50+
<dependency>
51+
<groupId>org.springframework.cloud</groupId>
52+
<artifactId>spring-cloud-starter-consul-config</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springcloud.labx28.consuldemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cn.iocoder.springcloud.labx28.consuldemo.config;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@ConfigurationProperties(prefix = "order")
8+
public class OrderProperties {
9+
10+
/**
11+
* 订单支付超时时长,单位:秒。
12+
*/
13+
private Integer payTimeoutSeconds;
14+
15+
/**
16+
* 订单创建频率,单位:秒
17+
*/
18+
private Integer createFrequencySeconds;
19+
20+
// /**
21+
// * 配置描述
22+
// */
23+
// private String desc;
24+
25+
public Integer getPayTimeoutSeconds() {
26+
return payTimeoutSeconds;
27+
}
28+
29+
public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
30+
this.payTimeoutSeconds = payTimeoutSeconds;
31+
return this;
32+
}
33+
34+
public Integer getCreateFrequencySeconds() {
35+
return createFrequencySeconds;
36+
}
37+
38+
public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
39+
this.createFrequencySeconds = createFrequencySeconds;
40+
return this;
41+
}
42+
43+
// public String getDesc() {
44+
// return desc;
45+
// }
46+
//
47+
// public OrderProperties setDesc(String desc) {
48+
// this.desc = desc;
49+
// return this;
50+
// }
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.iocoder.springcloud.labx28.consuldemo.controller;
2+
3+
import cn.iocoder.springcloud.labx28.consuldemo.config.OrderProperties;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@RestController
14+
@RequestMapping("/demo")
15+
public class DemoController {
16+
17+
@Autowired
18+
private OrderProperties orderProperties;
19+
20+
/**
21+
* 测试 @ConfigurationProperties 注解的配置属性类
22+
*/
23+
@GetMapping("/test01")
24+
public OrderProperties test01() {
25+
return orderProperties;
26+
}
27+
28+
@Value(value = "${order.pay-timeout-seconds}") // @NacosValue(value = "${order.pay-timeout-seconds}")
29+
private Integer payTimeoutSeconds;
30+
@Value(value = "${order.create-frequency-seconds}") // @NacosValue(value = "${order.create-frequency-seconds}")
31+
private Integer createFrequencySeconds;
32+
33+
/**
34+
* 测试 @Value 注解的属性
35+
*/
36+
@GetMapping("/test02")
37+
public Map<String, Object> test02() {
38+
Map<String, Object> results = new HashMap<String, Object>();
39+
results.put("payTimeoutSeconds", payTimeoutSeconds);
40+
results.put("createFrequencySeconds", createFrequencySeconds);
41+
return results;
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
spring:
2+
application:
3+
name: demo-application
4+
5+
cloud:
6+
# Spring Cloud Consul 通用配置项,对应 ConsulProperties 类
7+
consul:
8+
host: 127.0.0.1 # Consul 主机
9+
port: 8500 # Consul 端口
10+
# Spring Cloud Consul Config 配置项,对应 ConsulConfigProperties 类
11+
config:
12+
format: YAML # Consul 配置的格式
13+
prefix: config # Consul 配置的目录
14+
data-key: data # Consul 配置的文件
15+
profile-separator: ',' # 多环境目录分隔符,默认为 ,

‎labx-28/pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-28</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>labx-28-sc-consul-config-demo</module>
17+
</modules>
18+
19+
</project>

‎pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
<!-- <module>labx-26</module>-->
105105

106106
<module>labx-27</module>
107+
<module>labx-28</module>
107108
</modules>
108109

109110
</project>

0 commit comments

Comments
 (0)
Please sign in to comment.