https://www.consul.io/
安装
https://learn.hashicorp.com/consul/getting-started/install.html
1 2 3 % consul version Consul v1.6.3 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
官方教程
https://learn.hashicorp.com/consul/getting-started/agent
启动服务器
查看节点
尝试
1 2 3 % consul members Node Address Status Type Build Protocol DC Segment Ss-MacBook-Pro.local 127.0.0.1:8301 alive server 1.6.3 2 dc1 <all>
1 2 3 coconutnut@Ss-MacBook-Pro ~ % consul members -detailed Node Address Status Tags Ss-MacBook-Pro.local 127.0.0.1:8301 alive acls=0,build=1.6.3:7f3b5f3+,dc=dc1,id=d9c5a17b-cc6b-1ca6-7c72-07304fbcccdc,port=8300,raft_vsn=3,role=consul,segment=<all>,vsn=2,vsn_max=3,vsn_min=2,wan_join_port=8302
但是member命令获取的信息只是eventually consistent的
一致性更强的方式是使用http api
1 curl localhost:8500/v1/catalog/nodes
尝试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 % curl localhost:8500/v1/catalog/nodes [ { "ID": "d9c5a17b-cc6b-1ca6-7c72-07304fbcccdc", "Node": "Ss-MacBook-Pro.local", "Address": "127.0.0.1", "Datacenter": "dc1", "TaggedAddresses": { "lan": "127.0.0.1", "wan": "127.0.0.1" }, "Meta": { "consul-network-segment": "" }, "CreateIndex": 10, "ModifyIndex": 11 } ]
也可以用dns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 % dig @127.0.0.1 -p 8600 Judiths-MBP.node.consul ; <<>> DiG 9.10.6 <<>> @127.0.0.1 -p 8600 Judiths-MBP.node.consul ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 33684 ;; flags: qr aa rd; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;Judiths-MBP.node.consul. IN A ;; AUTHORITY SECTION: consul. 0 IN SOA ns.consul. hostmaster.consul. 1587353730 3600 600 86400 0 ;; Query time: 0 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Mon Apr 20 11:35:30 CST 2020 ;; MSG SIZE rcvd: 102
优雅的关闭节点 ctrl+C 暂停服务器
注册到consul
https://juejin.im/post/5db05582f265da4d4c20180f
将以前注册到eureka的服务改到consul
user-service模块
修改依赖
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-consul-discovery</artifactId > </dependency >
修改配置
1 2 3 4 5 6 7 8 9 10 11 12 13 cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
ribbon-service模块也是
Service Checks是X,但是可以访问?
但是http://localhost:8301/user/1获取不了
报错信息
1 java.lang.IllegalStateException: No instances available for user-service
https://blog.csdn.net/kxj19980524/article/details/86935415
还需要加一个监控中心的依赖
1 2 3 4 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
重启
访问http://localhost:8301/user/1四次
控制台输出
交替调用
成功
总结 终端启动consul服务器
Idea模块 consul客户端
依赖
1 2 3 4 5 6 7 8 9 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
配置
1 2 3 4 5 6 cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}