注册中心/配置管理 —— SpringCloud Consul

立山  金牌会员 | 2023-8-30 04:42:55 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 930|帖子 930|积分 2790

Consul 概述

Consul 是一个可以提供服务发现,健康检查,多数据中心,key/Value 存储的分布式服务框架,用于实现分布式系统的发现与配置。Cousul 使用 Go 语言实现,因此天然具有可移植性,安装包仅包含一个可执行文件,直接启动即可运行,方便部署

Consul 安装与启动

以 windows 为例,在官网下载 Consul:https://www.consul.io/

下载之后解压缩,进入目录运行 consul.exe 即可:.\consul.exe agent -dev
Consul 启动完成后,在浏览器中访问 http://ocalhost:8500/ 便可以看到 Consul 首页

Consul 服务注册与发现

创建 cousul-service 项目,引入依赖,其中 Spring Boot Actuator 是健康检查需要依赖的包,本项目基于 SpringBoot 2.3.1,SpringCloud Hoxton.SR12
  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.springframework.boot</groupId>
  4.         <artifactId>spring-boot-starter-actuator</artifactId>
  5.     </dependency>
  6.     <dependency>
  7.         <groupId>org.springframework.boot</groupId>
  8.         <artifactId>spring-boot-starter-web</artifactId>
  9.     </dependency>
  10.     <dependency>
  11.         <groupId>org.springframework.cloud</groupId>
  12.         <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  13.     </dependency>
  14. </dependencies>
复制代码
在 application.yml 配置文件中添加如下配置:
  1. server:
  2.   port: 8080
  3. spring:
  4.   application:
  5.     name: consul-service
  6.   cloud:
  7.     consul:
  8.       host: localhost
  9.       port: 8500
  10.       discovery:
  11.         instance-id: ${spring.application.name}:${server.port}
复制代码
在启动类上添加注解 @EnableDiscoveryClient
  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class ConsulProducerApplication {
  4.     public static void main(String[] args) {
  5.         SpringApplication.run(ConsulProducerApplication.class, args);
  6.     }
  7. }
复制代码
启动项目,查看 Consul Web 页面,即可看到服务注册成功

Consul 配置中心

参考上一节内容创建 cousul-config 项目,引入依赖
  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.springframework.boot</groupId>
  4.         <artifactId>spring-boot-starter-actuator</artifactId>
  5.     </dependency>
  6.     <dependency>
  7.         <groupId>org.springframework.boot</groupId>
  8.         <artifactId>spring-boot-starter-web</artifactId>
  9.     </dependency>
  10.     <dependency>
  11.         <groupId>org.springframework.cloud</groupId>
  12.         <artifactId>spring-cloud-starter-consul-config</artifactId>
  13.     </dependency>
  14.     <dependency>
  15.         <groupId>org.springframework.cloud</groupId>
  16.         <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  17.     </dependency>
  18. </dependencies>
复制代码
在 bootstrap.yml 配置文件(注意必须使用 bootstrap)中添加如下配置:
  1. server:
  2.   port: 8080
  3. spring:
  4.   application:
  5.     name: consul-service
  6.   # profiles:
  7.     # active: dev # 指定环境,默认加载 default 环境
  8.   cloud:
  9.     consul:
  10.       host: localhost
  11.       port: 8500
  12.       discovery:
  13.         instance-id: ${spring.application.name}:${server.port}
  14.       config:
  15.         enabled: true # false禁用Consul配置,默认为true
  16.         format: yaml  # 表示consul上面文件的格式,有四种:YAML、PROPERTIES、KEY-VALUE、FILES
  17.         prefix: config  # 可以理解为配置文件所在的最外层目录
  18.         default-context: consul-service # 设置应用的文件夹名称
  19.         data-key: consul-service-config # Consul的Key/Values中的Key,Value对应整个配置文件
  20.         # 以上配置可以理解为:加载config/consul-service/文件夹下Key为consul-service-config的Value对应的配置信息
  21.         # 配置环境分隔符,默认值 "," 和 default-context 配置项搭配
  22.         # 例如应用 consul-service 分别有环境 default、dev、test、prod
  23.         # 只需在 config 文件夹下创建 consul-service、consul-service-dev、consul-service-test、consul-service-prod 文件夹即可
  24.         # profile-separator: '-'
  25.         watch:
  26.           enabled: true # 是否开启自动刷新,默认值true开启
  27.           delay: 1000 # 刷新频率,单位毫秒,默认值1000
复制代码
在启动类上添加注解 @EnableDiscoveryClient
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. // 启用配置属性类,当SpringBoot程序启动时会立即加载@EnableConfigurationProperties注解中指定的类对象
  4. @EnableConfigurationProperties({MySqlComplexConfig.class})
  5. public class ConsulConfigApplication {
  6.     public static void main(String[] args) {
  7.         SpringApplication.run(ConsulConfigApplication.class, args);
  8.     }
  9. }
复制代码
定义 MysqlConfig 配置类
  1. @Component
  2. @ConfigurationProperties(prefix = "mysql")
  3. public class MysqlConfig {
  4.     private String host;
  5.     private String username;
  6.     private String password;
  7.     public String getHost() {
  8.         return host;
  9.     }
  10.     public void setHost(String host) {
  11.         this.host = host;
  12.     }
  13.     public String getUsername() {
  14.         return username;
  15.     }
  16.     public void setUsername(String username) {
  17.         this.username = username;
  18.     }
  19.     public String getPassword() {
  20.         return password;
  21.     }
  22.     public void setPassword(String password) {
  23.         this.password = password;
  24.     }
  25. }
复制代码
开发 ConfigController
  1. @RefreshScope // 用于重新刷新作用域实现属性值自动刷新
  2. @RestController
  3. public class ConfigController {
  4.     @Autowired
  5.     private MysqlConfig mysqlConfig;
  6.     @GetMapping("getConfig")
  7.     public Map<String, String> getMysqlConfig() {
  8.         HashMap<String, String> map = new HashMap<>();
  9.         map.put("host", mysqlConfig.getHost());
  10.         map.put("username", mysqlConfig.getUsername());
  11.         map.put("password", mysqlConfig.getPassword());
  12.         return map;
  13.     }
  14. }
复制代码
在 Consul 管理界面添加配置信息,点击左侧菜单的 Key/Value,按照 bootstrap.yml 中的配置创建 config/consul-service 目录,在 consul-service 目录下创建 key:consul-service-config,在 value 添加配置信息

请求 http://localhost:8080/getConfig,可以看到服务会从 Consul 中获取配置,并返回

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立山

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表