Today, all of us have slightly different understanding what Microservices are but, among all those definitions and attributes, there's probably one thing that fits them all - they need to be simple.
So let's have a look at some practical example of creating a REST service with Infinispan as a storage wired together using CDI. We will use Wildfly Swarm as a running platform.
Bootstrapping new project
A good way to start a new Wildfly Swarm project is to generate it. The only requirement here is to add "JAX-RS with CDI" and "JPA" as dependencies.
The next step is to add infinispan-embedded artifact. The final pom.xml should look like the following:
This file contains 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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.infinispan</groupId> | |
<artifactId>wildfly-swarm-demo</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<packaging>war</packaging> | |
<properties> | |
<version.infinispan>8.2.4.Final</version.infinispan> | |
<version.wildfly.swarm>2016.9</version.wildfly.swarm> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
<failOnMissingWebXml>false</failOnMissingWebXml> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>bom-all</artifactId> | |
<version>${version.wildfly.swarm}</version> | |
<scope>import</scope> | |
<type>pom</type> | |
</dependency> | |
<dependency> | |
<groupId>org.infinispan</groupId> | |
<artifactId>infinispan-bom</artifactId> | |
<version>${version.infinispan}</version> | |
<scope>import</scope> | |
<type>pom</type> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<build> | |
<finalName>wildfly-swarm-demo</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>wildfly-swarm-plugin</artifactId> | |
<version>${version.wildfly.swarm}</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>package</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<!-- Java EE 7 dependency --> | |
<dependency> | |
<groupId>javax</groupId> | |
<artifactId>javaee-web-api</artifactId> | |
<version>7.0</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.infinispan</groupId> | |
<artifactId>infinispan-embedded</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>javax.cache</groupId> | |
<artifactId>cache-api</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>jaxrs-cdi</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.wildfly.swarm</groupId> | |
<artifactId>jpa</artifactId> | |
</dependency> | |
</dependencies> | |
</project> |
Writing some code
Infinispan CDI Extension will take care of bootstrapping Infinispan, so we can dive directly into JAX-RS code:
This file contains 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
package com.example.configuration; | |
import javax.ws.rs.ApplicationPath; | |
import javax.ws.rs.core.Application; | |
@ApplicationPath("/rest") | |
public class RESTConfiguration extends Application { | |
} |
This file contains 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
package com.example.rest; | |
import javax.inject.Inject; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.POST; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Response; | |
import org.infinispan.Cache; | |
@Path("/infinispan") | |
public class InfinispanEndpoint { | |
@Inject | |
Cache<String, String> cache; | |
@GET | |
@Path("cache") | |
public Response printContent() { | |
return Response.ok(cache.keySet().toString()).build(); | |
} | |
@POST | |
@Path("cache") | |
public Response addSomethingToTheCache(String text) { | |
cache.put(text, text); | |
return Response.created(null).build(); | |
} | |
} |
And that's it!
What's next?
If you'd like to have a look at the complete example, check out my repository. The code is based on fresh build from Infinispan master branch which contains lots of improvements for CDI. You might build it yourself or just wait for 9.0.0.Beta1.
Have a lot of fun with Microservices!
Is it possible to share data between nodes via Infinispan cache? See https://github.com/slaskawi/infinispan-wf-swarm-example/issues/2
ReplyDeleteThe repository has been moved to https://github.com/infinispan-demos/infinispan-wf-swarm-example
ReplyDelete