consul是什么?

consul概念:

consul是用来做注册中心的 他和eureka是一样的 注册中心一般都是集群的形式存在
保证高可用 consul像是一个nosql 存储着键值对 可以做存储
consul是c/s架构的 其客户端负责和用户进行通讯 没有实际的意义 真正工作的是后端的服务端 存储数据也是在服务端存储

环境:

主机ip
consul-1 192.168.10.20
consul-2 192.168.10.30
consul-3 192.168.10.40
client客户端 192.168.10.10

在第一、二台consul主机和第四台client主机上安装jdk和maven环境
安装jdk

[root@localhost ~]# java -version
openjdk version "1.8.0_102"
OpenJDK Runtime Environment (build 1.8.0_102-b14)
OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
[root@localhost ~]# rm -rf /usr/bin/java #删除原本主机上的Java程序
[root@localhost ~]# java -version
-bash: /usr/bin/java: 没有那个文件或目录
[root@localhost ~]# tar -zxf jdk-8u201-linux-x64.tar.gz 
[root@localhost ~]# mv jdk1.8.0_201/ /usr/local/java

添加环境变量(通常情况我们使用第二种方法)

[root@localhost bin]# vim /etc/profile
#末尾添加
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
[root@localhost bin]# source /etc/profile
[root@localhost bin]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin:/usr/local/java/jre/bin
[root@localhost bin]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

安装maven

[root@localhost ~]# tar -zxf apache-maven-3.6.0-bin.tar.gz 
[root@localhost ~]# mv apache-maven-3.6.0 /usr/local/maven
[root@localhost ~]# vim /etc/profile
export PATH=$PATH:/usr/local/maven/bin
[root@localhost ~]# source /etc/profile
[root@localhost ~]# mvn -v
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/local/java/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-514.el7.x86_64", arch: "amd64", family: "unix"

安装consul服务(四台都要安)

[root@localhost ~]# unzip consul_1.4.0_linux_amd64.zip -d /usr/bin/
Archive:  consul_1.4.0_linux_amd64.zip
  inflating: /usr/bin/consul         
[root@localhost ~]# mkdir -p /opt/consul/{data,config,logs}

参数释义:
data:存放数据的目录
config:存放配置信息的目录
logs:存放日志

consul没有配置文件,所以直接通过启动命令来配置称为集群

consul-1

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# consul agent -server -node=consul1 -data-dir /opt/consul/data/ -config-dir /opt/consul/config/ -bind 192.168.10.20 -client 0.0.0.0 -enable-script-checks=true  -datacenter=kgc -bootstrap-expect=2

参数释义:
consul 命令
agent 启动一个代理
-server 以服务端启动
-node=consul1 当前节点在集群中名称
-data-dir /opt/consul/data/ 数据文件的目录
-config-dir /opt/consul/config/ 配置文件的目录
-bind 192.168.2.10 监听的ip
-client 0.0.0.0 指定的客户端 代表所有的客户端
-enable-script-checks=true 开启心跳连接
-datacenter=kgc 数据中心的名称
-bootstrap-expect=2 至少两个节点形成集群

consul-2

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# consul agent -server -ui -node=consul2 -data-dir /opt/consul/data/ -config-dir /opt/consul/config/ -bind 192.168.10.30 -client 0.0.0.0 -enable-script-checks=true -datacenter=kgc -join 192.168.10.20

参数释义:
-ui :图形化界面
-join:加入集群

consul-3

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# consul agent -server -ui -node=consul3 -data-dir /opt/consul/data/ -config-dir /opt/consul/config/ -bind 192.168.10.40 -client 0.0.0.0 -enable-script-checks=true -datacenter=kgc -join 192.168.10.20

client客户端

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# consul agent  -node=client -data-dir /opt/consul/data/ -config-dir /opt/consul/config/ -bind 192.168.10.10 -client 0.0.0.0 -enable-script-checks=true -datacenter=kgc -join 192.168.10.20

执行完后所有的主机都会阻塞终端,可以在第一台consul主机上另开一个终端去查看一下集群是否构建成功

[root@localhost ~]# consul members
Node     Address             Status  Type    Build  Protocol  DC   Segment
consul1  192.168.10.20:8301  alive   server  1.4.0  2         kgc  <all>
consul2  192.168.10.30:8301  alive   server  1.4.0  2         kgc  <all>
consul3  192.168.10.40:8301  alive   server  1.4.0  2         kgc  <all>
client   192.168.10.10:8301  alive   client  1.4.0  2         kgc  <default>

[root@localhost ~]# curl http://192.168.10.20:8500/v1/status/leader
"192.168.10.20:8300"
[root@localhost ~]# curl http://192.168.10.20:8500/v1/status/peers
["192.168.10.20:8300","192.168.10.30:8300","192.168.10.40:8300"]

[root@localhost ~]# consul info #查看集群的信息
agent:
	check_monitors = 0
	check_ttls = 0
	checks = 0
	services = 0
