Java Magazine, Jan/Feb 2016
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2016 85 microservices getResultList return Response ok bookings build @ POST public Response createBooking Booking b em getTransaction begin em persist b em getTransaction commit return Response status Response Status CREATED entity b build As you can see with KumuluzEE the microservice implementation does not require any modifications and is exactly the same as with any other Java EE application You can implement the remaining microservices the same way or look at the provided sample code Handling Service Discovery Lets suppose that we are using JavaServer Faces JSF for the front end UI module From there we will be calling our microservices via REST to receive or save the actual data The main problem with this is that we need to know the exact URLs of the microservices We could pass the URLs via environment variables However that approach would require manual updating every time anything changes And in the cloud these changes can be quite frequent A better solution would be to dynamically query the address of the requested microservice We will use Apache ZooKeeper for service discovery but we could also use any similar tool In short ZooKeeper is a centralized service for maintaining configuration information We will use it to store the correct URL to each microservice We add a helper class ServiceRegistry in our utils module to handle the dynamic registering unregistering and retrieval of endpoints with ZooKeeper @ ApplicationScoped public class ServiceRegistry private final CuratorFramework zookeeper private final ConcurrentHashMap String String zonePaths @ Inject public ZooKeeperServiceRegistry throws IOException try String zookeeperUri System getenv ZOOKEEPER_ URI zookeeper CuratorFrameworkFactory newClient zookeeperUri new RetryNTimes 5 1000 zookeeper start zonePaths new ConcurrentHashMap catch IOException public void registerService String name String uri try String node services name if zookeeper checkExists forPath node null zookeeper create creatingParentsIfNeeded forPath node String nodePath zookeeper create withMode
You must have JavaScript enabled to view digital editions.