Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/appform-io/dropwizard-service-discovery
Zookeeper service discovery bundle and client for dropwizard.
https://github.com/appform-io/dropwizard-service-discovery
Last synced: 2 months ago
JSON representation
Zookeeper service discovery bundle and client for dropwizard.
- Host: GitHub
- URL: https://github.com/appform-io/dropwizard-service-discovery
- Owner: appform-io
- License: apache-2.0
- Created: 2016-03-13T14:26:14.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-25T13:17:22.000Z (9 months ago)
- Last Synced: 2024-05-19T05:40:13.241Z (8 months ago)
- Language: Java
- Size: 1010 KB
- Stars: 16
- Watchers: 5
- Forks: 29
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-dropwizard - dropwizard-service-discovery - Zookeeper service discovery bundle and client for dropwizard. (Open Source / Data Stores)
README
# Dropwizard Service Discovery [![Build](https://github.com/appform-io/dropwizard-service-discovery/actions/workflows/merge-update.yml/badge.svg)](https://github.com/appform-io/dropwizard-service-discovery/actions/workflows/merge-update.yml)
Provides service discovery to dropwizard services. It uses [Ranger](https://github.com/flipkart-incubator/ranger) for service discovery.## Dependency for bundle
io.appform.dropwizard.discovery
dropwizard-service-discovery-bundle
2.1.10-2```
## Dependency for ZK client
```
io.appform.ranger
ranger-zk-client
1.0-RC13
```## Dependency for Http client
```
io.appform.ranger
ranger-http-client
1.0-RC13
```## How to use the bundle
You need to add an instance of type _ServiceDiscoveryConfiguration_ to your Dropwizard configuration file as follows:
```
public class AppConfiguration extends Configuration {
//Your normal config
@NotNull
@Valid
private ServiceDiscoveryConfiguration discovery = new ServiceDiscoveryConfiguration();
//Whatever...
public ServiceDiscoveryConfiguration getDiscovery() {
return discovery;
}
}
```Next, you need to use this configuration in the Application while registering the bundle.
```
public class App extends Application {
private ServiceDiscoveryBundle bundle;
@Override
public void initialize(Bootstrap bootstrap) {
bundle = new ServiceDiscoveryBundle() {
@Override
protected ServiceDiscoveryConfiguration getRangerConfiguration(AppConfig appConfig) {
return appConfig.getDiscovery();
}@Override
protected String getServiceName(AppConfig appConfig) {
//Read from some config or hardcode your service name
//This will be used by clients to lookup instances for the service
return "some-service";
}@Override
protected int getPort(AppConfig appConfig) {
return 8080; //Parse config or hardcode
}
@Override
protected NodeInfoResolver createNodeInfoResolver(){
return new DefaultNodeInfoResolver();
}
};
bootstrap.addBundle(bundle);
}@Override
public void run(AppConfig configuration, Environment environment) throws Exception {
....
//Register health checks
bundle.registerHealthcheck(() -> {
//Check whatever
return HealthcheckStatus.healthy;
});
...
}
}
```
That's it .. your service will register to zookeeper when it starts up.Sample config section might look like:
```
server:
...
discovery:
namespace: mycompany
environment: production
zookeeper: "zk-server1.mycompany.net:2181,zk-server2.mycompany.net:2181"
...
...
```The bundle also adds a jersey resource that lets you inspect the available instances.
Use GET /instances to see all instances that have been registered to your service.## How to use the client
The client needs to be created and started. Once started it should never be stopped before the using service
itself dies or no queries will ever be made to ZK. Creation of the client is expensive.Imagining ShardInfo is your nodeData.
```
RangerClient client = SimpleRangerZKClient.builder()
.connectionString("zk-server1.mycompany.net:2181, zk-server2.mycompany.net:2181")
.namespace("mycompany")
.serviceName("some-service")
.environment("production")
.objectMapper(new ObjectMapper())
.deserializer(
data -> {
try {
return environment.getObjectMapper().readValue(data, new TypeReference>() {
});
} catch (IOException e) {
log.warn("Error parsing node data with value {}", new String(data));
}
return null;
}
)
.initialCriteria(
shardInfo -> true
)
.alwaysUseInitialCriteria(true)
.build();
```Start the client
```
client.start();
```Get a valid node from the client:
```
Optional> node = client.getNode();//Always check if node is present for better stability
if(node.isPresent()) {
//Use the endpoint details (host, port etc)
final String host = node.get().getHost();
final int port = node.get().getPort();
//Make service calls to the node etc etc
}
```Close the client when not required:
```
client.stop();
```*Note*
- Never save a node. The node query is extremely fast and does not make any remote calls.
- Repeat the above three times and follow it religiously.## License
Apache 2# NOTE
Package and group id has changed from `io.dropwizard.discovery` to `io.appform.dropwizard.discovery` from 1.3.12-2.