Developing Resilient Microservices With Istio and MicroProfile
In this post, we give a quick rundown of how developers can go about creating and working with resilient microservices using these two popular frameworks.
Join the DZone community and get the full member experience.
Join For Freeas stated in the reactive manifesto , cloud-native reactive applications need to be resilient:
the system stays responsive in the face of failure. this applies not only to highly-available, mission-critical systems — any system that is not resilient will be unresponsive after a failure. resilience is achieved by replication, containment, isolation ...
in distributed systems, you need to design for failure. for example, microservices which invoke other microservices must be intelligent enough to continue to work even if some of their dependencies are currently not available.
there are several different ways to build resilient service meshes with istio, for example via circuit breakers and retries .
the istio functionality for resilient cloud-native applications is generic and independent from the implementation of the microservices. however, in some cases, the handling of failures depends on the business logic of the applications, which is why this needs to be implemented in the microservices.
i have open sourced a simple sample that demonstrates this functionality. the application uses a vue.js front-end that displays articles. the service 'web-api' implements the bff (backend for front-end) pattern. the web application accesses the 'web-api' service which invokes both the 'articles' and 'authors' services.

if you want to run this sample , get the code from github.
make sure that you've set up your development environment and that you have installed the prerequisites . after this, invoke the following commands to deploy the microservices.
$ git clone https://github.com/nheidloff/cloud-native-starter.git
$ cd cloud-native-starter
$ scripts/deploy-articles-java-jee.sh
$ scripts/deploy-web-api-java-jee.sh
$ scripts/deploy-authors-nodejs.sh
$ scripts/deploy-web-app-vuejs.sh
$ scripts/show-urls.sh
after running the scripts above, you will get a list of all urls in the terminal. the web application can be opened via http://your-minikube-ip:31380 . the initial page shows the five most recent articles including information about the authors.

now let's delete the authors service:
$ scripts/delete-authors-nodejs.sh
when you refresh the web application, it will still display five articles, but this time without the information about the authors. while the web application cannot display the complete information anymore, in this simple scenario it still makes sense to display the titles and links of the articles.

the implementation of this behavior has been done in service.java :
try {
author author = dataaccessmanager.getauthorsdataaccess().getauthor(corearticle.author);
article.authorblog = author.blog;
article.authortwitter = author.twitter;
} catch (noconnectivity | nonexistentauthor e) {
article.authorblog = "";
article.authortwitter = "";
}
next, let's delete the articles service:
$ scripts/delete-web-api-java-jee.sh
when you refresh the web application, you'll notice that the same five articles are still displayed. that's because, in this trivial scenario, the 'web-api' service caches the last read articles. if the 'articles' service is not available, it simply returns the information from the cache.
i'm using the eclipse microprofile fallback annotation. in this case, a fallback method is invoked if the original method throws an exception.
@fallback(fallbackmethod = "fallbacknoarticlesservice")
public list<article> getarticles() throws nodataaccess {
list<corearticle> corearticles = new arraylist<corearticle>();
try {
corearticles = dataaccessmanager.getarticlesdataaccess().getarticles(5);
} catch (noconnectivity e) {
system.err.println("com.ibm.webapi.business.getarticles: cannot connect to articles service");
throw new nodataaccess(e);
}
...
}
...
public list<article> fallbacknoarticlesservice() {
system.err.println("com.ibm.webapi.business.fallbacknoarticlesservice: cannot connect to articles service");
if (lastreadarticles == null) lastreadarticles = new arraylist<article>();
return lastreadarticles;
}
read the article microprofile, the microservice programming model made for istio , to learn more about how to build resilient applications with both istio and microprofile.
Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments