内容简介:本篇主要介绍了关于 Consul 的更多知识点不在这里赘述,但是在学习本节之前还是希望您能先了解下,请移步我之前写的注意:
本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper、Etcd 等,服务注册发现在微服务架构中扮演这一个重要的角色,伴随着服务的大量出现,服务与服务之间的配置管理、运维管理也变的难以维护,通过 Consul 可以解决这些问题,实现服务治理、服务监控。
关于 Consul 的更多知识点不在这里赘述,但是在学习本节之前还是希望您能先了解下,请移步我之前写的 微服务服务注册发现之 Consul 系列
快速导航
注意: 以下代码示例可在Github查看项目完整示例 chapter7-1
添加maven依赖
在 Spring Boot 项目的 pom.xml 文件中引入 spring-cloud-starter-consul-discovery 启动器
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
复制代码
使用 Consul 配置信息时需要引入 spring-cloud-starter-consul-config 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
复制代码
配置文件
系统级配置文件 bootstrap.yml
使用 Spring Cloud Consul Config ,需要配置以下信息在 bootstrap.yml 文件
spring.cloud.consul.host spring.cloud.consul.port spring.cloud.consul.config.prefix spring.cloud.consul.config.enabled spring.cloud.consul.config.format spring.cloud.consul.config.data-key
bootstrap.yml
spring:
cloud:
consul:
host: 192.168.6.128
port: 8500
config:
prefix: config
enabled: true
format: YAML
data-key: user
复制代码
应用级配置文件 application.yml
定义应用级别的配置在 bootstrap.yml 之后加载,例如搭配 spring-cloud-config 使用。
spring.cloud.consul.host spring.cloud.consul.port spring.cloud.consul.discovery.enabled spring.cloud.consul.discovery.register spring.cloud.consul.discovery.deregister spring.cloud.consul.discovery.prefer-ip-address spring.cloud.consul.discovery.health-check-interval spring.cloud.consul.discovery.health-check-path spring.cloud.consul.discovery.health-check-critical-timeout spring.cloud.consul.discovery.instance-id
server:
port: 8082
spring:
application:
name: consul-service
profiles:
active: dev
cloud:
consul:
host: 192.168.6.128
port: 8500
discovery: # 服务发现配置
enabled: true
register: true
deregister: true
prefer-ip-address: true
health-check-interval: 10s
health-check-critical-timeout: 30s
health-check-path: /health
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} # 应用名称+服务器IP+端口
复制代码
配置Consul管理控制台
Consul 提供了 Key/Value 存储用于存储配置数据,在 Spring Cloud Consul 中配置默认存储于 /config 文件夹下,根据应用程序名和模拟 Spring Cloud Config 顺序解析属性的规则来配置文件。
在本例中操作 Consul 管控台建立以下路径配置:
-
config:为配置基本文件,这里默认为config。 -
consul-service:为application.yml中配置的spring.application.name值。 -
dev:为application.yml中配置的spring.profiles.active值,也是本程序设置环境变量意为开发环境。 -
user.yml:为配置的文件名,格式为yml格式。
config/consul-service.dev/user.yml 复制代码
最终为 Consul 管控台建立的配置数据如下图所示:
项目构建
注意:以下只贴核心代码,源码参见: Github chapter7-1
建立Config获取Consul配置数据
- 获取student配置数据
注意以下属性名要与在 Consul 管控台中配置的一一对应。
@ConfigurationProperties 进行属性注入
config/StudentConfig.java
@ConfigurationProperties(prefix = "student")
public class StudentConfig {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "大家好我是" + name + ",今年" + age + "岁,我是一名在校大学生!";
}
}
复制代码
- 获取teach配置数据
同以上 student 配置,我们可以将不同类型的配置分文件进行配置定义
@ConfigurationProperties(prefix = "teach")
public class TeachConfig {
private String name;
private String course;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
@Override
public String toString() {
return "大家好我是" + name + ",是一名大学老师!教同学们学习" + course;
}
}
复制代码
编写启动类调用配置
注解说明:
-
EnableDiscoveryClient:让注册中心进行服务发现,将服务注册到服务组件上。 -
RestController:是@ResponseBody和@Controller注解的组合,注明该注解后整个类所有的方法返回值为json格式。 -
SpringBootApplication:SpringBoot 的启动注解。 -
EnableConfigurationProperties:属性配置的 class 添加到 SpringBoot 的属性配置注解里,否则不能通过@Autowired注解注入我们定义的属性配置类。
接口说明:
/health /user/description /user/student/intro /user/teach/intro
@EnableDiscoveryClient
@RestController
@SpringBootApplication
@EnableConfigurationProperties({ StudentConfig.class, TeachConfig.class })
public class ConsulApplication {
@Value("${description}")
private String description;
@Autowired
private StudentConfig studentConfig;
@Autowired
private TeachConfig teachConfig;
@GetMapping("/health")
public String Health() {
System.out.println("health");
return "OK";
}
@GetMapping("/user/description")
public String Description() {
return description;
}
@GetMapping("/user/student/intro")
public String StudentIntro() {
return studentConfig.toString();
}
@GetMapping("/user/teach/intro")
public String TeachIntro() {
return teachConfig.toString();
}
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class, args);
}
}
复制代码
接口测试
- 健康检查接口
该接口在服务启动后且向 Consul 配置中心注册后,根据 application.yml 文件配置的 health-check-interval 和 health-check-path属性进行自动调用。
$ curl http://127.0.0.1:8082/health OK! 复制代码
注册成功后展示我们服务的名称及健康检查结果如下:
- 获取教师简介配置接口
$ curl http://127.0.0.1:8082/user/teach/intro 大家好我是Teach Li,是一名大学老师!教同学们学习 Java 软件开发! 复制代码
- 获取学生简介配置接口
$ curl http://127.0.0.1:8082/user/student/intro 大家好我是Jack,今年18岁,我是一名在校大学生! 复制代码
- 采用 @Value 注解获取项目描述接口
$ curl http://127.0.0.1:8082/user/description 用户信息描述 复制代码
总结
这里我们只介绍了 Consul 在 Spring Boot 的配置功能,关于 Consul 做为注册中心在下一章节中介绍,本篇中需要注意通过 @Value 注入的属性,修改 Consul 后需要重启服务才能生效,通过 @ConfigurationProperties 注入的属性,在 Consul 管控台修改属性之后可立即生效。
如遇到其他什么可在 SpringBoot-Course issues 中提问
以上所述就是小编给大家介绍的《Spring Boot实战系列(7)集成Consul配置中心》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Jenkins 持续集成综合实战
- Go 和 Android 集成实战
- 实战 | Apache Hudi 集成 Alluxio
- Spring Boot集成Redis实战操作
- 持续集成之 Spring Boot 实战篇
- Java SpringBoot集成RabbitMQ实战和总结
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Hatching Twitter
Nick Bilton / Portfolio Hardcover / 2013-11-5 / USD 28.95
The dramatic, unlikely story behind the founding of Twitter, by New York Times bestselling author and Vanity Fair special correspondent The San Francisco-based technology company Twitter has become......一起来看看 《Hatching Twitter》 这本书的介绍吧!