consul 的入门指北针

记得前年 听某某技术大会,就已经有几家大公司在使用consul来替代 zookeeper
年轻的架构师喜欢尝试新的技术,比如架构师越年轻越推崇使用golang来做后端,
等九零后开始了,要用julia haskell rust做后端

consul 做服务发现 其实蛮不错的,自己功能还是很丰富的
首先 consul 学习曲线 还是挺曲折的,门槛不低,
给大家推荐 一些学习资料
http://consul.la/intro/getting-started/join
https://github.com/smarkm/consuldocs_zh/blob/a93ed5a2b75cbbde2fff4740891df8ae4b87148b/docs/agent/basics.md
http://www.liangxiansen.cn/2017/04/06/consul/
https://blog.csdn.net/scdxmoe/article/details/73866905
这些资料学会了 consul 基本就可以在测试生产中使用了

先下载 consul
https://releases.hashicorp.com/consul/1.2.3/consul_1.2.3_linux_amd64.zip
在 centos7上
使用root 用户

unzip consul_1.2.3_linux_amd64.zip  -d /usr/local
ln -s /usr/local/consul /usr/local/bin/consul
consul -version

consul 基本上就可以使用了
如果要搭建集群,要确保集群中的机器 防火墙是关闭的,至少 8300 8500 tcp udp 等都是好用的
另外最好是 ssh 免密码登陆的

单机模式
直接

consul   agent -dev

这个是开发模式 的,只是让你测试用的,
然后可以通过 web ui 8500端口查看
另外你也可以通过

consul members

Node   Address         Status  Type    Build  Protocol  DC    Segment
delpc  127.0.0.1:8301  alive   server  1.2.3  2         sz-1  <all>

之后我们尝试使用consul 的集群,限于自己当前只有两台电脑,我就搭建了一个server 一个client 的集群
node1 192.168.25.175
node2 192.168.25.104
以 node1为server node2 为client
首先在 node1上
建议加上 -client ,不然 ui web在其他机器访问不到,注意关闭防火墙

consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=agent-one -ui -bind=192.168.25.175 -client=0.0.0.0

这样 node1 上 server 就启动了

之后在 node2上

consul agent -data-dir /tmp/consu -node gf2 -ui -bind 192.168.25.104

这样 node2 上client 就启动了

之后node2 要投入到 node1的怀抱中,就是要join 到node1 的怀抱
然后在node2上执行

consul join 192.168.25.175

如果console 提示

Successful joined cluster by contacting 1 nodes

说明真的加入了,需要 提示一下,node2 必须先启动client 服务,不要直接就 join ,肯定是join 不上的

image.png

最简单的集群就ok了

另外如果 node1 server 挂掉了,node2 也就处于阻塞中,当node1 重启后,node2会主动加入进来,另外好像consul 支持 多个server 模式同存,这样有一个server挂掉了,另一个server 还可以继续提供服务

consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=idch9 -ui -bind=192.168.25.175

./consul agent -server -server-port=8501 -bootstrap-expect=1 -data-dir=/tmp/consul -node=consul-39 -ui -bind=10.10.16.39 -client=0.0.0.0

nohup ./consul agent -server -server-port=8501 -bootstrap-expect=1 -data-dir=/tmp/consul -node=consul-39 -ui -bind=10.10.16.39 -client=0.0.0.0 -log-file=./consul.log &

https://blog.csdn.net/achenyuan/article/details/80389410

https://releases.hashicorp.com/consul/1.2.3/consul_1.2.3_linux_amd64.zip

https://blog.csdn.net/zhengpeitao/article/details/54691563

https://blog.csdn.net/llianlianpay/article/details/79028916

server.port=8999

spring.application.name=ksk

spring.cloud.consul.host=localhost

spring.cloud.consul.port=8500

spring.cloud.consul.discovery.enabled=true

spring.cloud.consul.discovery.service-name=rms_tlin

spring.cloud.consul.discovery.health-check-interval=10s

spring.cloud.consul.discovery.register=true

spring.cloud.consul.discovery.health-check-path=/health

spring.cloud.consul.discovery.tags=rms

spring.http.encoding.charset=utf-8

spring.http.encoding.enabled=true

spring.cloud.consul.discovery.instance-id=rms_23

server:

port: 17004

application:

name: spring-boot-consul-client

spring:

http:

encoding:

  charset: utf-8

  enabled: true

cloud:

consul:

  host: 127.0.0.1

  port: 8500

  discovery:

    enabled: true

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

    health-check-interval: 10s

    register: true

    tags: foo=bar, baz

    health-check-path: /health

    health-check-path: /health

    health-check-path: /health

hostname: 127.0.0.1

server:

port: 17003

application:

name: spring-boot-consul-service

spring:

http:

encoding:

  charset: utf-8

  enabled: true

cloud:

consul:

  host: 127.0.0.1

  port: 8500

  discovery:

    enabled: true

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

    health-check-interval: 10s

    tags: foo=bar, baz

可以设置自定义健康监测接口

    health-check-path: /health

自定义脚本监测配置false

register: false

    register: true

<pre style="margin: 0.667rem 0px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(39, 40, 34); color: rgb(248, 248, 242); font-family: 文泉驿点阵正黑; font-size: 13.5pt;"><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,744评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,505评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,105评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,242评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,269评论 6 389
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,215评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,096评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,939评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,354评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,573评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,745评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,448评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,048评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,683评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,838评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,776评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,652评论 2 354

推荐阅读更多精彩内容