Skip to content

Microservice structure

AndreSonntag edited this page Mar 19, 2018 · 14 revisions

In this page we introduce the microservice structure. This includes the folder, package and class structure. For the following descriptions we are using the fictional microservice: my-ms

Project structure

Our microservices are maven projects with the following structure:

my-ms
|-- .gitignore
|-- pom.xml
`-- src
    `-- main
        |-- java
        |   `-- org
        |       `-- dice_research
        |           `-- sask
        |               `-- my_ms
        |                   -- MyMsApplication.java
        |                   -- MyMsController.java
        |                   -- IMyMsService.java
        |                   -- MyMsService.java
        |-- resources
        |   -- application.yml
    `-- test
        |-- java
        |   `-- org
        |       `-- dice_research
        |           `-- sask
        |               `-- my_ms

MsApplication

This class is responsible to starting the microservice as Eureka client. The following code shows the code with the annotaions.

@SpringBootApplication
@EnableDiscoveryClient
public class MyMsApplication {
	public static void main(String[] args) {
		SpringApplication.run(MyMsApplication.class, args);
	}
}

REST controller

The controller class provides a REST interface for other microservices and web applications. This class should just take incoming requests and prepare they.

@RestController
public class MyMsController {

   private Logger logger = Logger.getLogger(MyMsController.class.getName());

   @RequestMapping(value = "/foo")
   public boolean foo(String x) {
       logger.info(myMs foo() invoked)
       ...
   }
}

Service class

The service class implements the methods of the IService interface. This methods should include the real logic of the microservice. In our project is a typical example forwarding of data.

Application.yml

This config file is descripted in page "Microservice type"

Clone this wiki locally