Skip to content

Instantly share code, notes, and snippets.

@mohak1712
Last active March 9, 2020 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohak1712/324bb41672d5aa788b2312209b58cd7d to your computer and use it in GitHub Desktop.
Save mohak1712/324bb41672d5aa788b2312209b58cd7d to your computer and use it in GitHub Desktop.
public class App {
private static Consul consul;
private static final String SERVICE_ID = "ping-pong-1-id";
public static void main(String[] args) {
port(8000);
consul = Consul.builder().withHostAndPort(HostAndPort.fromParts(System.getenv("host"), 8500))
.build();
AgentClient agentClient = consul.agentClient();
registerOnConsul(agentClient);
get("/ping", (req, res) -> "{\"message\":\"Pong\"}", Object::toString);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
agentClient.deregister(SERVICE_ID);
}));
}
private static void registerOnConsul(AgentClient agentClient) {
Registration service = ImmutableRegistration.builder()
.id(SERVICE_ID)
.name("ping-pong-1")
.port(8000)
.build();
agentClient.register(service);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment