Skip to content

Commit f7d504e

Browse files
author
YunaiV
committed
增加 Consul 入门
1 parent 98cd933 commit f7d504e

File tree

10 files changed

+302
-1
lines changed

10 files changed

+302
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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-12</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-20-sc-config-server-git-auto-refresh-by-bus</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
20+
</properties>
21+
22+
<!--
23+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25+
-->
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-parent</artifactId>
31+
<version>${spring.boot.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>${spring.cloud.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba.cloud</groupId>
44+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
45+
<version>${spring.cloud.alibaba.version}</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-web</artifactId>
57+
</dependency>
58+
59+
<!-- 引入 Spring Cloud Stream Kafka 相关依赖,将 Kafka 作为消息队列,并实现对其的自动配置 -->
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-config-server</artifactId>
63+
</dependency>
64+
65+
<!-- 引入基于 RocketMQ 的 Spring Cloud Bus 的实现的依赖,并实现对其的自动配置 -->
66+
<dependency>
67+
<groupId>com.alibaba.cloud</groupId>
68+
<artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
69+
</dependency>
70+
71+
<!-- 引入 Spring Cloud Config Monitor 依赖,提供 `/monitor` 接口,用于刷新配置 -->
72+
<dependency>
73+
<groupId>org.springframework.cloud</groupId>
74+
<artifactId>spring-cloud-config-monitor</artifactId>
75+
</dependency>
76+
</dependencies>
77+
78+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springcloud.labx20.configserverdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.config.server.EnableConfigServer;
6+
7+
@SpringBootApplication
8+
@EnableConfigServer
9+
public class ConfigServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(ConfigServerApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
server:
2+
port: 8888
3+
4+
spring:
5+
application:
6+
name: demo-config-server
7+
8+
profiles:
9+
active: git # 使用的 Spring Cloud Config Server 的存储器方案
10+
# Spring Cloud Config 相关配置项
11+
cloud:
12+
config:
13+
server:
14+
# Spring Cloud Config Server 的 Git 存储器的配置项,对应 MultipleJGitEnvironmentProperties 类
15+
git:
16+
uri: https://github.com/YunaiV/demo-config-server.git # Git 仓库地址
17+
search-paths: / # 读取文件的根地址
18+
default-label: master # 使用的默认分支,默认为 master
19+
# username: ${CODING_USERNAME} # 账号
20+
# password: ${CODING_PASSWORD} # 密码
21+
22+
# rocketmq 配置项,对应 RocketMQProperties 配置类
23+
rocketmq:
24+
name-server: 127.0.0.1:9876 # RocketMQ Namesrv
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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-12</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-20-sc-config-user-application-auto-refresh-by-bus</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
20+
</properties>
21+
22+
<!--
23+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25+
-->
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-parent</artifactId>
31+
<version>${spring.boot.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>${spring.cloud.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba.cloud</groupId>
44+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
45+
<version>${spring.cloud.alibaba.version}</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
54+
<dependency>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-starter-web</artifactId>
57+
</dependency>
58+
59+
<!-- 引入 Spring Cloud Config Client 相关依赖,作为配置中心的客户端,并实现自动化配置 -->
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-starter-config</artifactId>
63+
</dependency>
64+
65+
<!-- 引入基于 RocketMQ 的 Spring Cloud Bus 的实现的依赖,并实现对其的自动配置 -->
66+
<dependency>
67+
<groupId>com.alibaba.cloud</groupId>
68+
<artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
69+
</dependency>
70+
</dependencies>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springcloud.labx20.userapplication;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class UserApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(UserApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.iocoder.springcloud.labx20.userapplication.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+
public Integer getPayTimeoutSeconds() {
21+
return payTimeoutSeconds;
22+
}
23+
24+
public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
25+
this.payTimeoutSeconds = payTimeoutSeconds;
26+
return this;
27+
}
28+
29+
public Integer getCreateFrequencySeconds() {
30+
return createFrequencySeconds;
31+
}
32+
33+
public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
34+
this.createFrequencySeconds = createFrequencySeconds;
35+
return this;
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cn.iocoder.springcloud.labx20.userapplication.controller;
2+
3+
import cn.iocoder.springcloud.labx20.userapplication.config.OrderProperties;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.cloud.context.config.annotation.RefreshScope;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
@RestController
15+
@RequestMapping("/demo")
16+
@RefreshScope
17+
public class DemoController {
18+
19+
@Autowired
20+
private OrderProperties orderProperties;
21+
22+
/**
23+
* 测试 @ConfigurationProperties 注解的配置属性类
24+
*/
25+
@GetMapping("/test01")
26+
public OrderProperties test01() {
27+
return orderProperties;
28+
}
29+
30+
@Value(value = "${order.pay-timeout-seconds}")
31+
private Integer payTimeoutSeconds;
32+
@Value(value = "${order.create-frequency-seconds}")
33+
private Integer createFrequencySeconds;
34+
35+
/**
36+
* 测试 @Value 注解的属性
37+
*/
38+
@GetMapping("/test02")
39+
public Map<String, Object> test02() {
40+
Map<String, Object> result = new HashMap<>();
41+
result.put("payTimeoutSeconds", payTimeoutSeconds);
42+
result.put("createFrequencySeconds", createFrequencySeconds);
43+
return result;
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# rocketmq 配置项,对应 RocketMQProperties 配置类
2+
rocketmq:
3+
name-server: 127.0.0.1:9876 # RocketMQ Namesrv
4+
5+
server:
6+
port: 8081
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring:
2+
application:
3+
name: user-application
4+
5+
cloud:
6+
# Spring Cloud Config Client 配置项,对应 ConfigClientProperties 类
7+
config:
8+
uri: http://127.0.0.1:8888 # Spring Cloud Config Server 的地址
9+
name: ${spring.application.name} # 读取的配置文件的名字,默认为 ${spring.application.name}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springcloud.labx29.listenerdemo.binder;
1+
package org.springframework.cloud.consul.binder;
22

33
import com.ecwid.consul.v1.ConsulClient;
44
import com.ecwid.consul.v1.QueryParams;

0 commit comments

Comments
 (0)