Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本),2021年不想被公司优化

Consul是HashiCorp公司推出的开源软件,提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。

Spring Cloud Consul 具有如下特性:

  • 支持服务治理:Consul作为注册中心时,微服务中的应用可以向Consul注册自己,并且可以从Consul获取其他应用信息;

  • 支持客户端负责均衡:包括Ribbon和Spring Cloud LoadBalancer;

  • 支持Zuul:当Zuul作为网关时,可以从Consul中注册和发现应用;

  • 支持分布式配置管理:Consul作为配置中心时,使用键值对来存储配置信息;

  • 支持控制总线:可以在整个微服务系统中通过 Control Bus 分发事件消息。

使用Consul作为注册中心


安装并运行Consul

首先我们从官网下载Consul,地址:https://www.consul.io/downloads.html

Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本),2021年不想被公司优化

下载完成后只有一个exe文件,双击运行;

在命令行中输入以下命令可以查看版本号:

consul --version

查看版本号信息如下:

Consul v1.6.2

Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

使用开发模式启动:

consul agent -dev

通过以下地址可以访问Consul的首页:http://localhost:8500

Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本),2021年不想被公司优化

创建应用注册到Consul

我们通过改造user-service和ribbon-service来演示下服务注册与发现的功能,主要是将应用原来的Eureka注册中心支持改为Consul注册中心支持。

创建consul-user-service模块和consul-ribbon-service模块;

修改相关依赖,把原来的Eureka注册发现的依赖改为Consul的,并添加SpringBoot Actuator的依赖:

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-consul-discovery

修改配置文件application.yml,将Eureka的注册发现配置改为Consul的:

server:

port: 8206

spring:

application:

name: consul-user-service

cloud:

consul:

将服务注册到consul

host: localhost

port: 8500

discovery:

service-name: ${spring.application.name}

运行两个consul-user-service和一个consul-ribbon-service,在Consul页面上可以看到如下信息:

Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本),2021年不想被公司优化

负载均衡功能

由于我们运行了两个consul-user-service,而consul-ribbon-service默认会去调用它的接口,我们调用consul-ribbon-service的接口来演示下负载均衡功能。

多次调用接口:http://localhost:8308/user/1 ,可以发现两个consul-user-service的控制台交替打印如下信息。

2019-12-29 18:20:47.834 INFO 10460 — [io-8207-exec-10] c.j.s.controller.UserController : 根据id获取用户信息,用户名称为:jourwon

使用Consul作为配置中心


我们通过创建consul-config-client模块,并在Consul中添加配置信息来演示下配置管理的功能。

创建consul-config-client模块

在pom.xml中添加相关依赖:

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-consul-config

org.springframework.cloud

spring-cloud-starter-consul-discovery

org.springframework.boot

spring-boot-starter-actuator

添加配置文件application.yml,启用的是dev环境的配置:

spring:

profiles:

active: dev

添加配置文件bootstrap.yml,主要是对Consul的配置功能进行配置:

server:

port: 9101

spring:

application:

name: consul-config-client

cloud:

consul:

host: localhost

port: 8500

discovery:

service-name: consul-config-client

config:

是否启用配置中心功能

enabled: true

设置配置值的格式

format: yaml

设置配置所在目录

prefix: config

设置配置的分隔符

profile-separator: ‘:’

配置key的名字,由于Consul是K/V存储,配置存储在对应K的V中

data-key: data

创建ConfigClientController,从Consul配置中心中获取配置信息:

@RefreshScope

@RestC

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ontroller

public class ConfigClientController {

@Value("${config.info}")

private String configInfo;

@GetMapping("/configInfo")

public String getConfigInfo() {

return configInfo;

}

}

在Consul中添加配置

在consul中添加配置存储的key为:

config/consul-config-client:dev/data

在consul中添加配置存储的value为:

上一篇:在使用Zookeeper以及Consul的时候,@LoadBalanced注解无效,无法通过服务名获取注册中心的URL的问题记录


下一篇:consul基础