Skip to content

Commit 8ee2d72

Browse files
committed
add readme in spring cloud consul config center sample
1 parent abf0be1 commit 8ee2d72

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

dubbo-consul-sample/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,21 @@ Master节点采用Raft算法保证多个Master节点数据一致性,Master节
9898
check必须是Script、HTTP、TCP、TTL四中类型的一种
9999

100100
**Script Check**
101+
101102
通过执行外部应用进行健康状态检查;
102103

103104
**HTTP Check**
105+
104106
这种检查将按照预设的时间间隔创建一个HTTP get请求,相应状态码必须为2XX系列,在SpringCloud中通常使用HTTP Check的方式;
105107

106108
**TCP Check**
109+
107110
根据设置的IP/端口创建一个TCP连接,连接成功为Success,失败是Critical状态;
108111

109112
**TTL Check**
113+
110114
这种Checks为给定的TTL保留了最后一种状态,Checks的状态必须通过HTTP接口周期性跟新状态,如果外部接口没有更新状态,那么状态就会被认为不正常;
111115
这种机制,在概念上类似“死人开关”,需要服务周期性汇报监控状态。在高版本的Dubbo中扩展了Consul使用的是TTL Check机制;
112-
113116
### Consul 安装部署
114117

115118
#### 1、下载与安装

springcloud-consul-config-sample/README.md

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,19 @@ Master节点采用Raft算法保证多个Master节点数据一致性,Master节
9898
check必须是Script、HTTP、TCP、TTL四中类型的一种
9999

100100
**Script Check**
101+
101102
通过执行外部应用进行健康状态检查;
102103

103104
**HTTP Check**
105+
104106
这种检查将按照预设的时间间隔创建一个HTTP get请求,相应状态码必须为2XX系列,在SpringCloud中通常使用HTTP Check的方式;
105107

106108
**TCP Check**
109+
107110
根据设置的IP/端口创建一个TCP连接,连接成功为Success,失败是Critical状态;
108111

109112
**TTL Check**
113+
110114
这种Checks为给定的TTL保留了最后一种状态,Checks的状态必须通过HTTP接口周期性跟新状态,如果外部接口没有更新状态,那么状态就会被认为不正常;
111115
这种机制,在概念上类似“死人开关”,需要服务周期性汇报监控状态。在高版本的Dubbo中扩展了Consul使用的是TTL Check机制;
112116

@@ -157,4 +161,98 @@ consul agent -server -ui -bootstrap-expect=3 -data-dir=/data/consul -node=agent-
157161
<img src="https://ipman-blog-1304583208.cos.ap-nanjing.myqcloud.com/dubbo/1121609122338_.pic.jpg" width = "800" height = "280" alt="图片名称" align=center />
158162

159163
### SpringCloud集成Consul框架-实现配置中心实战
160-
Conusl提供了一个Key/Value Store
164+
Conusl提供了一个Key/Value Store可以用于动态配置;
165+
166+
#### 1.添加Consul依赖,Springboot使用2.1.7版本(不同版本可能存在兼容问题,这个需要注意!)
167+
```java
168+
<dependency>
169+
<groupId>org.springframework.cloud</groupId>
170+
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
171+
<version>2.1.4.RELEASE</version>
172+
</dependency>
173+
174+
<dependency>
175+
<groupId>org.springframework.cloud</groupId>
176+
<artifactId>spring-cloud-starter-consul-config</artifactId>
177+
<version>2.1.4.RELEASE</version>
178+
</dependency>
179+
```
180+
181+
#### 2.添加Spring环境配置(application.properties)、Consul配置(bootstrap.properties)
182+
**application.properties**
183+
```java
184+
spring.profiles.active=dev
185+
spring.application.name=consul-config
186+
server.port=8081
187+
```
188+
189+
**bootstrap.properties**
190+
```java
191+
#配置consul地址
192+
spring.cloud.consul.host=10.211.55.8
193+
#配置consul端口
194+
spring.cloud.consul.port=8500
195+
#指定服务的 consul service name
196+
spring.cloud.consul.discovery.serviceName=consul-config
197+
#启动consul配置中心
198+
spring.cloud.consul.config.enabled=true
199+
#配置基本文件格式
200+
spring.cloud.consul.config.format=yaml
201+
#配置基本文件,默认值config
202+
spring.cloud.consul.config.prefix=config
203+
#profileSeparator设置用于使用配置文件在属性源中分隔配置文件名称的分隔符的值
204+
spring.cloud.consul.config.profile-separator=:
205+
#表示 consul 上面的 KEY 值(或者说文件的名字),默认是 data
206+
spring.cloud.consul.config.data-key=data
207+
# 健康检查url
208+
spring.cloud.consul.discovery.health-check-url=http://10.211.55.2:8081/actuator/health
209+
# 健康检查的频率, 默认 10
210+
spring.cloud.consul.discovery.health-check-interval=10s
211+
# 健康检查失败多长时间后,取消注册
212+
spring.cloud.consul.discovery.health-check-critical-timeout=5s
213+
```
214+
215+
#### 3.配置Spring入口程序,开启@EnableDiscoveryClient注册服务
216+
```java
217+
@SpringBootApplication
218+
@EnableDiscoveryClient //让注册中心进行服务发现,将服务注册到服务组件上
219+
public class SpringcloudConsulConfigSampleApplication {
220+
221+
public static void main(String[] args) {
222+
SpringApplication.run(SpringcloudConsulConfigSampleApplication.class, args);
223+
}
224+
}
225+
```
226+
227+
#### 4.添加配置实例,用@RefreshScope声明自动更新配置,当配置变更时,将会通过Spring Cloud Bus发送RefreshRemoteApplicationEvent事件给相关程序,在RefreshListener中,开始对于配置的刷新;
228+
```java
229+
@RestController
230+
@RequestMapping("/consul")
231+
@RefreshScope // 如果参数变化,自动刷新
232+
public class ConsulConfigCenterController {
233+
234+
@Value("${config.info}")
235+
private String configInfo;
236+
237+
@GetMapping("/config/get")
238+
public Object getItem() {
239+
return configInfo;
240+
}
241+
}
242+
```
243+
244+
#### 5.配置Conusl的Key/Value Store功能,添加yarm配置
245+
创建目录:config/consul-config:dev/data(需要和bootstrap.properties配置的内容一致)
246+
247+
<img src="https://ipman-blog-1304583208.cos.ap-nanjing.myqcloud.com/dubbo/1141609145146_.pic.jpg" width = "800" height = "480" alt="图片名称" align=center />
248+
249+
#### 6.启动项目,进行测试
250+
```java
251+
% curl -i -l http://127.0.0.1:8081/consul/config/get
252+
HTTP/1.1 200
253+
Content-Type: text/plain;charset=UTF-8
254+
Content-Length: 19
255+
Date: Mon, 28 Dec 2020 08:49:36 GMT
256+
257+
config info for dev
258+
```

springcloud-consul-config-sample/src/main/java/com/ipman/springcloud/consul/config/center/sample/SpringcloudConsulConfigSampleApplication.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
66

77
@SpringBootApplication
8-
@EnableDiscoveryClient
8+
@EnableDiscoveryClient //让注册中心进行服务发现,将服务注册到服务组件上
99
public class SpringcloudConsulConfigSampleApplication {
1010

1111
public static void main(String[] args) {
1212
SpringApplication.run(SpringcloudConsulConfigSampleApplication.class, args);
1313
}
14-
1514
}

0 commit comments

Comments
 (0)