Spring Cloud Consul入门

1、启动consul

consul agent -dev
#consul agent -dev -config-dir=/etc/consul.d
#consul agent -data-dir=/tmp/consul -node=agent02 -bind=172.16.172.62 -config-dir=/etc/consul.d
#consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=agent01 -bind=172.16.172.63 -config-dir=/etc/consul.d
#-bootstrap-expect=1	     等待一个节点加入
#-data-dir=/tmp/consu	     缓存路径
#-bind=172.20.20.10	     绑定ip地址
#-config-dir=/etc/consul.d   配置文件路径

==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
           Version: 'v0.7.5'
           Node ID: 'f82c6b1c-43b5-4423-af1d-343429555bbe'
         Node name: 'hiup03'
        Datacenter: 'dc1'
            Server: true (bootstrap: false)
       Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
      Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
    Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
             Atlas: <disabled>

==> Log data will now stream in as it occurs:

    2017/02/27 10:50:13 [DEBUG] Using unique ID "f82c6b1c-43b5-4423-af1d-343429555bbe" from host as node ID
    2017/02/27 10:50:13 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]
    2017/02/27 10:50:13 [INFO] serf: EventMemberJoin: hiup03 127.0.0.1
    2017/02/27 10:50:13 [INFO] serf: EventMemberJoin: hiup03.dc1 127.0.0.1
    2017/02/27 10:50:13 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
    2017/02/27 10:50:13 [INFO] consul: Adding LAN server hiup03 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
    2017/02/27 10:50:13 [INFO] consul: Adding WAN server hiup03.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)

    2017/02/27 10:50:19 [WARN] raft: Heartbeat timeout from "" reached, starting election
    2017/02/27 10:50:19 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
    2017/02/27 10:50:19 [DEBUG] raft: Votes needed: 1
    2017/02/27 10:50:19 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1
    2017/02/27 10:50:19 [INFO] raft: Election won. Tally: 1
    2017/02/27 10:50:19 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
    2017/02/27 10:50:19 [INFO] consul: cluster leadership acquired
    2017/02/27 10:50:19 [DEBUG] consul: reset tombstone GC to index 3
    2017/02/27 10:50:19 [INFO] consul: New leader elected: hiup03
    2017/02/27 10:50:19 [INFO] consul: member 'hiup03' joined, marking health alive
    2017/02/27 10:50:19 [INFO] agent: Synced service 'consul'
    2017/02/27 10:50:19 [DEBUG] agent: Node info in sync
    2017/02/27 10:51:37 [INFO] agent.rpc: Accepted client: 127.0.0.1:39792
    2017/02/27 10:52:07 [DEBUG] agent: Service 'consul' in sync
    2017/02/27 10:52:07 [DEBUG] agent: Node info in sync
    2017/02/27 10:52:47 [DEBUG] http: Request GET /v1/catalog/nodes (218.436μs) from=127.0.0.1:33428

2、带管理界面的启动方式

consul agent -ui  -data-dir /tmp/consul-ui -bind=172.16.172.63
consul agent -ui -client 0.0.0.0 -data-dir=/tmp/consul -node=agent02 -bind=172.16.172.62 -config-dir=/etc/consul.d
#访问http://172.16.172.63:8500/ui/#/dc1/services

3、加入主节点consul

consul join 172.16.172.63
Successfully joined cluster by contacting 1 nodes

4、检查健康状况

curl http://172.16.172.63:8080/health
{"description":"Spring Cloud Consul Discovery Client","status":"UP"}

5、负载均衡相关

curl http://172.16.172.63:8080/choose
http://hiup03:8080

curl http://172.16.172.63:8080/instances
[{"serviceId":"testConsulApp","host":"hiup03","port":8080,"secure":false,"metadata":{},"uri":"http://hiup03:8080"}]

6、服务描述

