Help Center> ServiceStage> FAQ> Java SDK FAQs> How Do I Perform Local Development and Test?

How Do I Perform Local Development and Test?

The lightweight local service center provides you with the service discovery, registration, and query functions during development, commissioning, and testing.

Concept

This section describes how you can locally develop and commission consumer and provider applications. Both service providers and consumers need to connect to a remote service center. Two methods of building a local service center for local microservice commissioning are as follows:

  1. Starting the local service center.
  2. Starting the local service center (Mock mechanism) by using the local file.

The local service center is an important component in the microservice architecture, and is used for managing, registering, and detecting metadata and instance metadata. The logic relationship between the service center and microservice provider/consumer is as follows:

Prerequisites

  • JDK 1.8 has been installed, and the JAVA_HOME environment variable has been correctly configured.
  • Starting the local service center will occupy ports 30100 and 30103 of the server, which respectively indicate the backend and frontend service ports of the service center. Ensure that these two ports are not in use.

Starting the Local Service Center

  1. Go to and download the installation package of Local Service Center.
  2. Install the package of the service center.

    • Software package installation: Decompress the installation package to the installation directory.
    • Image package installation: Run the following command to install the service center:
      docker load -i local-service-center-{version}-docker.tar
      1. The OS must be 64-bit.
      2. If you need to change the IP address and port number when installing the software package of the service center, modify the /../conf/app.conf file. In the file, frontend_host_ip and frontend_host_port respectively represent the IP address and port number for monitoring the frontend service of the service center, and httpaddr and httpport respectively represent the IP address and port number for monitoring the backend service of the service center. The following shows how to change the IP address and port number by taking the backend service of the service center as an example:
        If httpaddr is set to 127.0.0.1, the service center can be accessed by a local process. To enable the service center to be accessed by remote processes, set httpaddr to an external IP address or 0.0.0.0.
        # listen address
        httpaddr = 127.0.0.1
        httpport = 30100

  3. Start the local service center.

    • For the Windows OS: In the installation package, click the start.bat script to start the local service center.
    • For the Linux OS: Run the start.sh script to start the local service center.

      Before running the start.sh script, run the following command to modify the file permission:

      chmod -R 550 /../bin

    • For the Docker container: Run the following command to start the local service center:

      docker rm -f sc

      docker run -d --name sc -p 30100:30100 -p 30103:30103 local-service-center:latest

  4. Enter http://127.0.0.1:30103 in the address bar of a browser, and access the frontend page.
  5. After the local service center starts, configure the service center address in the microservice.yaml file that stores SDK configurations, as shown in the following:

    cse:
      service:
        registry:
          address: http://127.0.0.1:30100      #Backend address and port number of the service center
    • By default, the listening address of the service center is set to 0.0.0.0. To enhance security, you can set the listening address to 127.0.0.1.
    • If you have customized the IP address and port number when using the service center, change the value of address as required.
    • If the configuration center and dashboard are not used, delete related configurations.

    After the microservice application is developed, run MainServer on the server to start the service process and register services with the service center.

  6. (Development service providers and consumers) Start the microservice for local tests.

Enabling the Local Service Center Using the Mock Mechanism

  1. Create the registry.yaml file for local service center.

    springmvctest:
      - id: "001"
        version: "1.0"
        appid: myapp                        #Commissioning service ID
        instances:
          - endpoints:
            - rest://127.0.0.1:8080         

    In the mock mechanism, you need to prepare service schemas on your own. Currently, only local commissioning for service consumers is supported.

  2. Add the following code to the end of the Main function for service consumers.

    1
    2
    3
    4
    5
    6
    public class xxxClient {
    public static void main(String[] args) {
        System.setProperty("local.registry.file", "/path/registry.yaml");
        //your code
        System.clearProperty("local.registry.file");
    }
    

    Set the second parameter of setProperty to the absolute path of registry.yaml in the disk. Path separators vary depending on systems.

  3. (Development service consumers) Start the microservice for local tests.

Entering the Developer Mode

When the Local Service Center is downloaded for local test and development, the Local Service Center needs to be restarted or the version needs to be upgraded every time the code is changed. Otherwise, an error is displayed.

Add the following configuration to the microservice.yaml file to enter the development mode.

service_description.environment: development