Spring Cloud Microservices at Pivotal Platform

Spring Cloud Microservices at Pivotal Platform

Imagine you have multiple microservices running on different machines in multiple instances. It seems natural to think about the tools that help you in the process of monitoring and managing all of them. If we add that our microservices are created based on the Spring Cloud framework obviously seems we should look at the Pivotal platform. Here is figure with platform’s architecture download from the main Pivotal’s site.

Pivotal-Microservices-Architecture

Although the Pivotal Platform can run applications written in many languages it has the best support for Spring Cloud Services and Netflix OSS tools like you can see in the figure above. From the possibilities offered by Pivotal we can take advantage of three ways.

Pivotal Cloud Foundry – solution can be ran on public IaaS or private cloud like AWS, Google Cloud Platform, Microsoft Azure, VMware vSphere, OpenStack.

Pivotal Web Services – hosted cloud-native platform available at pivotal.io site.

PCF Dev – the instance which can be run locally as a a single virtual machine. It offers the opportunity to develop apps using an offline environment which basic services installed like Spring Cloud Services (SCS), MySQL, Redis databases and RabbitMQ broker. If you want to run it locally with SCS you need more than 6GB RAM free.

As a Spring Cloud Services there are available Circuit Breaker (Hystrix), Service Registry (Eureka) and standard Spring Configuration Server based on git configuration.

spring-config-server

That’s all I wanted to say about the theory. Let’s move on to practice. On the Pivotal website we have detailed materials on how to set it up, create and deploy a simple microservice based on Spring Cloud solutions. In this article I will try to present the essence collected from these descriptions based on one of my standard examples from the previous posts. As always sample source code is available on GitHub. If you are interested in detailed description of the sample application, microservices and Spring Cloud read my previous articles:

Part 1: Creating microservice using Spring Cloud, Eureka and Zuul

Part 3: Creating Microservices: Circuit Breaker, Fallback and Load Balancing with Spring Cloud

If you have a lot of free RAM you can install PCF Dev on your local workstation. You need to have Virtual Box installed. Then download and install Cloud Foundry Command Line Interface (CF CLI) and PCF Dev. All is described here. Finally you can run command below and take a small break for coffee. Virtual machine needs to downloaded and started.

$ cf dev start -s scs

For those who do not have RAM enough (like me) there is Pivotal Web Services platform. It is available here. Before use it you have to register on the Pivotal site. The rest of the article is identical for both options.
In comparison to previous examples of Spring Cloud microservices, we need to make some changes. There is one additional dependency inside every microservice’s pom.xml.

<properties>
   ...
   <spring-cloud-services.version>1.4.1.RELEASE</spring-cloud-services.version>
   <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>

<dependencies>
   <dependency>
      <groupId>io.pivotal.spring.cloud</groupId>
      <artifactId>spring-cloud-services-starter-service-registry</artifactId>
   </dependency>
   ...
</dependencies>

<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>${spring-cloud.version}</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
      <dependency>
         <groupId>io.pivotal.spring.cloud</groupId>
         <artifactId>spring-cloud-services-dependencies</artifactId>
         <version>${spring-cloud-services.version}</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>

We also use Maven Cloud Foundry plugin cf-maven-plugin for application deployment on Pivotal platform. Here is sample for account-service. We run two instances of that microservice with max memory 512MB. Our application name is piomin-account-service.

<plugin>
   <groupId>org.cloudfoundry</groupId>
   <artifactId>cf-maven-plugin</artifactId>
   <version>1.1.3</version>
   <configuration>
      <target>http://api.run.pivotal.io</target>
      <org>piotrminkowski</org>
      <space>development</space>
      <appname>piomin-account-service</appname>
      <memory>512</memory>
      <instances>2</instances>
      <server>cloud-foundry-credentials</server>
   </configuration>
</plugin>

Don’t forget to add credentials configuration into Maven settings.xml file.

<server>
   <id>cloud-foundry-credentials</id>
   <username>piotr.minkowski@gmail.com</username>
   <password>***</password>
</server>

Now, when building sample application we to append cf:push command.

$ mvn clean install cf:push
 

Here is circuit breaker implementation inside customer-service.

@Service
public class AccountService {

   @Autowired
   private AccountClient client;

   @HystrixCommand(fallbackMethod = "getEmptyList")
   public List<Account> getAccounts(Integer customerId) {
      return client.getAccounts(customerId);
   }

   List<Account> getEmptyList(Integer customerId) {
      return new ArrayList<>();
   }

}

There is a randomly generated delay on the account’s service side, so 25% of the calls circuit breaker should be activated.

@RequestMapping("/accounts/customer/{customer}")
public List<Account> findByCustomer(@PathVariable("customer") Integer customerId) {
   logger.info(String.format("Account.findByCustomer(%s)", customerId));
   Random r = new Random();
   int rr = r.nextInt(4);
   if (rr == 1) {
      try {
         Thread.sleep(2000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }
   return accounts.stream().filter(it -> it.getCustomerId().intValue() == customerId.intValue())
      .collect(Collectors.toList());
}

After successfully deploying application using Maven cf:push command we can go to Pivotal Web Services console available at https://console.run.pivotal.io/. Here are our two deployed services: two instances of piomin-account-service and one instance of piomin-customer-service.

pivotal-1

I have also activated the Circuit Breaker and Service Registry from Marketplace.

pivotal-2

Every application needs to be bound to service. To enable it select service, then expand Bound Apps overlap and a select checkbox next to each service name.

pivotal-4

After this step application needs to be restarted. It also can be using a web dashboard inside each service.

pivotal-5

Finally, all services are registered in Eureka and we can perform some tests using customer endpoint https://piomin-customer-service.cfapps.io/customers/{id}.

pivotal-4

Final words

With Pivotal solution, we can easily deploy, scale, and monitor our microservices. Deployment and scaling can be done using the Maven plugin or via a web dashboard. On Pivotal, there are also available some services prepared especially for microservices needs like service registry, circuit breaker, and configuration server. Pivotal is a competition for such solutions as Kubernetes which based on Docker containerization (more about these tools here). It is especially useful if you are creating a microservices-based on Spring Boot and Spring Cloud frameworks.

0 COMMENTS

Leave a Reply