Deploy RocketMQ Consumer Group
Application Scenario
Huawei Cloud Distributed Message Service RocketMQ is a highly available, highly reliable, and high-performance distributed message middleware service, widely used in distributed systems in industries such as e-commerce, finance, and IoT. Consumer groups are an important concept in RocketMQ message consumption, used to manage message consumption behavior and ensure messages can be consumed correctly and efficiently.
Through RocketMQ consumer groups, enterprises can implement message load balancing consumption, consumption progress management, consumption failure retry, and other functions, meeting message consumption requirements for different business scenarios. This best practice will introduce how to use Terraform to automatically deploy a RocketMQ consumer group, including RocketMQ instance and consumer group creation.
Related Resources/Data Sources
This best practice involves the following main resources and data sources:
Data Sources
Resources
Resource/Data Source Dependencies
Operation Steps
1. Script Preparation
Prepare the TF file (e.g., main.tf) in the specified workspace for writing the current best practice script, ensuring that it (or other TF files in the same directory) contains the provider version declaration and Huawei Cloud authentication information required for deploying resources. Refer to the "Preparation Before Deploying Huawei Cloud Resources" document for configuration introduction.
2. Query RocketMQ Availability Zone Information Through Data Source
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the query results are used to create RocketMQ instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the availability zones list query data source, only creates data source when
var.availability_zonesis empty (i.e., executes availability zones list query)
3. Query RocketMQ Flavor Information Through Data Source
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the query results are used to create RocketMQ instances:
Parameter Description:
count: Data source creation count, used to control whether to execute the flavors list query data source, only creates data source when
var.instance_flavor_idis empty (i.e., executes flavors list query)type: RocketMQ instance flavor type, prioritizes using the flavor type specified in input variables, defaults to "cluster.small" if not specified
availability_zones: Availability zones list, prioritizes using the availability zones specified in input variables, uses the first
var.availability_zones_countavailability zones from data source query results if not specifiedavailability_zones_count: Number of availability zones, prioritizes using the availability zones count specified in input variables, defaults to 1 if not specified
4. Create VPC Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a VPC resource:
Parameter Description:
name: VPC name
cidr: VPC CIDR block, prioritizes using the CIDR block specified in input variables, defaults to "192.168.0.0/16" if not specified
5. Create VPC Subnet Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a VPC subnet resource:
Parameter Description:
vpc_id: Subnet's VPC ID, references the ID of the VPC resource created earlier
name: Subnet name
cidr: Subnet CIDR block, prioritizes using the CIDR block specified in input variables, automatically calculated if not specified
gateway_ip: Subnet gateway IP, prioritizes using the gateway IP specified in input variables, automatically calculated if not specified
6. Create Security Group Resource
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a security group resource:
Parameter Description:
name: Security group name
delete_default_rules: Whether to delete default rules, set to true to delete default rules
7. Create RocketMQ Instance
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a RocketMQ instance resource:
Parameter Description:
name: RocketMQ instance name
flavor_id: RocketMQ instance flavor ID, prioritizes using the flavor specified in input variables, uses data source query results if not specified
engine_version: RocketMQ engine version, prioritizes using the version specified in input variables, uses data source query results if not specified
storage_spec_code: Storage spec code, prioritizes using the spec specified in input variables, uses data source query results if not specified
storage_space: Storage space size, prioritizes using the storage space size specified in input variables, defaults to 800GB if not specified
availability_zones: Availability zones list, prioritizes using the availability zones specified in input variables, uses data source query results if not specified
vpc_id: VPC ID, references the ID of the VPC resource created earlier
subnet_id: Subnet ID, references the ID of the subnet resource created earlier
security_group_id: Security group ID, references the ID of the security group resource created earlier
broker_num: Broker count, prioritizes using the broker count specified in input variables, defaults to 1 if not specified
description: Instance description
tags: Instance tags
enterprise_project_id: Enterprise project ID
enable_acl: Whether to enable ACL, prioritizes using the enable ACL setting specified in input variables, defaults to false if not specified
tls_mode: TLS mode, prioritizes using the TLS mode specified in input variables, defaults to "SSL" if not specified
configs: Instance configuration block
8. Query RocketMQ Broker Information Through Data Source
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to perform a data source query, the query results are used to create consumer groups:
Parameter Description:
count: Data source creation count, used to control whether to execute the Broker query data source, only creates data source when consumer group broker list is not specified and RocketMQ instance version is 4.8.0
instance_id: RocketMQ instance ID, references the ID of the RocketMQ instance resource created earlier
9. Create RocketMQ Consumer Group
Add the following script to the TF file (e.g., main.tf) to instruct Terraform to create a RocketMQ consumer group resource:
Parameter Description:
instance_id: RocketMQ instance ID, references the ID of the RocketMQ instance resource created earlier
name: Consumer group name
retry_max_times: Maximum retry times for failed consumption, prioritizes using the maximum retry times for failed consumption specified in input variables, defaults to 16 times if not specified
enabled: Whether to enable consumer group, prioritizes using the enable consumer group setting specified in input variables, defaults to true if not specified
broadcast: Whether to enable broadcast mode, prioritizes using the enable broadcast mode setting specified in input variables, defaults to false if not specified
description: Consumer group description
brokers: Broker list, prioritizes using the broker list specified in input variables, uses Broker query data source results if not specified
consume_orderly: Whether to enable orderly consumption, prioritizes using the enable orderly consumption setting specified in input variables, defaults to false if not specified, only valid when RocketMQ instance version is 5.x
10. Preset Input Parameters Required for Resource Deployment (Optional)
In this practice, some resources and data sources use input variables to assign values to configuration content. These input parameters need to be manually entered during subsequent deployments. At the same time, Terraform provides a method to preset these configurations through .tfvars files, which can avoid repeated input during each execution.
Create a terraform.tfvars file in the working directory with the following example content:
Usage:
Save the above content as
terraform.tfvarsfile in the working directory (this file name allows users to automatically import the content of thistfvarsfile when executing terraform commands; for other names,.autoneeds to be added before tfvars, such asvariables.auto.tfvars)Modify parameter values as needed
When executing
terraform planorterraform apply, Terraform will automatically read the variable values from this file
In addition to using terraform.tfvars file, variable values can also be set in the following ways:
Command line parameters:
terraform apply -var="vpc_name=my-vpc" -var="subnet_name=my-subnet"Environment variables:
export TF_VAR_vpc_name=my-vpcCustom named variable files:
terraform apply -var-file="custom.tfvars"
Note: If the same variable is set in multiple ways, Terraform will use the variable value according to the following priority: command line parameters > variable files > environment variables > default values.
11. Initialize and Apply Terraform Configuration
After completing the above script configuration, execute the following steps to create resources:
Run
terraform initto initialize the environmentRun
terraform planto view the resource creation planAfter confirming the resource plan is correct, run
terraform applyto start creating the RocketMQ consumer groupRun
terraform showto view the details of the created RocketMQ consumer group
Reference Information
Last updated