微服务之consul(一) - 诗码者 - 博客园

首先去官网现在合适的consul包:https://www.consul.io/downloads.html

安装直接下载zip包,解压后只有一个可执行的文件consul,将consul添加到系统的环境变量里面。

#unzip consul_1.2.3_linux_amd64.zip

#cp -a consul  /usr/bin

#consul

复制代码

Usage: consul [–version] [–help] []

Available commands are:

agent Runs a Consul agent

catalog Interact with the catalog

connect Interact with Consul Connect

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

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

snapshot Saves, restores and inspects snapshots of Consul server state

validate Validate config files/directories

version Prints the Consul version

watch Watch for changes in Consul

复制代码

输入consul,出现上面的内容证明安装成功。

2)启动

consul必须启动agent才能使用,有两种启动模式server和client,还有一个官方自带的ui。server用与持久化服务信息,集群官方建议3或5个节点。client只用与于server交互。ui可以查看集群情况的。

server:

cn1:

#consul agent  -bootstrap-expect 2  -server   -data-dir /data/consul0 -node=cn1 -bind=192.168.1.202 -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1

cn2:

#consul agent    -server  -data-dir /data/consul0 -node=cn2 -bind=192.168.1.201 -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1  -join 192.168.1.202

cn3:

#consul agent  -server  -data-dir /data/consul0 -node=cn3 -bind=192.168.1.200 -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1  -join 192.168.1.202

参数解释:

-bootstrap-expect:集群期望的节点数,只有节点数量达到这个值才会选举leader。

-server: 运行在server模式

-data-dir:指定数据目录,其他的节点对于这个目录必须有读的权限

-node:指定节点的名称

-bind:为该节点绑定一个地址

-config-dir:指定配置文件,定义服务的,默认所有一.json结尾的文件都会读

-enable-script-checks=true:设置检查服务为可用

-datacenter: 数据中心没名称,

-join:加入到已有的集群中

client:

#consul agent   -data-dir /data/consul0 -node=cn4 -bind=192.168.1.199 -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1  -join 192.168.1.202

client节点可以有多个,自己根据服务指定即可。

ui:

#consul agent  -ui  -data-dir /data/consul0 -node=cn4 -bind=192.168.1.198  -client 192.168.1.198   -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1  -join 192.168.1.202

-ui:使用自带的ui,

-ui-dir:指定ui的目录,使用自己定义的ui

-client:指定web  ui、的监听地址,默认127.0.0.1只能本机访问。

集群创建完成后:

使用一些常用的命令检查集群的状态:

#consul  info

可以在raft:stat看到此节点的状态是Fllower或者leader

#consul members

Node Address Status Type Build Protocol DC Segment

cn1 192.168.1.202:8301 alive server 1.0.2 2 dc1

cn2 192.168.1.201:8301 alive server 1.0.2 2 dc1

cn3 192.168.1.200:8301 alive client 1.0.2 2 dc1

新加入一个节点有几种方式;

1、这种方式,重启后不会自动加入集群

#consul  join  192.168.1.202

2、#在启动的时候使用-join指定一个集群

#consul agent  -ui  -data-dir /data/consul0 -node=cn4 -bind=192.168.1.198 -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1  -join 192.168.1.202

3、使用-startjoin或-rejoin

#consul agent  -ui  -data-dir /data/consul0 -node=cn4 -bind=192.168.1.198 -config-dir /etc/consul.d -enable-script-checks=true  -datacenter=dc1  -rejoin

访问ui:

http://192.168.1.198:8500/ui

端口:

8300:consul agent服务relplaction、rpc(client-server)

8301:lan gossip

8302:wan gossip

8500:http api端口

8600:DNS服务端口

3)服务注册

采用的是配置文件的方式,(官方推荐)首先创建一个目录用于存放定义服务的配置文件

#mkdir /etc/consul.d/

启动服务的时候要使用-config-dir 参数指定。

下面给出一个服务定义:

#cat web.json

复制代码

{

“service”:{

“name”:“web”,

“tags”:[

“rails”

],

“port”:80,

“check”:{

“name”:“ping”,

“script”:“curl -s localhost:80”,

“interval”:“3s”

}

}

}

复制代码

