Skip to main content

Tencent Cloud Lighthouse Lightweight Instance Access

Prerequisites

  • Lightweight Instance service in the Shanghai region has been created and configured.
  • Start the service and listen on a specific port within the host.

Lightweight instances that meet the above conditions can be integrated via the AnyService, and can subsequently be accessed by Mini Programs to request services running within the instances.

Illustrate with an example

Here we select the Node.js application template for demonstration. You can create the Lightweight Instance service using any method.

  1. Through the Lightweight Instance creation panel, select the application template with Nodejs and choose the Shanghai region to create.

  2. After creation is complete, allow port 80 on the host through the firewall.

alt

  1. Use curl or access the host ip via a browser to ensure the service within the host is accessible:
curl http://xxx.xxx.xx.xxx    
Hello World%

Create Service

  1. Go to CloudBase/AnyService

  2. Click Create New Service Connection and fill in the relevant information according to the form requirements:

  • Service Name: Integrate with Lightweight Instances
  • Service Identifier: lh
  • Origin Type: Tencent Cloud Lighthouse Lightweight Instance
  • Origin Connection Information: Select the Lightweight Instance created in the prerequisites; configure the port to 80

After completing the form, confirm and save to finalize the service connection creation.

Mini Program Request Validation

The env parameter in the wx.cloud.init initialization for Mini Programs must match the environment where the anyService is created.

Add the following code in the Mini Program:

// Initialize the environment before invocation or in the Mini Program's onLaunch
wx.cloud.init({
env: 'test-xxxxx',
traceUser: true,
})

const result = await wx.cloud.callContainer({
"path": "/",
"header": {
"X-WX-SERVICE": "tcbanyservice", // Set to tcbanyservice
"X-AnyService-Name": "lh", // Fill in the AnyService service identifier in "X-AnyService-Name". Obtain it from "Tencent Cloud Development Platform - AnyService"
"content-type": "application/json",
},
"method": "GET",
})
console.log(result);

The example running result will be able to retrieve and output the same "Hello World" content.

alt