All Products
Search
Document Center

Microservices Engine:Implement agile development of microservice applications

Last Updated:Feb 22, 2024

This topic describes the best practices for implementing agile development of microservice applications when your on-premises network is not connected to Alibaba Cloud.

Background information

Existing issues

Development and test engineers who perform agile development of microservice applications may encounter the following issues:

  • When development engineers develop an interface, response data from other interfaces is required. As a result, joint debugging and testing cannot be separately performed for the developed interface. In this case, an environment in which all the involved applications can run is required.

  • After the development engineers deploy the developed interface in an environment, the joint debugging or testing result does not meet expectations. The troubleshooting results such as log analysis results show that the code of the required applications is changed. The code change results in changes to the original logic.

  • Multiple development engineers develop multiple applications in parallel. During joint debugging and testing, only one application can be debugged or tested at a time. All applications must be debugged or tested in sequence.

  • An error is reported during the joint debugging and testing of an application interface. During the same period of time, errors are also reported for the other interfaces that are being debugged or tested.

Solutions

Traffic flows in feature environments are essential and provide the basis for agile development of microservice applications.

Microservices facilitate agile development but make the development environment complex. To prevent applications from interfering with each other, you must make sure that the complete closed-loop traffic processing logic is implemented among the applications. Microservices Engine (MSE) allows you to enable Microservices Governance for applications. After you deploy applications in an environment and configure tag-based routing for the applications, precise traffic throttling can be implemented on the applications. This way, you can benefit from the agile development of microservice applications and implement routine construction of development environments at a low cost.

  • Solution 1: Provide an exclusive environment for each iteration or feature.

    All applications of a microservice application set run in this exclusive environment. The applications include the registries and access-layer applications.

    The calculation method that is used for this solution shows that the costs linearly increase with the number of applications and environments that are deployed.

    • Advantage: This solution is easy to implement.

    • Disadvantage: The cost is relatively high.

  • Solution 2: Use the MSE tag-based routing feature to isolate development environments.

    Multiple environments are available and a complete set of microservice applications run in each environment. For the purposes of cost reduction, some application nodes are shared by multiple environments. This way, you need to maintain only a base environment. In the base environment, all the involved microservice applications and related entities such as service registries, DNS systems, Server Load Balancer (SLB) instances, and gateways are deployed. To build a feature environment, you need to deploy only the modified applications that are involved in this feature. You do not need to deploy a set of microservice applications and related entities in each feature environment.

    The cost for maintaining N feature environments is calculated by using the following formula: Fees for N feature environments + Fees for M application nodes in each feature environment. Unlike Solution 1, Solution 2 allows you to build feature environments without extra costs. This way, exclusive feature environments can be provided for each development or test engineer.

In this example, your on-premises network and virtual private clouds (VPCs) in Alibaba Cloud are not connected over Express Connect circuits. Applications deployed in your on-premises network need to connect to the development and test clusters in Alibaba Cloud, and precise traffic isolation is required. In the following typical scenarios, you can implement Solution 2 based on the basic access method and the tag-based routing feature of MSE.

  • All applications in development environments are deployed in on-premises data centers. In this case, all involved development environments are self-managed or are deployed in self-managed data centers that are connected over an on-premises network. Each development team requires an exclusive development environment to prevent the applications from interfering with each other.

  • Your on-premises network and Alibaba Cloud are connected over Express Connect circuits. In this case, your on-premises network is connected to VPCs in Alibaba Cloud. Most development and test environments are deployed in Alibaba Cloud, but some ongoing projects remain deployed in your on-premises network or the personal computers of development engineers. An exclusive environment is required to perform joint debugging and testing.

Prerequisites

Step 1: Enable Microservices Governance for applications

Enable Microservices Governance for microservice applications in the ACK cluster. For more information, see Enable Microservices Governance for microservice applications in an ACK cluster.

Step 2: Deploy applications and perform verification

To simulate a real call process, deploy the nacos-server application and four business applications: spring-cloud-zuul, spring-cloud-a, spring-cloud-b, and spring-cloud-c. The nacos-server application is deployed as a registry.

You can obtain the source code of this demo from alibabacloud-microservices-demo.

