Help Center > > Developer Guide> Microservice Development Guide> Connecting Microservice Applications to CSE> Connecting Java Chassis Applications to CSE

Connecting Java Chassis Applications to CSE

Updated at: Sep 17, 2021 GMT+08:00

This section describes how to connect Java chassis applications to CSE and use the most common functions of CSE. For details about the development guide, see Using CSE 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 requirement: Java chassis 2.3.0 or later.
  • 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>

    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. If you do not want the new version to evolve with the CSE, you can add the mapping.yaml file to your project instead of adding the servicestage-environment dependency.

    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. If you do not want the new version to evolve with the CSE, 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.

    • Add the following configuration in plaintext to the microservice.yaml file:
        servicecomb:  
          credentials:
            accessKey: AK
            secretKey: SK
            project: 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/serivces/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  
          secretKey: SK # SK information in ciphertext
            project: project name
          akskCustomCipher: youciphername # 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 methods. For details, see Java Chassis.

    • 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.

    Similar to the encryption scheme of AK/SK authentication, API org.apache.servicecomb.foundation.auth.Cipher can be used to encrypt passwords for storage.

    • 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:30113
            refreshMode: 1

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel