我们知道Eureka官方已经停更,现在技术选型时基本不会再用Eureka,我们也可以使用Zookeeper代替Eureka来实现注册中心功能。接下来我们来来看看实现替代的过程。
一、Zookeeper介绍
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,可以实现注册中心功能。
二、Zookeeper环境准备
我们需要准备安装好Zookeeper的虚拟机(我们这里演示使用单机版,注意要开启2181端口),具体参考:[neilian ids=2727]

程序员导航
优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站
三、实现注册中心功能
[v_warn]注意:在此演示我们忽略掉数据库的增删改查,因此,相关依赖和service、dao、mapper就不写了,如果你需要可以参考之前的eureka部分[/v_warn]
第1步:实现服务提供者
1)创建Module
我们在mscloud下新建名为cloud-provider-payment8004的Module
2)修改pom
我们修改8004的pom.xml如下(参考payment8001的,去掉数据库依赖,将eureka依赖改为zookeeper依赖):
mscloud com.panziye.springcloud 1.0-SNAPSHOT 4.0.0 cloud-provider-payment8004 org.springframework.cloud spring-cloud-starter-zookeeper-discovery com.panziye.springcloud cloud-api-commons ${project.version} org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
3)写yml
在该模块的resources新建application.yml,参考payment8001配置(剔除数据库配置),如下:
server:
port: 8004
spring:
application:
name: cloud-payment-service
cloud:
zookeeper:
# zookeeper服务器地址
connect-string: 192.168.61.128:2181
4)主启动类
在com.panziye.springcloud包下新建PaymentMain8004主启动类:

AI 工具导航
优网导航旗下AI工具导航,精选全球千款优质 AI 工具集
package com.panziye.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
5)业务类
这里我们业务类只创建controller演示即可,在com.panziye.springcloud.controller包中新建PaymentController:
package com.panziye.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/zk")
public String zk(){
return "payment zookeeper server port:"+serverPort+",随机数:"+ Math.random();
}
}
6)测试
1)我们先启动Linux上的Zookeeper
2)我们再启动payment8004,如果发现如下报错,说明没有开启2181端口,去开启即可。

3)浏览器访问http://localhost:8004/payment/zk:

4)查看zookeeper
a)执行bin目录中如下指令进入zk客户端:
zkCli.sh
b)执行如下指令查看节点,发现成功注册:
ls / ls /services /services/cloud-payment-service
发现除了zookeeper自身节点还多了个services节点,里面有cloud-payment-service,再里面有一个序列号,如下:

c)查看序列号
get /services/cloud-payment-service/929917f7-c1be-4c9c-abee-7f77ef8d77ce
{
"name": "cloud-payment-service",
"id": "929917f7-c1be-4c9c-abee-7f77ef8d77ce",
"address": "XTZJ-2021DICDMM",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-payment-service",
"metadata": {
"instance_status": "UP"
}
},
"registrationTimeUTC": 1616640042560,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
[v_warn]注意:zookeeper中的节点是临时节点,不是持久节点,因为如果你关闭8004服务,zookeeper会很快移除该节点信息[/v_warn]

免费在线工具导航
优网导航旗下整合全网优质免费、免注册的在线工具导航大全
第2步:实现服务消费者
[v_blue]参考cloud-consumer-order80[/v_blue]
1)建Module
在mscloud下新建cloud-consumer-zk-order80模块
2)改pom
参考cloud-consumer-order80的将eureka依赖改为zookeeper依赖
mscloud com.panziye.springcloud 1.0-SNAPSHOT 4.0.0 cloud-consumer-zk-order80 org.springframework.cloud spring-cloud-starter-zookeeper-discovery com.panziye.springcloud cloud-api-commons ${project.version} org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
3)写yml
server:
port: 80
spring:
application:
name: cloud-order-zk-service
cloud:
zookeeper:
# zookeeper服务器地址
connect-string: 192.168.61.128:2181
4)主启动类
在com.panziye.springcloud包中新建主启动类OrderZkMain80:
package com.panziye.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZkMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZkMain80.class,args);
}
}
5)业务类
a)在com.panziye.springcloud包中新建cofnig包,在config中新建ApplicationContextCofig如下:
package com.panziye.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextCofig {
/**
* 配置RestTemplate
* @return
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
b)在com.panziye.springcloud包中新建controller包,在controller中新建OrderController:
package com.panziye.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderController {
// 注册中心中服务地址-zookeeper中service名称
public static final String PAYMENT_URL = "http://cloud-payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/zk")
public String zk(){
return restTemplate.getForObject(PAYMENT_URL+"/payment/zk",String.class);
}
}
6)测试
a)启动payment8004和zk-order-80,查看zookeeper发现都注册进来了:

b)访问http://localhost/consumer/payment/zk





