Develop the first microservice


Before developing the first Java-Chassis microservice, please make sure that your local development environment is prepared. See more details about this in Prepare the local development environment.

The Apache Service Center is needed in this show case. About using service center, please refer to Install of ServiceCenter.

Develop a HelloWorld microservice

pom configurations

First, please to create an empty maven project. It is suggested that the dependencies should be managed by dependencyManagement item, so that only the solution-basic is needed to be imported as dependency:


solution-basic has contains almost all of the dependencies you need in the common cases.

For the version 2.0.0 Java-Chassis, maven-compiler-plugin compilation plugin is also needed, which can make the method argument names preserved during the source project is build into a jar file.


add microservice configurations

By default, Java-Chassis read configurations from the file named microservice.yaml placed in resources directory.

The content of the file is like below.

APPLICATION_ID: sample    # this microservice belongs to the sample application
  name: helloworld        # this microservice is named "helloworld"
  version: 1.0.0
      address:  # this address of the service center

Main class

Add a main class in the project:

package org.apache.servicecomb.samples;


public class AppMain {
  public static void main(String[] args) {

Invoking the method will trigger the boot up procedure including configuration loading, Spring application context loading, microservice registration.

Write a REST service interface

Add a REST service interface class to declare the request you want to handle.

package org.apache.servicecomb.samples.service;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RestSchema(schemaId = "hello")
public class HelloWorldService {
  public String hello() {
    return "Hello world!";

Add log configuration

The solution-basic module introduces the log4j2 module. To enable it, a configuration file is needed. This file should be placed in resources\log4j2.xml and the content is like below:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}[%L] - %msg%n"/>
    <Root level="info">
      <AppenderRef ref="Console"/>

Start the microservice

After completing the work above, the microservice can be started by running the AppMain class. Please visit the web page of local service center If the helloworld microservice instance is found like below, the microservice is started successfully.

If you visit the address, you can see the response "Hello world!" from helloworld service. Now, your first Java-Chassis microservice is completed!.


The introduction demo is developed in Spring MVC style. And there are currently 3 ways to choose:

  • Spring MVC
  • JaxRS
  • RPC

Developers can quickly build a project in following ways:

Maven provide archetypes to generate projects. Java-chassis implements many types of archetypes, see LINK for details.

  • Generate projects using ServiceComb Spring Initializer

ServiceComb Spring Initializer is an graphic user interface to generate projects. See LINK