Last active
March 9, 2020 07:05
-
-
Save mohak1712/324bb41672d5aa788b2312209b58cd7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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