build:
	prerelease = 
	revision = 0bddfa23
	version = 1.4.0
consul:
	acl = disabled
	bootstrap = false
	known_datacenters = 1
	leader = true
	leader_addr = 192.168.10.20:8300
	server = true
……………………

[root@localhost ~]# curl http://192.168.10.20:8500/v1/catalog/nodes?pretty #查看所有的节点,并且转换为json格式
[
    {
        "ID": "e280d23d-58b1-72b4-1c93-5a240f0ee1cd",
        "Node": "client",
        "Address": "192.168.10.10",
        "Datacenter": "kgc",
        "TaggedAddresses": {
            "lan": "192.168.10.10",
            "wan": "192.168.10.10"
        },
        "Meta": {
            "consul-network-segment": ""
        },
        "CreateIndex": 23,
        "ModifyIndex": 24
    },
………………

访问第二台consul-2的ip和端口(因为第一台是没有图形化界面的,只有第二台和第三台有,所以这儿我们可以去访问验证查看一下)
在这里插入图片描述

consul的基础命令

nosql 存储数据 添加数据

1、创建或者更新数据

[root@localhost ~]# consul kv put redis/config/one 1 #添加数据到集群
Success! Data written to: redis/config/one
[root@localhost ~]# consul kv get redis/config/one  #查看
1

2、获取值

[root@localhost ~]# consul kv get -detailed redis/config/one
CreateIndex      379
Flags            0
Key              redis/config/one
LockIndex        0
ModifyIndex      379
Session          -
Value            1

在这里插入图片描述
3、查看详情

[root@localhost ~]# consul kv get -detailed redis/config/one
CreateIndex      379
Flags            0
Key              redis/config/one
LockIndex        0
ModifyIndex      379
Session          -
Value            1

4、删除

[root@localhost ~]# consul kv delete redis/config/one #删除
Success! Deleted key: redis/config/one
[root@localhost ~]# consul kv get redis/config/one
Error! No key exists at: redis/config/one

5、递归删除

[root@localhost ~]# consul kv put redis/config/one 1
Success! Data written to: redis/config/one
[root@localhost ~]# consul kv put redis/config/two 2
Success! Data written to: redis/config/two
[root@localhost ~]# consul kv delete -recurse redis  
Success! Deleted keys with prefix: redis
[root@localhost ~]# consul kv get redis/two
Error! No key exists at: redis/two

6、列出指定前缀的内容

[root@localhost ~]# consul kv put redis/config/one 1
Success! Data written to: redis/config/one
[root@localhost ~]# consul kv put redis/config/two 2
Success! Data written to: redis/config/two
[root@localhost ~]# consul kv get -keys redis/config/
redis/config/one
redis/config/two

7、列出所有的键

[root@localhost ~]# consul kv get -keys
redis/

consul作为注册中心的功能

在这里插入图片描述

consul-1、consul-2、client客户端
注:另开终端执行

root@localhost ~]# git clone https://github.com/luojunyong/spring-cloud-examples.git
正克隆到 'spring-cloud-examples'...
remote: Enumerating objects: 700, done.
remote: Total 700 (delta 0), reused 0 (delta 0), pack-reused 700
接收对象中: 100% (700/700), 66.07 KiB | 0 bytes/s, done.
处理 delta 中: 100% (216/216), done.

consul-1 生产者

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-consul/spring-cloud-consul-producer
[root@localhost spring-cloud-consul-producer]# vim src/main/resources/application.properties
 3 spring.cloud.consul.host=192.168.10.40 #这儿写的是第三台consul-3的ip(也就是没有安装maven的那台主机)
[root@localhost spring-cloud-consul-producer]# source /etc/profile
[root@localhost spring-cloud-consul-producer]# mvn spring-boot:run

consul-2 生产者

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-consul/spring-cloud-consul-producer-2/
[root@localhost spring-cloud-consul-producer-2]# vim src/main/resources/application.properties
3 spring.cloud.consul.host=192.168.10.40
[root@localhost spring-cloud-consul-producer-2]# source /etc/profile
[root@localhost spring-cloud-consul-producer-2]# mvn spring-boot:run

client 消费者

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-consul/spring-cloud-consul-consumer/
[root@localhost spring-cloud-consul-consumer]# vim src/main/resources/application.properties 
spring.cloud.consul.host=192.168.10.40
[root@localhost spring-cloud-consul-consumer]# source /etc/profile
[root@localhost spring-cloud-consul-consumer]# mvn spring-boot:run

第四台client另开一个终端去访问

[root@localhost ~]# curl 192.168.10.10:8503/call
helle consul
[root@localhost ~]# curl 192.168.10.10:8503/call
helle consul two 

再次去图形化界面查看验证
在这里插入图片描述

posted on 2021-01-24 12:24  1763392456  阅读(313)  评论(0编辑  收藏  举报

导航