The following figure shows the architecture of this demo. Calls in this demo involve calls of applications that are developed based on Spring Cloud and Dubbo, the two most common microservices frameworks. The applications involved comply with the standard use of Spring Cloud and Dubbo.部署应用

  1. In the left-side navigation pane of the ACK console, click Clusters.

  2. On the Clusters page, find the cluster that you want to manage and click the name of the cluster or click Details in the Actions column. The details page of the cluster appears.

  3. In the left-side navigation pane of the details page, choose Workloads > Deployments.

  4. On the Deployments page, select a namespace and click Create from YAML.

    The following YAML files provide the sample configuration that is used to deploy applications in the ACK console.

    Note

    To implement the interconnection between on-premises and cloud applications, you must use an MSE Nacos instance as a registry. Therefore, you must set spring.cloud.nacos.discovery.server-addr and dubbo.registry.address in the YAML files for the applications to the endpoint of the MSE Nacos instance that you purchased. If a public endpoint of the MSE Nacos instance is used, you must also make sure that an IP address whitelist is configured for the public endpoint.

    Show code

    # Deploy business applications.
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-cloud-zuul
    spec:
      selector:
        matchLabels:
          app: spring-cloud-zuul
      template:
        metadata:
          labels:
            app: spring-cloud-zuul
            msePilotCreateAppName: spring-cloud-zuul
        spec:
          containers:
            - env:
                - name: JAVA_HOME
                  value: /usr/lib/jvm/java-1.8-openjdk/jre
                -name: spring.cloud.nacos.discovery.server-addr //The endpoint of the MSE Nacos instance.
                  value: 'mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
              image: registry.cn-shanghai.aliyuncs.com/yizhan/spring-cloud-zuul:1.0.0
              imagePullPolicy: Always
              name: spring-cloud-zuul
              ports:
                - containerPort: 20000
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        service.beta.kubernetes.io/alibaba-cloud-loadbalancer-spec: slb.s1.small
        service.beta.kubernetes.io/alicloud-loadbalancer-address-type: internet
      name: zuul-slb
    spec:
      ports:
        - port: 80
          protocol: TCP
          targetPort: 20000
      selector:
        app: spring-cloud-zuul
      type: LoadBalancer
    status:
      loadBalancer: {}
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-cloud-a
    spec:
      selector:
        matchLabels:
          app: spring-cloud-a
      template:
        metadata:  
          labels:
            app: spring-cloud-a
            msePilotCreateAppName: spring-cloud-a
        spec:
          containers:
            - env:
                - name: JAVA_HOME
                  value: /usr/lib/jvm/java-1.8-openjdk/jre
                - name: spring.cloud.nacos.discovery.server-addr
                  value: 'mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
                - name: dubbo.registry.address
                  value: 'nacos://mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
              image: registry.cn-shanghai.aliyuncs.com/yizhan/spring-cloud-a:1.0.0
              imagePullPolicy: Always
              name: spring-cloud-a
              ports:
                - containerPort: 20001
              livenessProbe:
                tcpSocket:
                  port: 20001
                initialDelaySeconds: 10
                periodSeconds: 30
    
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-cloud-b
    spec:
      selector:
        matchLabels:
          app: spring-cloud-b
      template:
        metadata:
          labels:
            app: spring-cloud-b
            msePilotCreateAppName: spring-cloud-b
        spec:
          containers:
            - env:
                - name: JAVA_HOME
                  value: /usr/lib/jvm/java-1.8-openjdk/jre
                - name: spring.cloud.nacos.discovery.server-addr
                  value: 'mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
                - name: dubbo.registry.address
                  value: 'nacos://mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
              image: registry.cn-shanghai.aliyuncs.com/yizhan/spring-cloud-b:1.0.0
              imagePullPolicy: Always
              name: spring-cloud-b
              ports:
                - containerPort: 20002
              livenessProbe:
                tcpSocket:
                  port: 20002
                initialDelaySeconds: 10
                periodSeconds: 30
    
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-cloud-c
    spec:
      selector:
        matchLabels:
          app: spring-cloud-c
      template:
        metadata:
          labels:
            app: spring-cloud-c
            msePilotCreateAppName: spring-cloud-c
        spec:
          containers:
            - env:
                - name: JAVA_HOME
                  value: /usr/lib/jvm/java-1.8-openjdk/jre
                - name: spring.cloud.nacos.discovery.server-addr
                  value: 'mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
                - name: dubbo.registry.address
                  value: 'nacos://mse-xxxxxxx-nacos-ans.mse.aliyuncs.com:8848'
              image: registry.cn-shanghai.aliyuncs.com/yizhan/spring-cloud-c:1.0.0
              imagePullPolicy: Always
              name: spring-cloud-c
              ports:
                - containerPort: 20003
              livenessProbe:
                tcpSocket:
                  port: 20003
                initialDelaySeconds: 10
                periodSeconds: 30
  5. On the Applications page in the MSE console, select the required region, and then click the name of an application or click Manage in the Actions column for the application. On the application details page, view the nodes that belong to this application.

    • After you configure the kubeconfig file for the ACK cluster in your on-premises machine, run the following command:

      kubectl get svc,deploy

      Expected output:

      NAME                 TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
      service/kubernetes   ClusterIP      192.168.xx.xx     <none>         4xx/TCP        7d
      service/zuul-slb     LoadBalancer   192.168.xx.xx     47.94.xx.xx     80:319xx/TCP   9m30s
      
      NAME                                READY   UP-TO-DATE   AVAILABLE   AGE
      deployment.apps/spring-cloud-a      1/1     1            1           9m30s
      deployment.apps/spring-cloud-b      1/1     1            1           9m30s
      deployment.apps/spring-cloud-c      1/1     1            1           9m30s
      deployment.apps/spring-cloud-zuul   1/1     1            1           9m30s
    • Run the curl http://47.94.xx.xx/A/a command to initiate a call.

      curl http://47.94.xx.xx /A/a

      Expected output:

      A[10.242.xx.xx] -> B[10.242.xx.xx] -> C[10.242.xx.xx]%

