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. 从HumanEval到CoderEval: 你的代码生成模型真的work吗?

    本文分享自华为云社区<从HumanEval到CoderEval: 你的代码生成模型真的work吗?>,作者:华为云软件分析Lab . 本文主要介绍了一个名为CoderEval的代码生成大模 ...

  2. Linux TTY/PTS

    转载:https://segmentfault.com/a/1190000009082089 当我们在键盘上敲下一个字母的时候,到底是怎么发送到相应的进程的呢?我们通过ps.who等命令看到的类似tt ...

  3. 发现AI自我意识:从理解到思维

    广义"理解"已经实现 在最新的人工智能系统中,我们经常可以观察到一种类似"理解"的能力.这种广义的"理解"能力,主要建立在两个基础之上:海量 ...

  4. 一个小型markdown读写编辑器推荐

    在作业完成时我和很多同学们发现并没有很好地利用博客园里面自带的markdown标记语法的形式完成作业 因此我在网上自己搜索发现了这款软件 下载地址 在帮助里可以学到一些简单的使用形式 可以方便编辑已有 ...

  5. Vue06-Pinia

    Pinia Pinia是西班牙语piña(西班牙语中的"菠萝")单词的形似. 它是一个状态管理的库,用于跨组件.页面进行状态共享(这点和Vuex.Redux一样),同时兼容Vue2 ...

  6. Cplex学术版申请及Python API环境配置

    当使用Cplex时弹出下面错误: CPLEX Error 1016: Community Edition. Problem size limits exceeded. Purchase at http ...

  7. ASP.NET Core 8 在 Windows 上各种部署模型的性能测试

    ASP.NET Core 8 在 Windows 上各种部署模型的性能测试 我们知道 Asp.net Core 在 windows 服务器上部署的方案有 4 种之多.这些部署方案对性能的影响一直以来都 ...

  8. 分享一个 asp.net core 多文件分块同时上传的组件

    分享一个可多个文件同时上传.断点续传,并实时反馈上传进度的 Asp.Net core 组件. 服务器端 引用 nuget 包:JMS.FileUploader.AspNetCore 然后启用上传组件: ...

  9. 反转数组、打印数组元素、加强(增强)for循环

    package com.guoba.array; public class Demo04 { public static void main(String[] args) { int[] arr = ...

  10. TypeScript核心基础

    前言 为了方便我们直接使用脚手架让他帮我们直接编译就完事了 创建一个 Vue 应用 前提条件 熟悉命令行 已安装 16.0 或更高版本的 Node.js npm init vue@latest 这一指 ...