Updated on 2024-05-06 GMT+08:00

Connecting Java Chassis Applications to CSE Engines

This section describes how to connect Java chassis applications to CSE engines and use the most common functions of CSE engines. For details about the development guide, see Using Microservice Engine Functions.

In the Apache ServiceComb Samples project, you can find the code corresponding to the development methods in this section.

Prerequisites

  • Microservice applications have been developed based on Java chassis.

    For details about microservice application development in the Java chassis framework, see https://servicecomb.apache.org/references/java-chassis/en_US/.

  • Version requirements. See Requirements for Microservice Development Framework of a.
  • This document assumes that you use Maven for dependency management and packaging in your project. You are familiar with the Maven dependency management mechanism and are able to modify the dependency management and dependency in the pom.xml file.
  • Java chassis can be used together with different technologies. The name of the configuration file is related to the technology you use. For example, if you use Java chassis in Spring mode, the configuration file name is microservice.yaml. If you use Java chassis in Spring Boot mode, the configuration file name is application.yaml. This document uses microservice.yaml to indicate the configuration file. You need to use a configuration file name corresponding to your project.

Procedure

  1. Add dependencies to the pom.xml file of the project.

    <dependency>
        <groupId>org.apache.servicecomb</groupId>
        <artifactId>solution-basic</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.servicecomb</groupId>
        <artifactId>servicestage-environment</artifactId>
    </dependency>
    • The solution-basic module contains common Java chassis functions, such as the configuration center module and service governance module, which allow you to enable these functions in one-click.
      <!-- Configuration center module -->
      <dependency>
      <groupId>org.apache.servicecomb</groupId>
      <artifactId>config-cc</artifactId>
      </dependency>
      <!-- Service governance module -->
      <dependency>
      <groupId>org.apache.servicecomb</groupId>
      <artifactId>handler-governance</artifactId>
      </dependency>
    • The servicestage-environment module consists of the following dependent module:
      <!-- Registry and discovery module -->
      <dependency>
        <groupId>org.apache.servicecomb</groupId>
        <artifactId>registry-service-center</artifactId>
      </dependency>

    You are advised to use Maven Dependency Management to manage the third-party software dependencies of a project. Add the following information to the pom.xml file of the project:

    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.apache.servicecomb</groupId>
            <artifactId>java-chassis-dependencies</artifactId>
            <version>${java-chassis.version}</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>  
    </dependencyManagement>

    Skip the operation if your project already contains the preceding dependencies.

    The servicestage-environment software package is optional. This software package provides the environment variable mapping function. When you use ServiceStage to deploy applications, you do not need to manually modify information such as the registry center address, configuration center address, and project name. The default configurations in the microservice.yaml file are overwritten by environment variables. The mapping.yaml file is contained in the software package. You can also add the mapping.yaml file to your own project.

    The mapping.yaml file may change in later versions to support the latest functions of CSE engines. If you do not want the new version to evolve with CSE engines, you can add the mapping.yaml file to your project instead of adding the servicestage-environment dependency.

    Generally, the microservice.yaml and mapping.yaml files are stored in the /src/main/resources/ directory in the root directory of the current project.

    PAAS_CSE_ENDPOINT:
      - servicecomb.service.registry.address
      - servicecomb.config.client.serverUri
    PAAS_CSE_SC_ENDPOINT:
      - servicecomb.service.registry.address
    PAAS_CSE_CC_ENDPOINT:
      - servicecomb.config.client.serverUri
    PAAS_PROJECT_NAME:
      - servicecomb.credentials.project
    
    # CAS_APPLICATION_NAME:
    #  - servicecomb.service.application
    # CAS_COMPONENT_NAME:
    #  - servicecomb.service.name
    # CAS_INSTANCE_VERSION:
    #  - servicecomb.service.version

    Common software packages are added to solution-basic, and the default microservice.yaml file is provided. This configuration file configures common Handlers and parameters as follows:

    # order of this configure file
    servicecomb-config-order: -100
    
    servicecomb:
    
    # handlers
      handler:
        chain:
          Provider:
            default: qps-flowcontrol-provider
          Consumer:
            default: qps-flowcontrol-consumer,loadbalance,fault-injection-consumer
    
    # loadbalance strategies
      references:
        version-rule: 0+
      loadbalance:
        retryEnabled: true
        retryOnNext: 1
        retryOnSame: 0
    
    # metrics and access log
      accesslog:
        enabled: true
      metrics:
        window_time: 60000
        invocation:
          latencyDistribution: 0,1,10,100,1000
        Consumer.invocation.slow:
          enabled: true
          msTime: 1000
        Provider.invocation.slow:
          enabled: true
          msTime: 1000
        publisher.defaultLog:
          enabled: true
          endpoints.client.detail.enabled: true

    servicecomb-config-order is set to -100 in the microservice.yaml configuration file, indicating that the priority of the configuration file is low. (A larger order indicates a higher priority. The default value is 0.) If the same configuration item is added to the service, the configuration will be overwritten.

    The microservice.yaml file may change in later versions to support the latest functions of CSE engines. If you do not want the new version to evolve with CSE engines, you can write the configuration items to your own microservice.yaml file.

  2. (Optional) Configure the AK/SK.

    Perform this step only when you use the professional microservice engine.

    The AK/SK is configured in the microservice.yaml file. ServiceComb provides plaintext configuration by default and allows you to customize the encryption storage scheme. For details about how to obtain the AK/SK and project name, see Obtaining the AK/SK and Project Name.

    • Add the following configuration in plaintext to the microservice.yaml file:
        servicecomb:  
          credentials:
            accessKey: AK #Enter the AK.
            secretKey: SK #Enter the SK.
            project: Project name #Enter the project name.
            akskCustomCipher: default
    • Implement the org.apache.servicecomb.foundation.auth.Cipher API using either of the following methods:
      • String name()

        Name definition of servicecomb.credentials.akskCustomCipher, which needs to be added to the configuration file.

      • char[] decode(char[] encrypted)

        Decrypt the API, which is used after secretKey is decrypted.

      The implementation class must be declared as SPI. For example:

      package com.example
      public class MyCipher implements Cipher

      Create an SPI configuration file. The file name and path are META-INF/serivce/org.apache.servicecomb.foundation.auth.Cipher, and the file content is as follows:

      com.example.MyCipher

      Add the following configuration to the microservice.yaml file:

      servicecomb:
        credentials:
          accessKey: AK #Enter the AK.
          secretKey: SK #Enter the encrypted SK.
          project: Project name #Enter the project name.
          akskCustomCipher: youciphername #Enter the returned name of the name() method in the implementation class.

    If you do not want to write the AK/SK into the configuration file, use either of the following environment variable methods. For details, see Method 2.

    • Use environment variables. The OS environment variable name cannot contain periods (.). The Java chassis can automatically process the environment variable servicecomb_credentials_accessKey and map it to servicecomb.credentials.accessKey.
    • Add the mapping.yaml file and customize the environment variable name. For details about this method, see the servicestage-environment module in 1.

  3. (Optional) Configure security authentication parameters.

    Perform this step only when you use the exclusive microservice engine and enable security authentication. In other scenarios, skip this step.

    After security authentication is enabled for a microservice engine, all called APIs can be called only after a token is obtained. For details about the authentication process, see RBAC.

    To use security authentication, obtain the username and password from the microservice engine and then add the following configuration to the configuration file.

    servicecomb:
      credentials:
        rbac.enabled: true
        account:
          name: your account name # Username obtained from the microservice engine
          password: your password # Password obtained from the microservice engine
        cipher: default # Returned name of the name() method in the implementation class of API org.apache.servicecomb.foundation.auth.Cipher

    cipher specifies the name of the algorithm used to encrypt the password. By default, the password is stored in plaintext. The encryption is implemented through customization. The details are as follows:

    • Implement the org.apache.servicecomb.foundation.auth.Cipher API using either of the following methods:
      • String name()

        Name definition of servicecomb.credentials.cipher, which needs to be added to the configuration file.

      • char[] decode(char[] encrypted)

        Decrypt the API, which is used after secretKey is decrypted.

      The implementation class must be declared as SPI. For example:

      package com.example
      public class MyCipher implements Cipher

      Create an SPI configuration file. The file name and path are META-INF/serivce/org.apache.servicecomb.foundation.auth.Cipher, and the file content is as follows:

      com.example.MyCipher

      Add the following configuration to the microservice.yaml file:

      servicecomb:
        credentials:
          rbac.enabled: true
          account:
            name: your account name 
            password: your password # Encrypted password
          cipher: youciphername # Returned name of the name() method in the implementation class
    • Plaintext storage cannot ensure security. You are advised to encrypt the password.
    • You can also use environment variables to configure the username and password, which is the same as configuring the AK/SK. For details, see Java Chassis.
    • If you use the professional microservice engine, the watch function is not available. You need to disable the watch function in the configuration file. Otherwise, error logs will be periodically printed. In the service center, set watch to false. In the configuration center, set refreshMode to 1.
      servicecomb:
        service:
          application: porter-application
          name: user-service
          version: 0.0.1
          registry:
            address: http://localhost:30100
            instance:
              watch: false
        config:
          client:
            serverUri: http://localhost:30103
            refreshMode: 1