Step 3: Connect an application that is started by using IntelliJ IDEA to the development environment

This step demonstrates how to connect an on-premises application to the development environment when your on-premises network is not connected to Alibaba Cloud. You must first save the kubeconfig file from your ACK cluster to your on-premises machine.

  1. Download the source code. You can obtain all source code that is used in this demo from Demo. Copy the code to your on-premises machine by using the Git command. Find the source code for the gateway application and Applications A, B, and C that are used in this demo in the mse-simple-demo folder.

  2. Install Alibaba Cloud Toolkit. For more information about the latest version of Alibaba Cloud Toolkit, visit Cloud Toolkit.

  3. In the top navigation bar of the IntelliJ IDEA application, choose Tools > Alibaba Cloud > Preferences and enter the AccessKey ID and AccessKey secret.

    Note

    To establish connections between on-premises applications and Alibaba Cloud, you must enter the AccessKey ID and AccessKey secret of your Alibaba Cloud account that has the permissions to manage MSE resources.

  4. In the left-side navigation pane of the Preferences page, choose Alibaba Cloud Toolkit > Accounts. Enter your AccessKey ID and AccessKey secret, and click OK.

  5. In the left-side navigation pane of the Preferences page, choose Alibaba Cloud Toolkit > Microservice > MSE. In the right-side pane, select Activate Microservices Governance, and configure the parameters.

    Parameter

    Description

    LicenseKey

    The MSE license key that is assigned to your Alibaba Cloud account. You can view the license key on the Overview page in the MSE console.

    image

    Note

    We recommend that you keep this license key confidential. The license key may be different for each region. Select the region in which the development environment resides.

    App Name

    The application name that you use to connect the application to MSE. Configure this parameter based on your business requirements.

    Note

    The application must have the same name as the application you currently use.

    Tag

    The tag of the environment in which the application runs. You do not need to configure tags for base environments. Configure this parameter for other environments based on your business requirements. If the application runs in the feature1 environment, enter feature1 in the field.

    Agent Address

    The region in which the application runs. Select the region that corresponds to the license key and houses the base environment.

    Enable RPC Canary Release

    Specifies whether to implement precise traffic throttling on the Spring Cloud and Dubbo applications. By default, this option is enabled. Do not disable it unless you know when to disable it.

    Enable Label-based Dyeing

    Specifies whether to enable tag-based dyeing. We recommend that you enable tag-based dyeing so that traffic that passes through an application is routed to downstream environments that have the same tag.

    Enable Message Canary Release

    Specifies whether to enable the message canary release feature. You can enable or disable the message canary release feature based on your business requirements. This feature is supported only for messages of RocketMQ 4.5 or later. For more information about the canary release for messaging, see Step 3: Enable the RocketMQ-based canary release feature for applications.

  6. Configure the parameters for implementing the interconnection between on-premises and cloud applications.

    1. Specify a Kubernetes proxy. In the left-side navigation pane of the Preferences page, choose Alibaba Cloud Toolkit > Microservice > Proxy. Click AddProfile to add a proxy named K8s.

    2. Click Add, set Proxy Type to Kubernetes, and then configure the File Path and Namespace parameters.

    3. In the left-side navigation pane of the Preferences page, choose Alibaba Cloud Toolkit > Microservice. Find and select Interconnection between On-premises Applications and Alibaba Cloud, and configure the related parameters. Select Microservices Engine (MSE) from the Product drop-down list, configure the Region, Instance, and Namespace parameters to the same values that you configured when the application is deployed, and select k8s from the Proxy drop-down list. If the application is a Spring Cloud application, specify the Tomcat port for the application.

