Creating a microservice (the Producer)

We will discuss and create a microservice, account-service, as we have seen in the preceding diagram. This microservice registers itself with the registry service or discovery service with its logical service name as account-service.

First, we need to add the required Maven dependencies. Again, use the spring-cloud release train to manage versions:

<properties> 
   ... 
   <spring-cloud.version>Finchley.M8</spring-cloud.version> 
</properties> 
 
<dependencies> 
   <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-data-jpa</artifactId> 
   </dependency> 
   <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-web</artifactId> 
   </dependency> 
   <dependency> 
         <groupId>org.springframework.cloud</groupId> 
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 
   </dependency> 
   <dependency> 
         <groupId>com.h2database</groupId> 
         <artifactId>h2</artifactId> 
         <scope>runtime</scope> 
   </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> 
   </dependencies> 
</dependencyManagement> 

The preceding Maven configuration has the spring-cloud-starter-netflix-eureka-client dependency for @EnableEurkaClient instead of the @EnableDiscoveryClient annotation. But you can also use the @EnableDiscoveryClient annotation to register this service with the registry server. And this configuration also has the spring-boot-starter-data-jpa dependency for creating the Spring Data JPA repository, spring-boot-starter-web for creating @RestController, and the h2 in-memory database to save the data associated with the account application.

Let's see the following main class of the Spring Boot application for accountservice:

package com.dineshonjava.accountservice; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
 
@SpringBootApplication 
@EnableEurekaClient 
public class AccountServiceApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(AccountServiceApplication.class, args); 
   } 
} 

The preceding main application class is annotated with @SpringBootApplication and @EnableEurekaClient@SpringBootApplication is used for the Spring Boot auto-configuration, as we have already discussed. The next annotation, @EnableEurekaClient, is used to activate the Netflix EurekaClient implementation.

We can also enable discovery with the @EnableDiscoveryClient annotation on a @Configuration class or the @SpringBootApplication entry point class. @EnableDiscoveryClient activates the Netflix Eureka DiscoveryClient implementation.

There are multiple implementations of Discovery Service, such as Eureka, Consul, and Zookeeper. In this example, we have used @EnableEurekaClient explicitly but it will be available only when the spring-cloud-starter-netflix-eureka-client dependency is available on your application classpath, and it only works for Eureka. You could also use @EnableDiscoveryClient; it lives in spring-cloud-commons and picks the implementation on the classpath. And there is no difference between either using the @EnableEurekaClient or the @EnableDiscoveryClient annotation. They are effectively the same.

Let's create a configuration file, application.properties (or application.yml), with a couple of configuration settings:

spring: 
  application: 
    name: account-service 
 
server: 
  port: 6060 
 
eureka: 
  client: 
    service-url: 
      default-zone: ${EUREKA_URI:http://localhost:8761/eureka} 
  instance: 
    prefer-ip-address: true 

This file tells us the application name is account-service, and the server port will be 6060. And this tells the application where the Eureka Server is to register itself with the account-service logical service name.

Now let's start account-service and you can see that it will be registered with Eureka discovery service:

The ACCOUNT-SERVICE microservice is registered with Eureka discovery service, as you can see under Instances currently registered with Eureka in the Dashboard.

The @EnableEurekaClient annotation makes this application account-service into both a Eureka instance by registering itself, as you can see in the preceding dashboard, and also a client, so that it can access or query other registered services. We can control the behavior of this instance, and also we can see the health of this instance by configuring some settings with the eureka.instance.* configuration keys.

Let's see other classes of this service. We have created a REST Controller by using the @RestController annotation. You can refer to my book, Spring 5 Design Patterns—it explains the Spring MVC module in details. Here I am just going to use the @RestController annotation without explaining much about the Spring MVC module, to create the REST controller to handle RESTful API calls:

package com.dineshonjava.accountservice.controller; 
import java.util.List; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.DeleteMapping; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.PostMapping; 
import org.springframework.web.bind.annotation.PutMapping; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RestController; 
 
import com.dineshonjava.accountservice.domain.Account; 
import com.dineshonjava.accountservice.repository.AccountRepository; 
 
@RestController 
public class AccountController { 
    
   @Autowired 
   AccountRepository accountRepository; 
    
   @PostMapping(value = "/account") 
   public Account save (@RequestBody Account account){ 
         return accountRepository.save(account); 
   } 
    
   @GetMapping(value = "/account") 
   public Iterable<Account> all (){ 
         return accountRepository.findAll(); 
   } 
    
   @GetMapping(value = "/account/{accountId}") 
   public Account findByAccountId (@PathVariable Integer accountId){ 
         return accountRepository.findAccountByAccountId(accountId); 
   } 
    
   @PutMapping(value = "/account") 
   public Account update (@RequestBody Account account){ 
         return accountRepository.save(account); 
   } 
    
   @DeleteMapping(value = "/account") 
   public void delete (@RequestBody Account account){ 
         accountRepository.delete(account); 
   } 
   ... 
} 

This REST controller has several request handler methods to perform the CRUD operations. The save() request handler method creates a new account, we can read all accounts using the all() handler method, the update() handler method updates the existing account of a given account ID. And also we can delete an account using the delete() handler method.

The AccountController REST controller has an AccountRepository property. This repository is an interface extending with the CrudRepository interface of the Spring Data JPA. We will cover Spring Data in the next section. This repository uses the H2 database to store all information about the account. You can find complete a example on GitHub about this account service application.

Let's see whether the following URIs have been exposed by this REST controller of the account microservice:

  • Create a new account: @PostMapping("/account")
  • Read all accounts: @GetMapping("/account")
  • Get details of a specific account: @GetMapping("/account/{accountId}")
  • Update account details: @PutMapping("/account/{accountId}")
  • Delete an account: @DeleteMapping("/account")

The preceding endpoints have been used by the web application to access the account microservice.

Let's create microservice consumers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.235.144