服务注册中心zookeeper方案
SpringCloud默认使用Eureka作为注册中心,但是Zookeeper也可以用作替代方案。Zookeeper是分布式协调工具,可以实现注册中心,采用临时节点,和生命周期进行关联,如果服务断开连接,该节点会自动被删除。
前置条件
zookeeper环境搭建
-
下载zookeeper
到apache官网下载zookeeper最新版本(apache-zookeeper-3.6.2),注意下载apache-zookeeper-3.6.2-bin压缩文件。
-
解压zookeeper
-
创建配置文件
cp ./conf/zoo_example.cfg ./conf/zoo.cfg
配置文件:
tickTime=2000 initLimit=10 syncLimit=5 dataDir=/Users/huzd/Applications/apache-zookeeper-3.6.2/data clientPort=2181
-
启动服务器端
在bin文件下执行如下命令启动zookeeper
./bin/zkServer.sh start
-
使用客户端连接
在bin文件夹下启动zookeeper客户端
./bin/zkCli.sh -server 127.0.0.1:2181
实战开发
zk服务提供支付模块
创建项目
创建名为 cloud-provider-payment-8004 的maven项目
修改配置
修改pom.xml文件
<dependencies>
<!-- * 其他的省略,重点提出zookeeper的依赖,
* 其余的依赖请参考cloud-provider-payment-8002
* 这里可能会有zookeeper版本冲突问题,如果遇到请在下面依赖中排除冲突的版本zookeeper
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
修改application.yml文件
删除eureka的配置,添加如下配置:
spring:
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
添加 /payment/zk 访问路径
@GetMapping("/zk")
@ResponseBody
public Object zk() {
return "This is from Payment Service of Register :"+port + ",Random String:"+ UUID.randomUUID();
}
启动程序
日志显示zookeeper已经连接并注册成功
2021-02-25 09:15:43.482 INFO 8220 --- [ restartedMain] org.apache.zookeeper.ZooKeeper : Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@50a4c4cc
2021-02-25 09:15:43.486 INFO 8220 --- [ restartedMain] org.apache.zookeeper.ClientCnxnSocket : jute.maxbuffer value is 4194304 Bytes
2021-02-25 09:15:43.489 INFO 8220 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2021-02-25 09:15:43.492 INFO 8220 --- [ restartedMain] o.a.c.f.imps.CuratorFrameworkImpl : Default schema
2021-02-25 09:15:43.497 INFO 8220 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established, initiating session, client: /127.0.0.1:59183, server: 127.0.0.1/127.0.0.1:2181
2021-02-25 09:15:43.502 INFO 8220 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100018816930004, negotiated timeout = 40000
2021-02-25 09:15:43.503 INFO 8220 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
查看zk注册信息
访问/payment/zk 得到如下界面:
查看zk注册信息
[zk: 127.0.0.1:2181(CONNECTED) 6] ls /
[services, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 7] ls /services
[cloud-provider-payment]
[zk: 127.0.0.1:2181(CONNECTED) 8] ls /services/cloud-provider-payment
[c2fe9f07-8864-4d9b-8bcd-1f7531344d6a]
[zk: 127.0.0.1:2181(CONNECTED) 10] get /services/cloud-provider-payment/c2fe9f07-8864-4d9b-8bcd-1f7531344d6a
{"name":"cloud-provider-payment","id":"c2fe9f07-8864-4d9b-8bcd-1f7531344d6a","address":"130.30.3.224","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1614215743754,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
停止payment8004服务查看zk
[zk: 127.0.0.1:2181(CONNECTED) 18] ls /services
[]
zookeeper使用临时节点存储注册信息;服务端停止,zookeeper会立刻删除该节点的注册信息!
当services下面无节点时,在一定时间间隔下services节点也会被删除。
[zk: 127.0.0.1:2181(CONNECTED) 23] ls / #重启8004服务节点
[services, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 24] ls /services
[cloud-provider-payment]
[zk: 127.0.0.1:2181(CONNECTED) 26] ls /services/cloud-provider-payment #zookeeper中又重新注册了该服务;并且使用了新的ID
[1f211200-7bfe-4fc8-aba8-f1731df2406c]
zk消费者订单模块
创建项目
创建名为 cloud-consumer-order-zk-80 的maven项目
修改配置
修改pom.xml文件
<dependencies>
<!-- * 其他的省略,重点提出zookeeper的依赖,
* 其余的依赖请参考cloud-consumer-order-80
* 这里可能会有zookeeper版本冲突问题,如果遇到请在下面依赖中排除冲突的版本zookeeper
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
</dependencies>
修改application.yml文件
server:
port: 80
spring:
application:
name: cloud-consumer-order-zk-80
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
编写调用程序
@Controller
@RequestMapping("/consumer")
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://CLOUD-PROVIDER-PAYMENT/";
@Resource
RestTemplate restTemplate;
@GetMapping("/payment/zk")
@ResponseBody
public String zk(){
System.out.println("请求地址:"+PAYMENT_URL+"payment/zk");
return restTemplate.getForObject(PAYMENT_URL+"/payment/zk",String.class);
}
}
启动程序
查看启动日志;说明消费者order-zk-80已经注册到zk中
2021-02-25 10:09:28.243 INFO 9220 --- [ restartedMain] o.a.c.f.imps.CuratorFrameworkImpl : Starting
2021-02-25 10:09:28.260 INFO 9220 --- [ restartedMain] org.apache.zookeeper.ZooKeeper : Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@7d8ef487
2021-02-25 10:09:28.269 INFO 9220 --- [ restartedMain] org.apache.zookeeper.ClientCnxnSocket : jute.maxbuffer value is 4194304 Bytes
2021-02-25 10:09:28.281 INFO 9220 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2021-02-25 10:09:28.287 INFO 9220 --- [ restartedMain] o.a.c.f.imps.CuratorFrameworkImpl : Default schema
2021-02-25 10:09:28.303 INFO 9220 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established, initiating session, client: /127.0.0.1:62328, server: 127.0.0.1/127.0.0.1:2181
2021-02-25 10:09:28.313 INFO 9220 --- [127.0.0.1:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x100018816930007, negotiated timeout = 40000
2021-02-25 10:09:28.320 INFO 9220 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
查看zk注册信息
[zk: 127.0.0.1:2181(CONNECTED) 27] ls /services
[cloud-consumer-order-zk-80, cloud-provider-payment]
[zk: 127.0.0.1:2181(CONNECTED) 28] ls /services/cloud-
cloud-consumer-order-zk-80 cloud-provider-payment
[zk: 127.0.0.1:2181(CONNECTED) 28] ls /services/cloud-consumer-order-zk-80
[6c3a86d2-f0fc-4dee-8a1c-dadd7e66de6e]
[zk: 127.0.0.1:2181(CONNECTED) 29] get /services/cloud-
cloud-consumer-order-zk-80 cloud-provider-payment
[zk: 127.0.0.1:2181(CONNECTED) 29] get /services/cloud-consumer-order-zk-80/6c3a86d2-f0fc-4dee-8a1c-dadd7e66de6e
{"name":"cloud-consumer-order-zk-80","id":"6c3a86d2-f0fc-4dee-8a1c-dadd7e66de6e","address":"130.30.3.224","port":80,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-consumer-order-zk-80","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1614218968763,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
[zk: 127.0.0.1:2181(CONNECTED) 30]
访问服务
总结
和Eureka区别是:Zookeeper不用建立注册中心模块,只需要注册自己的模块,就可以调用,因为ZookeeperClient是工具生成的;
使用不同注册中心,接口调用方式不变,无非变化配置文件和maven依赖信息
本文由 huzd 创作,采用 知识共享署名4.0 国际许可协议进行许可本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名最后编辑时间
为:
2021/02/25 10:40