Developing and Deploying a ServiceComb Java Microservice
Apache ServiceComb Java chassis is an open-source Java microservice development framework that helps you quickly develop microservices and provides a series of out-of-the-box features.
This section describes how to use Java chassis to connect to HUAWEI CLOUD CSE.
Prerequisites
- You have obtained the AK/SK. For details about how to obtain the AK/SK, see Access Keys.
- You have obtained the project name. For details about how to obtain the project name, see API Credentials.
- You have learned how to purchase a ServiceComb microservice engine and have purchased a ServiceComb microservice engine. For details, see Creating an Exclusive Microservice Engine.
- You have a good understanding of Java programming and be able to use a Java IDE for development.
- You are familiar with Maven for dependency management and could use common Maven commands for compilation and packaging.
Service Running Principles
The service running principles and description are as follows:

- The consumer and provider are microservices developed.
- The exclusive ServiceComb engine is purchased on the console and services are connected to the engine.
- Services are registered with the service center so that other services can discover the address for communication. You can manage configuration items through the configuration management service.
Local Development
Java chassis supports the following development modes for service provider:
- Develop Microservice with SpringMVC
- Develop Microservice with JAX-RS
- Develop Microservice with Transparent RPC
Service consumers can be developed in either of the following ways:
You are advised to use Spring MVC to develop service providers and use transparent RPC to develop service consumers.
After the development, you can use ServiceStage for service deployment, hosting, and O&M.
Developing the First Microservice
Before developing a Java chassis microservice, you need to prepare the local development environment.
- Configure the POM dependency.
<!--Configure the CSE version--> <properties> <cse.version>3.0.0</cse.version> </properties> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>huaweicloudsdk-releases</id> <name>huaweicloudsdk</name> <url>https://repo.huaweicloud.com/repository/maven/huaweicloudsdk/</url> </repository> </repositories> <dependencyManagement> <dependencies> <dependency> <groupId>com.huawei.paas.cse</groupId> <artifactId>cse-dependency</artifactId> <version>${cse.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.huawei.paas.cse</groupId> <artifactId>cse-solution-service-engine</artifactId> </dependency> <!-- default using log4j2 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </dependency> </dependencies>To create an empty Maven project, you are advised to configure dependencyManagement to manage dependencies. Only the dependencies related to cse-solution-service-engine and log4j need to be introduced.
- Add a configuration file.
By default, CSE reads the configuration file microservice.yaml and stores it in the resources directory.
The file content is shown as follows. This file indicates that the helloworld microservice of the sample application is developed and the version is 0.0.1. The service center and configuration center connected to the microservice are CSE engines, and the listening port is 8080.
In this example, the service center is used. You need to ensure that the local network can communicate with the CSE engine. For details about how to obtain the service center and configuration center addresses, see Viewing the Access Address of a Microservice Engine.
APPLICATION_ID: sample # Application ID of the application to which the service belongs. By default, services mutually called must be in the same application. service_description: name: helloworld #Service name. version: 1.0.0 #Service version. servicecomb: service: registry: address: https://xxx.xxx # Service registry and discovery address of the CSE engine. Set https or http based on the site requirements. Ensure that the local address can communicate with the registry center address. config: client: serverUri: https://xxx.xxx # Configuration center address of the CSE engine. Set https or http based on the site requirements. Ensure that the local address can communicate with the configuration center address. rest: address: 0.0.0.0:8080 # Service listening address. credentials: accessKey: ak #Set it based on the actual AK and its encryption and decryption extension. secretKey: sk #Set it based on the actual SK and its encryption and decryption extension. akskCustomCipher: default #Extend the value of name for the implementation class of com.huawei.paas.foundation.auth.credentials.AKSKCipher. Setting it to default indicates that the key is in plaintext and does not need encryption or decryption. - Compile the Main class for microservice instance start.
Add a Main class to the project to start microservice instances.
import org.apache.servicecomb.foundation.common.utils.BeanUtils; public class AppMain { public static void main(String[] args) { BeanUtils.init(); } }Calling the org.apache.servicecomb.foundation.common.utils.BeanUtils#init() method completes a series of startup processes, such as configuration loading, Spring application context loading, and microservice registry.
- Compile the REST API class.
Add a REST API class to the project for receiving and verifying requests.
import org.apache.servicecomb.provider.rest.common.RestSchema; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @RestSchema(schemaId = "hello") @RequestMapping("/") public class HelloWorldService { @GetMapping("/hello") public String hello() { return "Hello world!"; } } - Add a log configuration file.
The log4j2 component is introduced to solution-basic. To view run logs, you need to manually add a log configuration file, which is stored in resources\log4j2.xml. The following is an example:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}[%L] - %msg%n"/> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> - Start and verify the service.
Start the AppMain class to start the microservice.
Then, you can view the registered service by choosing Service Catalog from the CSE console. Access http://127.0.0.1:8080/hello. The helloworld service response "Hello world!" is displayed.
# Try to access http://127.0.0.1:8080/hello. curl -k http://127.0.0.1:8080/hello "Hello world!"
The first Java chassis microservice is developed.
- Generate an executable JAR package.
- Generally, microservices are started and deployed in JAR packages.
- Java chassis has no restriction on the packaging mode of executable JAR packages. In this example, spring-boot-maven-plugin is used. You need to add the following configurations to the pom file. Note that the mainClass property must be set to the main class of the service.
<build> <!-- Name of the generated JAR package is specified.--> <finalName>app</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--The latest version is recommended.--> <version>2.2.5.RELEASE</version> <configuration> <!--Path for starting the Main class is specified.--> <mainClass>xxx.xxx.xxx.AppMain</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>Run the mvn clean install -DskipTests command in the directory at the same level as the pom.xml file. An executable JAR package named app is generated.
# Command in the directory at the same level as the pom file is executed. mvn clean install -DskipTests # The default JAR package is generated in the target directory. cd target # Start the microservice. java -jar app.jar
After the microservice is started, you can access the CSE console and http://127.0.0.1:8080/hello to verify that the microservice is started and functions properly.
Deployment
Prerequisites for deployment:
- An application has been created.
- An Environment has been created, including CCE clusters, ELBs, and CSE engines.
- Upload the JAR package.
Log in to ServiceStage, choose Software Center, and upload images. For details, see Uploading the Software Package.
- Log in to ServiceStage, add a component to the created application, and set the component type to Microservice.

- Deploy the JAR package of the helloworld sevice.
- Enter hello-cse for Name.
- Select the Java Chassis framework.
- Select Java8 for Runtime System.
- Select JAR package for Source Code/Software Package.

- Click Create and Deploy and select an environment.

- Configure the component, enable Internet Access, and select the target Cloud Service Engine.

- Confirm the specifications and submit the configuration. Wait until the component is deployed.
Verifying Deployment
- Choose Infrastructure > Cloud Service Engines, select a CSE engine, and click Console. On the page that is displayed, choose Service Catalog. The component is successfully registered.

- Go back to the ServiceStage console, choose Application Component from the navigation pane, and select and click the created component. On the component Overview page, click the External Access Address. Call the corresponding API "/hello". The following information is displayed:
"Hello world!"
Routine Maintenance
Last Article: Developing and Deploying a Spring Cloud Microservice
Next Article: Developing and Deploying a Go Microservice
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.