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

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>

在 application.yml 配置文件中添加如下配置:

server:
port: 8080 spring:
application:
name: consul-service
cloud:
consul:
host: localhost
port: 8500
discovery:
instance-id: ${spring.application.name}:${server.port}

在启动类上添加注解 @EnableDiscoveryClient

@EnableDiscoveryClient
@SpringBootApplication
public class ConsulProducerApplication { public static void main(String[] args) {
SpringApplication.run(ConsulProducerApplication.class, args);
}
}

启动项目,查看 Consul Web 页面,即可看到服务注册成功

Consul 配置中心

参考上一节内容创建 cousul-config 项目,引入依赖

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>

在 bootstrap.yml 配置文件(注意必须使用 bootstrap)中添加如下配置:

server:
port: 8080 spring:
application:
name: consul-service
# profiles:
# active: dev # 指定环境,默认加载 default 环境
cloud:
consul:
host: localhost
port: 8500
discovery:
instance-id: ${spring.application.name}:${server.port}
config:
enabled: true # false禁用Consul配置,默认为true
format: yaml # 表示consul上面文件的格式,有四种:YAML、PROPERTIES、KEY-VALUE、FILES
prefix: config # 可以理解为配置文件所在的最外层目录
default-context: consul-service # 设置应用的文件夹名称
data-key: consul-service-config # Consul的Key/Values中的Key,Value对应整个配置文件
# 以上配置可以理解为:加载config/consul-service/文件夹下Key为consul-service-config的Value对应的配置信息
# 配置环境分隔符,默认值 "," 和 default-context 配置项搭配
# 例如应用 consul-service 分别有环境 default、dev、test、prod
# 只需在 config 文件夹下创建 consul-service、consul-service-dev、consul-service-test、consul-service-prod 文件夹即可
# profile-separator: '-'
watch:
enabled: true # 是否开启自动刷新,默认值true开启
delay: 1000 # 刷新频率,单位毫秒,默认值1000

在启动类上添加注解 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
// 启用配置属性类,当SpringBoot程序启动时会立即加载@EnableConfigurationProperties注解中指定的类对象
@EnableConfigurationProperties({MySqlComplexConfig.class})
public class ConsulConfigApplication { public static void main(String[] args) {
SpringApplication.run(ConsulConfigApplication.class, args);
}
}

定义 MysqlConfig 配置类

@Component
@ConfigurationProperties(prefix = "mysql")
public class MysqlConfig { private String host;
private String username;
private String password; public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

开发 ConfigController

@RefreshScope // 用于重新刷新作用域实现属性值自动刷新
@RestController
public class ConfigController { @Autowired
private MysqlConfig mysqlConfig; @GetMapping("getConfig")
public Map<String, String> getMysqlConfig() {
HashMap<String, String> map = new HashMap<>();
map.put("host", mysqlConfig.getHost());
map.put("username", mysqlConfig.getUsername());
map.put("password", mysqlConfig.getPassword());
return map;
}
}

在 Consul 管理界面添加配置信息,点击左侧菜单的 Key/Value,按照 bootstrap.yml 中的配置创建 config/consul-service 目录,在 consul-service 目录下创建 key:consul-service-config,在 value 添加配置信息

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

注册中心/配置管理 —— SpringCloud Consul的更多相关文章

  1. 服务注册发现与注册中心对比-Eureka,Consul,Zookeeper,Nacos对比

    服务注册发现与注册中心对比-Eureka,Consul,Zookeeper,Nacos对比 注册中心简介 流程和原理 基础流程 核心功能 1.Eureka.Consul.Zookeeper三者异同点 ...

  2. 注册中心(Eureka/Consul)

    基于SpringBoot1.5.4与SpringCloud(Dalston.SR2)的SpringCloud学习博客,转载请标明出处,O(∩_∩)O谢谢 - Spring Cloud简介 Spring ...

  3. 注册中心ZooKeeper,Eureka,Consul,Nacos对比

    简介 ​ 服务注册中心本质上是为了解耦服务提供者和服务消费者.对于任何一个微服务,原则上都应存在或者支持多个提供者,这是由微服务的分布式属性决定的.更进一步,为了支持弹性扩缩容特性,一个微服务的提供者 ...

  4. 【springcloud alibaba】注册中心之nacos

    1.为什么需要注册中心 1.1 没有注册中心会怎么样 1.2 注册中心提供什么功能以及解决什么问题 2.常用的微服务注册中心对比 3.案例项目父工程 4.nacos作为注册中心的使用 4.1 单机版的 ...

