Apache Curator 提供了Zookeeper注册中心的API,流程如下:
创建两个SpringBoot项目:
1.服务注册
2.服务发现
项目结构如下:
pom.xml
org.apache.curator
curator-framework
5.1.0
org.apache.curator
curator-recipes
5.1.0
org.apache.curator
curator-x-discovery
5.1.0
application.yml
server:
port: 8081
zookeeper:
curator:
ip: 192.168.126.156:2181
sessionTimeOut: 50000
sleepMsBetweenRetry: 1000
maxRetries: 3
namespace: terry
connectionTimeoutMs: 50000
创建ZookeeperConfig.java,作用是初始化curator客户端
package com.terry.config;
import lombok.Data;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "zookeeper.curator")
@Data
public class ZookeeperConfig {
/**
* 集群地址
*/
private String ip;
/**
* 连接超时时间
*/
private Integer connectionTimeoutMs;
/**
* 会话超时时间
*/
private Integer sessionTimeOut;
/**
* 重试机制时间参数
*/
private Integer sleepMsBetweenRetry;
/**
* 重试机制重试次数
*/
private Integer maxRetries;
/**
* 命名空间(父节点名称)
*/
private String namespace;
/**
* curator 客户端
* @return
* @throws Exception
*/
@Bean("curatorClient")
public CuratorFramework curatorClient() throws Exception {
// 开启客户端,并连接
CuratorFramework client = CuratorFrameworkFactory.builder()
//连接地址 集群用,隔开
.connectString(ip)
.connectionTimeoutMs(connectionTimeoutMs)
//会话超时时间
.sessionTimeoutMs(sessionTimeOut)
//设置重试机制
.retryPolicy(new ExponentialBackoffRetry(sleepMsBetweenRetry, maxRetries))
//设置命名空间 在操作节点的时候,会以这个为父节点
.namespace(namespace)
.build();
client.start();
return client;
}
}
创建ApplicationRunnerImpl 作用是Springboot 启动成功后 监听服务注册
package com.terry.config;
import lombok.extern.java.Log;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
import org.apache.curator.x.discovery.ServiceInstance;
import org.omg.CORBA.ServiceDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.util.Collection;
/**
* 服务发现 (Springboot 启动成功后 监听服务注册)
* @author terry
* @version 1.0
* @date 2022/5/17 12:43
*/
@Component
@Log
public class ApplicationRunnerImpl implements ApplicationRunner {
@Autowired
private CuratorFramework client;
@Override
public void run(ApplicationArguments args) throws Exception {
ServiceDiscovery<ServiceDetail> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServiceDetail.class)
.client(client)
.basePath("terry")
.build();
serviceDiscovery.start();
// 每隔三秒获取服务列表
while (true) {
Collection<ServiceInstance<ServiceDetail>> services = serviceDiscovery.queryForInstances("user");
for(ServiceInstance<ServiceDetail> service : services) {
log.info("监听服务注册:" + service.getAddress() + ":" + service.getPort());
}
Thread.sleep(3000);
}
}
}
application.yml
zookeeper:
curator:
ip: 192.168.126.156:2181
sessionTimeOut: 50000
sleepMsBetweenRetry: 1000
maxRetries: 3
namespace: terry
connectionTimeoutMs: 50000
创建ZookeeperConfig.java,作用是初始化curator客户端
package com.terry.config;
import lombok.Data;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "zookeeper.curator")
@Data
public class ZookeeperConfig {
/**
* 集群地址
*/
private String ip;
/**
* 连接超时时间
*/
private Integer connectionTimeoutMs;
/**
* 会话超时时间
*/
private Integer sessionTimeOut;
/**
* 重试机制时间参数
*/
private Integer sleepMsBetweenRetry;
/**
* 重试机制重试次数
*/
private Integer maxRetries;
/**
* 命名空间(父节点名称)
*/
private String namespace;
/**
* curator 客户端
* @return
* @throws Exception
*/
@Bean("curatorClient")
public CuratorFramework curatorClient() throws Exception {
// 开启客户端,并连接
CuratorFramework client = CuratorFrameworkFactory.builder()
//连接地址 集群用,隔开
.connectString(ip)
.connectionTimeoutMs(connectionTimeoutMs)
//会话超时时间
.sessionTimeoutMs(sessionTimeOut)
//设置重试机制
.retryPolicy(new ExponentialBackoffRetry(sleepMsBetweenRetry, maxRetries))
//设置命名空间 在操作节点的时候,会以这个为父节点
.namespace(namespace)
.build();
client.start();
return client;
}
}
创建ApplicationRunnerImpl 作用是Springboot 启动成功后注册到注册中心
package com.terry.config;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
import org.apache.curator.x.discovery.ServiceInstance;
import org.omg.CORBA.ServiceDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
/**
* 服务注册(Springboot 启动成功后注册到注册中心)
* @author terry
* @version 1.0
* @date 2022/5/17 12:43
*/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
@Autowired
private CuratorFramework client;
@Override
public void run(ApplicationArguments args) throws Exception {
InetAddress address = InetAddress.getLocalHost();
ServiceInstance<ServiceDetail> instance = ServiceInstance.<ServiceDetail>builder()
.address(address.getHostAddress())//ip地址
.port(8080)
.name("user") //服务的名称
.build();
ServiceDiscovery<ServiceDetail> serviceDiscovery = ServiceDiscoveryBuilder.builder(ServiceDetail.class)
.client(client)
.basePath("terry")// 注册中心根路径
.build();
//服务注册
serviceDiscovery.registerService(instance);
serviceDiscovery.start();
}
}
成功启动两个项目,服务发现打印输出:
2022-05-17 13:35:51.883 INFO 11752 --- [ main] com.terry.config.ApplicationRunnerImpl : 监听服务注册:192.168.126.1:8080
2022-05-17 13:35:54.898 INFO 11752 --- [ main] com.terry.config.ApplicationRunnerImpl : 监听服务注册:192.168.126.1:8080
2022-05-17 13:35:57.910 INFO 11752 --- [ main] com.terry.config.ApplicationRunnerImpl : 监听服务注册:192.168.126.1:8080
2022-05-17 13:36:00.922 INFO 11752 --- [ main] com.terry.config.ApplicationRunnerImpl : 监听服务注册:192.168.126.1:8080
2022-05-17 13:36:03.938 INFO 11752 --- [ main] com.terry.config.ApplicationRunnerImpl : 监听服务注册:192.168.126.1:8080
当停止注册服务后,服务发现将不再打印。