服务提供者
compile group: 'org.apache.curator', name: 'curator-framework', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-recipes', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-client', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-x-discovery', version: '2.10.0'
private ServiceDiscovery<InstanceDetails> serviceDiscovery;
private final CuratorFramework client;
public ServiceRegistry(CuratorFramework client, String basePath) throws Exception {
this.client = client;
JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(
InstanceDetails.class);
serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).serializer(serializer)
.basePath(basePath).build();
serviceDiscovery.start();
}
public void registerService(ServiceInstance<InstanceDetails> serviceInstance) throws Exception {
serviceDiscovery.registerService(serviceInstance);
}
public void unregisterService(ServiceInstance<InstanceDetails> serviceInstance) throws Exception {
serviceDiscovery.unregisterService(serviceInstance);
}
public void updateService(ServiceInstance<InstanceDetails> serviceInstance) throws Exception {
serviceDiscovery.updateService(serviceInstance);
}
public void close() throws IOException {
serviceDiscovery.close();
}
}
public static void main(String[] args) throws Exception {
//zookeeper ip 地址,重试策略
CuratorFramework client = CuratorFrameworkFactory.newClient("10.113.90.160:2181", new ExponentialBackoffRetry(1000, 3));
client.start();
//定义根节点
ServiceRegistry serviceRegistrar = new ServiceRegistry(client,"services");
//获取相关的服务配置信息 InstanceDetails 服务对象(包含相关服务吗 端口号,ip属性)
ServiceInstance<InstanceDetails> instance1 = ServiceInstance.<InstanceDetails>builder()
.name("service1")
.port(12345)
.address("192.168.1.100") //address不写的话,会取本地ip
.payload(new InstanceDetails(UUID.randomUUID().toString(),"192.168.1.100",12345,"Test.Service1"))
.uriSpec(new UriSpec("{scheme}://{address}:{port}"))
.build();
ServiceInstance<InstanceDetails> instance2 = ServiceInstance.<InstanceDetails>builder()
.name("service2")
.port(12345)
.address("192.168.1.100")
.payload(new InstanceDetails(UUID.randomUUID().toString(),"192.168.1.100",12345,"Test.Service2"))
.uriSpec(new UriSpec("{scheme}://{address}:{port}"))
.build();
//注册服务
serviceRegistrar.registerService(instance1);
serviceRegistrar.registerService(instance2);
System.out.println("aaaaaaaa");
Thread.sleep(Integer.MAX_VALUE);
}
}
服务消费方
compile group: 'org.apache.curator', name: 'curator-framework', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-recipes', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-client', version: '2.10.0'
compile group: 'org.apache.curator', name: 'curator-x-discovery', version: '2.10.0'
private ServiceDiscovery<InstanceDetails> serviceDiscovery;
private Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap();
private List<Closeable> closeableList = Lists.newArrayList();
private Object lock = new Object();
public ServiceDiscoverer(CuratorFramework client, String basePath) throws Exception {
JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(
InstanceDetails.class);
serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(basePath)
.serializer(serializer).build();
serviceDiscovery.start();
}
public ServiceInstance<InstanceDetails> getInstanceByName(String serviceName) throws Exception {
ServiceProvider<InstanceDetails> provider = providers.get(serviceName);
if (provider == null) {
synchronized (lock) {
provider = providers.get(serviceName);
if (provider == null) {
provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName)
.providerStrategy(new RandomStrategy<InstanceDetails>()).build();
provider.start();
closeableList.add(provider);
providers.put(serviceName, provider);
}
}
}
return provider.getInstance();
}
public synchronized void close() {
for (Closeable closeable : closeableList) {
CloseableUtils.closeQuietly(closeable);
}
}
}
public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient("10.113.90.160:2181", new ExponentialBackoffRetry(1000, 3));
client.start();
ServiceDiscoverer serviceDiscoverer = new ServiceDiscoverer(client,"services");
ServiceInstance<InstanceDetails> instance1 = serviceDiscoverer.getInstanceByName("service1");
System.out.println(instance1.buildUriSpec());
System.out.println(instance1.getPayload());
ServiceInstance<InstanceDetails> instance2 = serviceDiscoverer.getInstanceByName("service1");
System.out.println(instance2.buildUriSpec());
System.out.println(instance2.getPayload());
serviceDiscoverer.close();
CloseableUtils.closeQuietly(client);
}
}
参考链接
[https://www.cnblogs.com/hupengcool/p/3976362.html][[https://blog.csdn.net/jrn1012/article/details/77009025]