如果这样启动consul后,会发现consul的日志里面一直报错,因为我们没有启动80端口的服务,下面给出我写的一个go程序:

#cat  web.go

复制代码

package main

import (

“io”

“log”

“net/http”

“strconv”

“fmt”

)

var iCnt int = 0;

func helloHandler(w http.ResponseWriter, r*http.request) {

iCnt++;

str :=“Hell eorld ! friend(”+ strconv.Itoa(iCnt)+“)”

io.WriteString(w,str)

fmt.Println(str)

}

func main(){

ht :=http.HanderFunc(helloHandler)

if ht != nil {

http.Handle(“/hello”,ht)

}

err := http.ListenAndServe(“:80”,nil)

if err != nil{

log.Fatal(“ListenAndserve:”,err.Error())

}

}

复制代码

#需要一个goalong的环境:

#go  build -o  web  web.go

#./web

此时就可以在没有运行web服务的机器上面执行DNS查询:

# dig @127.0.0.1 -p 8600 web.service.consul SRV

;; ANSWER SECTION:

web.service.consul. 0 IN SRV 1 1 80 cn2.node.dc1.consul.

web.service.consul. 0 IN SRV 1 1 80 cn3.node.dc1.consul.

;; ADDITIONAL SECTION:

cn2.node.dc1.consul. 0 IN A 192.168.1.201

cn2.node.dc1.consul. 0 IN TXT “consul-network-segment=”

cn3.node.dc1.consul. 0 IN A 192.168.1.200

cn3.node.dc1.consul. 0 IN TXT “consul-network-segment=”

;; Query time: 17 msec

;; SERVER: 127.0.0.1#8600(127.0.0.1)

;; WHEN: 四 1月 04 14:39:32 CST 2018

;; MSG SIZE rcvd: 229

可以看到服务已经注册到集群里面了。

使用dns查询,默认域名格式NAME.service.consul,NAME就是web.json里面定义的service的name。可以自己指定域和端口:-domain、-dns-port 53

为了方便使用consul集群的注册使用,所以写了一个三节点client的注册脚本,方便统一注册服务和管理。还利用到了nfs,将服务文件共享到集群。

复制代码

#!/usr/bin/env python

#encoding: utf8

#decription: registered a service to consul

from subprocess import call

import sys

hosts = {“b1”:“10.10.1.01:8500”,“b2”:“10.10.7.1:8500”,“b3”:“10.10.8.21:8500”}

def consul_relaod():

if (len(sys.argv) != 2) or sys.argv[1] == ‘-h’:

print((“Usage: {0} [option] :{1} ,if you wang update all of them,you must use ‘all’”).format(sys.argv[0],hosts.keys()))

sys.exit()

elif(sys.argv[1] == ‘all’):

for i in hosts.keys():

call((“consul reload -http-addr {}”).format(hosts[i]),shell=True)

else:

call((“consul reload -http-addr {}”).format(hosts[sys.argv[1]]),shell=True)

if name == ‘main’:

consul_relaod()

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

最后:

总结来说,面试成功=基础知识+项目经验+表达技巧+运气。我们无法控制运气,但是我们可以在别的地方花更多时间,每个环节都提前做好准备。

面试一方面是为了找到工作,升职加薪,另一方面也是对于自我能力的考察。能够面试成功不仅仅是来自面试前的临时抱佛脚,更重要的是在平时学习和工作中不断积累和坚持,把每个知识点、每一次项目开发、每次遇到的难点知识,做好积累,实践和总结。

点击这里领取Web前端开发经典面试题

图片转存中…(img-KF8Xn3Le-1713805032315)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

[外链图片转存中…(img-FPwzD7j8-1713805032315)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

[外链图片转存中…(img-SA8b4VGN-1713805032316)]

最后:

总结来说,面试成功=基础知识+项目经验+表达技巧+运气。我们无法控制运气,但是我们可以在别的地方花更多时间,每个环节都提前做好准备。

面试一方面是为了找到工作,升职加薪,另一方面也是对于自我能力的考察。能够面试成功不仅仅是来自面试前的临时抱佛脚,更重要的是在平时学习和工作中不断积累和坚持,把每个知识点、每一次项目开发、每次遇到的难点知识,做好积累,实践和总结。

点击这里领取Web前端开发经典面试题

  • 16
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值