Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 86 microservices CreateMode EPHEMERAL_ SEQUENTIAL forPath node _ uri getBytes zonePaths put uri nodePath catch Exception ex Similar for the other methods See full example available for download The ZOOKEEPER_ URL environment variable should contain our ZooKeeper URL We will be starting a ZooKeeper service alongside our microservices using Docker which is discussed in the next section Also make sure that the utils package is added as a dependency to every microservice We can now inject the service above into our microservices to register its URL upon startup and unregister it upon shutdown One way to do this is to add a bean to our booking microservice to handle this task dynamically @ ApplicationScoped public class BookingService @ Inject ServiceRegistry services private String serviceName trains booking private String endpointURI public BookingService endpointURI System getenv BASE_ URI @ PostConstruct public void registerService services registerService serviceName endpointURI @ PreDestroy public void unregisterService services unregisterService serviceName endpointURI We use the @ PostConstruct @ PreDestroy and @ ApplicationScoped annotations to make sure we register and unregister the service only once per lifecycle The BASE_ URI environment variable should contain the microservices public URI We can now create a similar bean for each of our microservices Injecting the Microservices at the Front End We can use the just described approach for injection on the front end to connect to the REST services We can inject our ServiceDiscovery bean to our JSF backing bean to retrieve the URL of a microservice we want to invoke as shown below @ Model public class BookingsBean @ Inject ServiceRegistry services public List getAllBookings return ClientBuilder newClient target services discoverServiceURI trains booking path bookings request get List class
You must have JavaScript enabled to view digital editions.