corporate
2015/12/17
 
17 Dec, 2015 | 3 min read

WSO2 Microservices Server: Microservices in Ten Minutes!

  • Yudhanjaya Wijeratne
  • Marketing Officer - WSO2
Before we begin, I’d like to start by saying that this is not an introduction to microservices. Much has been written about the microservices paradigm and monolithic hell. If what you’re after is a good introduction, look no further than NginX’s introduction to microservices or Microservices: a definition to this architectural term by Martin Fowler and James Lewis. Instead, we’re going to talk about WSO2 Microservices Server. At WSO2, we’ve put together a lightweight, high-performance microservices runtime with a  simple programming model for developing Java based microservices. It’s new, starts in under 300 milliseconds, and of course, integrates with our Data Analytics Server for additional insight. Let’s get started.

Hello, World

Let’s run through the process of getting the most basic sample running. This is literally a Hello World. Firstly, make sure you have Java 8 and Maven installed. Then grab the zip from Github. This folder is basically <MSS_HOME>. Make sure you mount the pom.xml into your Java IDE of choice. Then navigate to <MSS_HOME>/samples/hello_world. If you haven’t fired up your command prompt already, do it and type mvn package. This’ll take a short while, and when you’re done you’ll have a helloworld*.jar. Run it. From the target directory, use java -jar. Congratulations! You’ll see a Netty listener starting up. You’ll also see that the Microservices Server has started up - in so many milliseconds.

It’s alive!

Of course, you need to test it. Use the curl command: curl -v https://localhost:8080/hello/wso2 (depending on where you’ve put it) For windows users, try https://www.confusedbycode.com/curl/. You can also forgo Curl entirely and use HTTPie. You should get a response similar to the following: * Adding handle: conn: 0x7fc9d3803a00 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7fc9d3803a00) send_pipe: 1, recv_pipe: 0 * About to connect() to localhost port 8080 (#0) *   Trying ::1... * Connected to localhost (::1) port 8080 (#0) > GET /hello/wso2 HTTP/1.1 > User-Agent: curl/7.30.0 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < Content-Type: */* < Content-Length: 10 < Connection: keep-alive < * Connection #0 to host localhost left intact Hello wso2

What happens here?

Open up the HelloService class. import javax.ws.rs.GET; import javax.ws.rs Path; import javax.ws.rs Pathparam; That’s JAX-RS, which comes in handy when creating RESTful web services. @Path(“/hello”) public class HelloService{ @GET @Path (“(/name)”) public String hello(@PathParam(“name”) String name) { return “Hello “ + name; } } Here we have a simple class acting as a REST endpoint. This is what says hello. How does this get started? By something like this: public class Application ( public static void main(String[] args) { new MicrosevicesRunner(8080) .deploy(new HelloService()) .start(); } If you want to pass services through different ports, all you need to do is modify the arguments passed to MicroserviceRunner(). MicroservicesRunner(7888, 8888) will start up two Netty Listeners on port 7888 and 8888 respectively. Now for a second exercise: stockquotes. In the same manner as before, enter the <MSS_HOME> folder, navigate to samples, and enter the stockquotes-services folder. Technically, this sample is meant to demonstrate the use of @Produces and @Consumes annotations for bean conversion. We’re going to use it to see how a simple way of sending a POST message to our service. Use mvn package. curl -v https://localhost:8080/stockquote/GOOG You should get an output along these lines: (“symbol”: “GOOG”, “name”: “Alphabet Inc.”: “last”:652.3, “low”:657.81,”high”:643.15”) We’ve just pulled data that’s in our stockquote service. What if we wanted to add data, say, the stock price of another company? We can add it via a POST message using the same format. curl -v -X POST -H "Content-Type:application/json" -d '{"symbol":"BVMF","name": "Bovespa","last":149.62,"low":150.78,"high":149.18,"createdByHost":"localh ost"}' https://localhost:8080/stockquote This command will save a new symbol into the Stock Quote Service. Congratulations! You’ve successfully used Java 8 and Maven for setting up the WSO2 Microservices Server and building our services and samples, executed Microservices with a basic java -jar approach and seen how easy it to to deploy a REST Service onto our Microservices Server. For more exploration, drop by our documentation page for more samples and check out our more detailed slideshow.
Undefined