| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package discovery; |
| 21 |
|
| 22 |
import java.io.Closeable; |
| 23 |
import java.io.IOException; |
| 24 |
import org.apache.curator.framework.CuratorFramework; |
| 25 |
import org.apache.curator.utils.CloseableUtils; |
| 26 |
import org.apache.curator.x.discovery.ServiceDiscovery; |
| 27 |
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; |
| 28 |
import org.apache.curator.x.discovery.ServiceInstance; |
| 29 |
import org.apache.curator.x.discovery.UriSpec; |
| 30 |
import org.apache.curator.x.discovery.details.JsonInstanceSerializer; |
| 31 |
|
| 32 |
/** |
| 33 |
* This shows a very simplified method of registering an instance with the service discovery. Each individual |
| 34 |
* instance in your distributed set of applications would create an instance of something similar to ExampleServer, |
| 35 |
* start it when the application comes up and close it when the application shuts down. |
| 36 |
*/ |
| 37 |
public class ExampleServer implements Closeable { |
| 38 |
private final ServiceDiscovery<InstanceDetails> serviceDiscovery; |
| 39 |
private final ServiceInstance<InstanceDetails> thisInstance; |
| 40 |
|
| 41 |
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) |
| 42 |
throws Exception { |
| 43 |
// in a real application, you'd have a convention of some kind for the URI layout |
| 44 |
UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); |
| 45 |
|
| 46 |
thisInstance = ServiceInstance.<InstanceDetails>builder() |
| 47 |
.name(serviceName) |
| 48 |
.payload(new InstanceDetails(description)) |
| 49 |
.port((int) (65535 * Math.random())) // in a real application, you'd use a common port |
| 50 |
.uriSpec(uriSpec) |
| 51 |
.build(); |
| 52 |
|
| 53 |
// if you mark your payload class with @JsonRootName the provided JsonInstanceSerializer will work |
| 54 |
JsonInstanceSerializer<InstanceDetails> serializer = |
| 55 |
new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); |
| 56 |
|
| 57 |
serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) |
| 58 |
.client(client) |
| 59 |
.basePath(path) |
| 60 |
.serializer(serializer) |
| 61 |
.thisInstance(thisInstance) |
| 62 |
.build(); |
| 63 |
} |
| 64 |
|
| 65 |
public ServiceInstance<InstanceDetails> getThisInstance() { |
| 66 |
return thisInstance; |
| 67 |
} |
| 68 |
|
| 69 |
public void start() throws Exception { |
| 70 |
serviceDiscovery.start(); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void close() throws IOException { |
| 75 |
CloseableUtils.closeQuietly(serviceDiscovery); |
| 76 |
} |
| 77 |
} |