curl http://172.16.172.63:8080/
{"serviceId":"testConsulApp","server":{"host":"hiup03","port":8080,"id":"hiup03:8080","zone":"UNKNOWN","readyToServe":true,"metaInfo":
{"serverGroup":null,"serviceIdForDiscovery":null,"instanceId":"testConsulApp-8080","appName":"testConsulApp"},"metadata":{},"healthService":{"node":
{"node":"agent01","address":"172.16.172.63"},"service":{"id":"testConsulApp-8080","service":"testConsulApp","tags":
[],"address":"hiup03","port":8080},"checks":[{"node":"agent01","checkId":"serfHealth","name":"Serf Health 
Status","status":"PASSING","notes":"","output":"Agent alive and reachable","serviceId":"","serviceName":""},
{"node":"agent01","checkId":"service:testConsulApp-8080","name":"Service 'testConsulApp' check","status":"PASSING","notes":"","output":"HTTP GET 
http://hiup03:8080/health: 200  Output: {\"description\":\"Spring Cloud Consul Discovery Client\",\"status\":\"UP\"}","serviceId":"testConsulApp-
8080","serviceName":"testConsulApp"}]},"passingChecks":true,"alive":true,"hostPort":"hiup03:8080"},"secure":false,"metadata":
{},"uri":"http://hiup03:8080","host":"hiup03","port":8080}

curl http://172.16.172.63:8080/me
{"serviceId":"testConsulApp-8080","host":"hiup03","port":8080,"secure":false,"metadata":{},"uri":"http://hiup03:8080"}

curl http://172.16.172.63:8080/rest
{"serviceId":"testConsulApp-8080","host":"hiup03","port":8080,"secure":false,"metadata":{},"uri":"http://hiup03:8080"}

curl http://172.16.172.63:8080/myenv
{"timestamp":1488183397275,"status":400,"error":"Bad 
Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required String parameter 'prop' is not present","path":"/myenv"}

curl http://172.16.172.63:8080/prop
default value

curl http://172.16.172.63:8080/feign
http://hiup03:8080

7、查看成员

consul members
Node     Address             Status  Type    Build  Protocol  DC
agent01  172.16.172.63:8301  alive   server  0.7.5  2         dc1
agent02  172.16.172.62:8301  alive   client  0.7.5  2         dc1

8、查看节点、域名及服务

curl http://localhost:8500/v1/catalog/nodes
curl http://localhost:8500/v1/catalog/service/hello
curl 'http://localhost:8500/v1/health/service/hello?passing'
curl http://localhost:8500/v1/health/state/critical
dig @localhost -p 8600 hiup03
dig @localhost -p 8600 hello.service.consul
dig @localhost -p 8600 hello.service.consul SRV
dig @localhost -p 8600 rails.hello.service.consul 
dig @localhost -p 8600 rails.hello.service.consul SRV
dig @127.0.0.1 -p 8600 hello.service.consul

dig @127.0.0.1 -p 8600 agent02.node.consul
; <<>> DiG 9.10.3-P4-Ubuntu <<>> @127.0.0.1 -p 8600 agent02.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61862
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;agent02.node.consul.           IN      A

;; ANSWER SECTION:
agent02.node.consul.    0       IN      A       172.16.172.62

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Mon Feb 27 11:44:25 CST 2017
;; MSG SIZE  rcvd: 53

9、重新加载服务

consul reload

10、键值数据库

consul kv put hiup/config/msg "read to go"
consul kv put -flags=15 hiup/config/msg_max_length 128

consul kv get hiup/config/msg
consul kv get -detailed  hiup/config/msg

consul kv get -recurse
consul kv delete hiup/config/msg
consul kv delete -recurse hiup

11、键值数据库并发处理

consul kv get -detailed  hiup/config/msg
CreateIndex      1094
Flags            0
Key              hiup/config/msg
LockIndex        0
ModifyIndex      1094
Session          -
Value            read to go

consul kv put -cas -modify-index=1093 hiup/config/msg "msg1093"
consul kv put -cas -modify-index=1094 hiup/config/msg "msg1094"

Comments are closed.