  5. Spring Cloud2.0之整合Consul作为注册中心

    使用Consul来替换Eureka Consul简介 Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发. 它具有很多优点.包括: 基于 raft ...

  6. 微服务系列之 Consul 注册中心

    原文链接:https://mrhelloworld.com/posts/spring/spring-cloud/consul-service-registry/ Netflix Eureka 2.X ...

  7. 微服务架构 | 3.4 HashiCorp Consul 注册中心

    目录 前言 1. Consul 基础知识 1.1 Consul 是什么 1.2 Consul 的特点 2. 安装并运行 Consul 服务器 2.1 下载 Consul 2.2 运行 Consul 服 ...

  8. SpringCloud(一)Eureka注册中心

    Eureka简介 Eureka作为注册中心,管理各种服务功能包括服务的注册.发现.熔断.负载.降级等 Eureka注册中心实例 Eureka Server 1.pom文件配置SpringBoot.Sp ...

  9. 基于spring-cloud的微服务(1) 服务注册中心eureka

    eureka是Netflix提供的服务注册中心组建,springcloud将其做了封装,作为自己的微服务架构中的一个注册中心组建 下面的例子在IDEA中启动一个eureka的实例,然后提供一个prov ...

  10. Spring Cloud 系列之 Consul 注册中心(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Consul 注册中心(一) 本篇文章讲解 Consul 集群环境的搭建. Consul 集群 上图是一个简单的 Co ...

随机推荐

  1. JS判断点是否在线段上

    本文利用向量的点积和叉积来判断点是否在线段上. 基础知识补充 从零开始的高中数学--向量.向量的点积.带你一次搞懂点积(内积).叉积(外积).Unity游戏开发--向量运算(点乘和叉乘 说明 点积可以 ...

  2. goto关键词

    1.前言 goto,一个蒟蒻一用就废,大佬一用就吊炸天的神奇关键字. 今天,我要来盘它!!! 2.goto只能在函数内实现跳转,不能跨函数跳转 因为标号label是局部有效的. #include &l ...

  3. Codeforces Round #693 (Div. 3) A~E 题解

    写在前边 链接:Codeforces Round #693 (Div. 3) 没有打,闲的没事补一下题. A. Cards for Friends 链接:A题链接 题目大意: 给定一张\(w*h\)的 ...

  4. Python9*9乘法表

    for i in range(1, 10): for j in range(1, i+1): n = i*j print('%s*%s=%s' % (i, j, n), end=' ') if i = ...

  5. 生产实践:Redis与Mysql的数据强一致性方案

    公众号「架构成长指南」,专注于生产实践.云原生.分布式系统.大数据技术分享. 数据库和Redis如何保持强一致性,这篇文章告诉你 目的 Redis和Msql来保持数据同步,并且强一致,以此来提高对应接 ...

  6. 玩转开源 |Hugo 的使用实践

    Hugo 是一个能够以出色速度构建静态网页的工具,它为我们提供了极具灵活性的平台,可以塑造成符合个人需求的网页.在上一篇博文中已经介绍了 Hugo 的基本搭建步骤,那如何使用 Hugo 搭建符合自己需 ...

  7. vim的各种快捷键

    目录 一.准备一份用于练习的文件 二.命令模式下的快捷键 1.移动光标 2.删除文字 3.复制粘贴 4.撤销上一次操作 5.恢复上一次撤销的操作 6.查找 7.替换单个字符 8.光标移动到最左边 9. ...

  8. Go:条件控制语句

    在 Go 语言中,主要的条件控制语句有 if-else.switch 和 select.以下是对它们的简单介绍: 1. if 语句: if 语句用于根据条件执行不同的代码块.它的基本形式如下: if ...

  9. 开发工具使用:CubeMX、KEIL MDK-ARM

    来源:成电<微机原理与嵌入式系统>漆强 第四章 STM32CubeMX软件的使用 来源:成电<微机原理与嵌入式系统>漆强 第五章 MDK-ARM软件的使用 一.STM32的Cu ...

  10. RocksDB 在 vivo 消息推送系统中的实践

    作者:vivo 互联网服务器团队 - Zeng Luobin 本文主要介绍了 RocksDB 的基础原理,并阐述了 RocksDB 在vivo消息推送系统中的一些实践,通过分享一些对 RocksDB ...