Spring Cloud注册中心之Consul
Consul简介
Consul是HashiCorp公司使用Golang语言开发的一中多服务解决方案工具,相比于其他服务注册中心来说,Consul的功能更为强大,丰富,其中最基本的功能包含下面几点(翻译自官网):
- 服务发现:Consul的客户端可以注册服务,如api或mysql,其他客户端可以使用Consul来发现给定服务的提供者。使用DNS或HTTP,应用程序可以很容易地找到它们所依赖的服务。
- 运行状况检查:Consul客户端可以提供任意数量的运行状况检查,这些检查要么与给定的服务相关(“web服务器是否返回200 OK”),要么与本地节点相关(“内存利用率是否低于90%”)。操作员可以使用此信息监视群集运行状况,服务发现组件也可以使用此信息将通信路由到远离不正常主机的位置。
- Kvstore:应用程序可以将Consul的分层密钥/值存储用于任何目的,包括动态配置、特征标记、协调、领导选择等。简单的HTTP API使其易于使用。
- 安全服务通信:Consul可以为服务生成和分发TLS证书,以建立相互的TLS连接。意图可用于定义允许哪些服务进行通信。使用可以实时更改的意图,而不是使用复杂的网络拓扑和静态防火墙规则,可以轻松地管理服务分段。
- 多数据中心:Consul支持多个现成的数据中心。这意味着Consul的用户不必担心构建额外的抽象层来扩展到多个区域。
Consul服务的提供者都需要运行一个consul agent,agent负责服务和节点的健康检测。agent与一个或多个Consul Server进行交互,Consul Server用于存放和复制数据,Consul服务节点数一般为奇数个(建议3,5,7),类似于zookeeper的节点设置方案,consul节点之间也会通过自己的协议选举出leader,Consul可以单节点运行,但作为数据服务中心为保证服务的高可用,一般都是集群部署。
Consul Server和Client启动都可以通过agent的方式启动,启动客户端时agent并不是必须的,Consul启动可以通过浏览器访问可视化界面管理服务。
具体文档官方介绍:https://www.consul.io/intro/index.html,
Consul安装
- 下载地址:https://www.consul.io/downloads.html
- Linux安装:
下载后的文件是一个zip文件,使用unzip命令解压后得到一个可执行文件,使用./consul命令执行后,看到如下界面表示安装完成,展示Consul相关 的命令和使用案例Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
login Login to Consul using an auth method
logout Destroy a Consul token created with login
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
- Windows安装:
下载Consul后解压是一个可执行文件,双击无法运行,需要把consul.exe配置到Windos的环境变量中,在cmd中使用consul的命令即可。
Consul Server
安装后,通过命令启动Consul Server
consul agent -server -bind=x.x.x.x -client=0.0.0.0 -bootstrap-expect=3 -data-dir=/consul/data -node=server1 -ui
- agent:表示使用agent启动
- -server:以服务端启动Consul
- -bind:绑定服务器节点的某个网卡,针对服务器多网卡环境
- -client:允许连接到server端的client IP地址,0.0.0.0表示任何地址都可以访问
- -bootstrap-expect:server节点数低于这个阈值,将无法正常工作
- -data-dir:consul server数据存放节点,此目录须存在
- -node:该节点在管理端的服务名称
- -ui:开启浏览器端web界面访问
服务启动后可以通过默认端口8500,在浏览器端访问,http://localhost:8500

使用consul members查看节点信息,当前只有一个节点
Node Address Status Type Build Protocol DC Segment
DESKTOP-4BMJIRJ 127.0.0.1:8301 alive server 1.7.2 2 dc1 <all>
在多个服务节点上启动服务端,每个服务端之间可以通过命令来管理Consul Server,加入或者离开集群
consul join ip
consul leave ip
Consul可以使用config-dir来配置服务启动信息,该文件夹下的所有.json结尾的文件都会被加载。
{
"ports": {
"http": 8500,
"dns": 8600,
"rpc": 8400,
"serf_lan": 8301,
"serf_wan": 8302,
"server": 8300
}
}
Consul Discovery
Spring Cloud Eureka发布声明将不再维护Eureka 2.X版本,但是Eureka 1.x仍然是一个活跃的项目
Eureka 2.0 (Discontinued)
The existing open source work on eureka 2.0 is discontinued. The code base and artifacts that were released as part of the existing repository of work on the 2.x branch is considered use at your own risk.
Eureka 1.x is a core part of Netflix's service discovery system and is still an active project.
技术总是再不停的迭代发展,既然Eureka不再维护,Spring Cloud Consul可以作为其替代品,而且功能更为强大,和Spring Cloud整合也是非常简单。
创建Spring cloud项目
pom文件引入spring cloud consul
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- consul discovery-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- consul config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
编写主启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulApplication { public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class,args);
} @Value("${server.port}")
private String serverPort; @RequestMapping("consul")
public String getInfo() {
return "Spring Cloud Consul :"+serverPort + UUID.randomUUID().toString();
} }
配置application.yml
spring:
application:
name: consul-client
cloud:
consul:
enabled: true
host: 127.0.0.1 #连接的服务端IP
port: 8500 #服务端口
discovery:
enabled: true #是否启用服务发现
register: true
deregister: true #服务停止时取消注册
health-check-path: /actuator/health #健康检查地址
health-heck-interval: 15s #健康检查间隔
health-check-critical-timeout: #清理不健康服务间隔时长
instance-id: consul-client1 #服务注册ID
service-name: consul-client #服务注册名称
tags: version 1.0,Chsoul Test Consul
config:
enabled: true #启用consul config
prefix: config
format: yaml server:
port: 8080
启动服务
访问http://localhost:8080/consul
Spring Cloud Consul :8080 ecc5da16-ad08-40a6-9d98-4f729dee70f3
每次刷新页面随之变化
访问http://localhost:8500