Step 4: Start the application and perform joint debugging

Check whether the application is connected to the environment.

  1. Start the application in IntelliJ IDEA. If a message, indicating that the interconnection is successful, appears, the interconnection is enabled.

  2. Log on to the MSE console. In the left-side navigation pane, choose Microservices Governance > Applications to view the details of the application.

    In this demo, the on-premises application is connected to MSE and is tagged with feature1.

Step 5: Initiate traffic calls for testing

If you send an HTTP request to the gateway and the request must remain within environments that have a specified tag such as feature1, you must add x-mse-tag=[feature1] to the request header. Then, traffic can flow into the environments that are tagged with feature1. Note that the value of x-mse-tag is fixed. The value must be the same as the value of alicloud.service.tag that is configured in the preceding step.

If you send a Dubbo request, you must specify x-mse-tag=[feature1] in Attachment of RpcContext.

After the environment tag is configured, use the curl commands to initiate calls and view the outputs.

  • Run the following curl command:

    curl http://47.94.143.xx:80/A/a

    Returned output:

    A[10.242.xx.90] -> B[10.242.xx.91] -> C[10.242.xx.152]%
  • Run the following curl command:

    curl -H"x-mse-tag:feature1" http://47.94.143.xx:80/A/a

    Returned output:

    Afeature1[xx.xx.12.118] -> B[10.242.xx.91] -> C[10.242.xx.152]%

You can also add a routing rule in the MSE console. For example, you can specify that only traffic with the name parameter set to xiaoming can be sent to the environments tagged with feature1.

  1. Log on to the MSE console.

  2. In the left-side navigation pane, choose Microservices Governance > Application Governance. On the page that appears, click the resource card of the spring-cloud-a application.

  3. In the left-side navigation pane, click Traffic management. On the Label routing tab, find the application that has the feature1 tag and click Add in the Traffic rules column.

  4. In the Create label route panel, configure the parameters and click Determine.

    In this example, the name parameter is set to xiaoming for the routing rule.

    For more information about how to add a routing rule, see Configure tag-based routing.

    After the traffic routing rule takes effect, you can view the traffic distribution on the Application overview page.

  5. After you add the routing rule, run the curl commands to initiate calls and view the outputs.

    • Run the following curl command:

      curl http://47.94.xx.xx:80/A/a

      Returned output:

      A[10.242.0.xx] -> B[10.242.0.xx] -> C[10.242.0.xx]%
    • Run the following curl command:

      curl http://47.94.xx.xx:80/A/a\?name\=xiaoming

      Returned output:

      Afeature1[30.225.12.xx] -> B[10.242.0.xx] -> C[10.242.0.xx]%
    • Run the following curl command:

      curl http://47.94.xx.xx:80/A/a\?name\=xiaoming

      Returned output:

      Afeature1[30.225.12.xx] -> B[10.242.0.xx] -> C[10.242.0.xx]%

If your gateway application is a non-Java application, you must configure routing rules at the gateway level. You can configure routing rules for gateways such as MSE cloud-native gateways, NGINX Ingress gateways, and Kubernetes Ingress gateways. For information about the connection methods, see the related topics such as Implement an end-to-end canary release by using MSE cloud-native gateways and Implement an end-to-end canary release based on NGINX Ingress gateways.