可以看到已经注册进来的服务信息,健康状态等。
Spring Cloud注册中心之Consul的更多相关文章
- Spring Cloud注册中心高可用搭建
Spring Cloud的注册中心可以由Eureka.Consul.Zookeeper.ETCD等来实现,这里推荐使用Spring Cloud Eureka来实现注册中心,它基于Netfilix的Eu ...
- Spring Cloud配置中心之Consul
Consul不仅可以作为Spring Cloud中服务的注册中心,也可以作为其配置中心,这样一个系统就可以实现服务发现和统一配置,减少系统维护的麻烦,其中在使用Consul作为配置中心使用的过程中可以 ...
- Spring Cloud 注册中心Eureka
一.简介 最近在看Spring Cloud微服务,接下来的时间和大家一起分享我所看到的,公司现在用的是dubbo ,之后有时间也去了解了解dubbo的源码.与dubbo相比较,Spring Cloud ...
- spring cloud 注册中心--eureka注册与发现
本文详细介绍spring cloud微服务的默认注册中心--eureka注册与发现.开发环境需要Windows系统.jdk和intellij idea.与zookeeper注册中心相比,eureka不 ...
- spring cloud - 注册中心
服务注册与发现 这里我们会用到Spring Cloud Netflix,该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用 ...
- kubernetes部署spring cloud注册中心 Eureka
系统环境 java JDK 1.8 Docker 18.09.6 kubernetes 1.16 创建Eureka Server 1.Maven引入相应的jar 引入 SpringBoot 做基础框架 ...
- Spring Cloud注册中心之Zookeeper
zookeeper可以作为分布式服务的注册中心 在服务端安装zookeeper 参考:https://www.cnblogs.com/conly/p/12267506.html 创建spring bo ...
- JAVA Spring Cloud 注册中心 Eureka 相关配置
转载至 https://www.cnblogs.com/fangfuhai/p/7070325.html Eureka客户端配置 1.RegistryFetchIntervalSecon ...
- Spring Cloud注册中心Eureka设置访问权限并自定义鉴权页面
原文:https://blog.csdn.net/a823007573/article/details/88971496 使用Spring Security实现鉴权 1. 导入Spring Secur ...
随机推荐
- laravel job 队列
1.数据库建表 php artisan queue:table<span> </span>//队列任务表 php artisan queue:failed-table<s ...
- Python函数递归调用
函数的递归调用: 是函数嵌套调用的一种特殊形式 具体是指: 在调用一个函数的过程中又直接或间接地调用到了本身 # 直接调用本身 def func(): print('我是func') func() f ...
- ffmpeg+Python实现B站MP4格式音频与视频的合并
目录 安装 官网下载 环境变量 验证 ffmpeg的使用 Python实现自动处理 文件结构 番剧缓存结构 常规缓存结构 文件信息 代码 具体代码 代码说明 安装 官网下载 http://ffmpeg ...
- 子父类存在同名成员时super的使用条件
1.子父类存在同名成员时,子类中默认访问子类的成员,可通过super指定访问父类的成员,格式:super.xx (注:xx是成员名): 2.创建子类对象时,默认会调用父类的无参构造方法,可通过sup ...
- Zookeeper(2)---节点属性、监听和权限
之前通过客户端连接之后我们已经知道了zk相关的很多命令(Zookeeper(1)---初识). 节点属性: 现在我们就通过stat指令来看看节点都有哪些属性,或者使用get 指令和-s参数来查看节点数 ...
- java数据结构-08队列
一.什么是队列 队列是一种特殊的线性表,只能在头尾两端进行操作,特点是先进先出:就像排队买票一样,先来的先买 二.接口设计 三.代码实现 可以使用动态数组.链表等实现:这里两种实现栈与双向链表 1. ...
- 统计学与R语言
书籍推荐 文末见下载链接. 主要有 :统计学习方法.R语言实战.统计学(贾俊平) 链接:百度网盘 提取码:提取码
- 一起学Vue:路由(vue-router)
前言 学习vue-router就要先了解路由是什么?前端路由的实现原理?vue-router如何使用?等等这些问题,就是本篇要探讨的主要问题. vue-router是什么 路由是什么? 大概有两种说法 ...
- NB-IoT的同步信号解析
NB-IoT的小区搜索和LTE的小区搜索是类似的,每个UE都是通过对同步信号的检测,来实现与小区时间和频率上的同步,以此来获取小区的ID.NB-IoT的同步信号包括NPSS和NSSS. NPSS用于完 ...
- Java学习的第四十天
1.例4.1在其他函数中调用主函数 package bgio; public class cjava { public static void main(String[] args) { prints ...