Skip to content

Commit e6db2b3

Browse files
yorklolshanye997
authored andcommitted
rocketmq-data-consistency
1 parent 66e6449 commit e6db2b3

File tree

4 files changed

+444
-0
lines changed

4 files changed

+444
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Introduction
2+
3+
<!-- DOCS_DESCRIPTION_CN -->
4+
本示例用于实现解决方案[通过RocketMQ事务消息实现分布式事务](https://www.aliyun.com/solution/tech-solution/rocketmq-data-consistency), 涉及到专有网络(VPC)、交换机(VSwitch)、云服务器(ECS)、RAM 用户等资源的创建。
5+
<!-- DOCS_DESCRIPTION_CN -->
6+
7+
<!-- DOCS_DESCRIPTION_EN -->
8+
This example is used to implement solution [RocketMQ Data Consistency](https://www.aliyun.com/solution/tech-solution/rocketmq-data-consistency). It involves the creation, and deployment of resources such as Virtual Private Cloud (VPC), VSwitch, Elastic Compute Service (ECS), and RAM users.
9+
<!-- DOCS_DESCRIPTION_EN -->
10+
11+
<!-- BEGIN_TF_DOCS -->
12+
## Providers
13+
14+
| Name | Version |
15+
|------|---------|
16+
| <a name="provider_alicloud"></a> [alicloud](#provider\_alicloud) | n/a |
17+
| <a name="provider_random"></a> [random](#provider\_random) | n/a |
18+
19+
## Modules
20+
21+
No modules.
22+
23+
## Resources
24+
25+
| Name | Type |
26+
|------|------|
27+
| [alicloud_db_account_privilege.account_privilege](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/db_account_privilege) | resource |
28+
| [alicloud_db_database.rds_database](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/db_database) | resource |
29+
| [alicloud_db_instance.rds_instance](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/db_instance) | resource |
30+
| [alicloud_ecs_command.run_command_consumer](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_command) | resource |
31+
| [alicloud_ecs_command.run_command_provider](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_command) | resource |
32+
| [alicloud_ecs_invocation.invoke_script_consumer](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_invocation) | resource |
33+
| [alicloud_ecs_invocation.invoke_script_provider](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ecs_invocation) | resource |
34+
| [alicloud_instance.ecs_instance_consumer](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/instance) | resource |
35+
| [alicloud_instance.ecs_instance_provider](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/instance) | resource |
36+
| [alicloud_ram_access_key.ramak](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ram_access_key) | resource |
37+
| [alicloud_ram_user.ram_user](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ram_user) | resource |
38+
| [alicloud_ram_user_policy_attachment.attach_policy_to_user](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/ram_user_policy_attachment) | resource |
39+
| [alicloud_rds_account.rds_account](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rds_account) | resource |
40+
| [alicloud_rocketmq_account.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rocketmq_account) | resource |
41+
| [alicloud_rocketmq_acl.consumer_group](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rocketmq_acl) | resource |
42+
| [alicloud_rocketmq_acl.topic1](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rocketmq_acl) | resource |
43+
| [alicloud_rocketmq_consumer_group.consumer_group](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rocketmq_consumer_group) | resource |
44+
| [alicloud_rocketmq_instance.rocketmq](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rocketmq_instance) | resource |
45+
| [alicloud_rocketmq_topic.topic1](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/rocketmq_topic) | resource |
46+
| [alicloud_security_group.security_group](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/security_group) | resource |
47+
| [alicloud_security_group_rule.allow_ssh](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/security_group_rule) | resource |
48+
| [alicloud_security_group_rule.allow_web](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/security_group_rule) | resource |
49+
| [alicloud_vpc.vpc](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/vpc) | resource |
50+
| [alicloud_vswitch.ecs_vswitch](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/vswitch) | resource |
51+
| [alicloud_vswitch.rds_vswitch](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/resources/vswitch) | resource |
52+
| [random_string.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
53+
| [alicloud_db_zones.rds_zones](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/db_zones) | data source |
54+
| [alicloud_images.default](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/images) | data source |
55+
| [alicloud_zones.ecs_zones](https://registry.terraform.io/providers/hashicorp/alicloud/latest/docs/data-sources/zones) | data source |
56+
57+
## Inputs
58+
59+
| Name | Description | Type | Default | Required |
60+
|------|-------------|------|---------|:--------:|
61+
| <a name="input_app_demo_password"></a> [app\_demo\_password](#input\_app\_demo\_password) | 请输入登录应用演示账户的密码。密码长度为8-32位,需包含大写字母、小写字母、数字和特殊字符(如:!@#$%^&*()\_+-=)。 | `string` | n/a | yes |
62+
| <a name="input_app_demo_username"></a> [app\_demo\_username](#input\_app\_demo\_username) | 请输入登录应用演示账户的用户名。用户名长度为4-16位,只能包含字母、数字和下划线。 | `string` | `"appuser"` | no |
63+
| <a name="input_db_account_name"></a> [db\_account\_name](#input\_db\_account\_name) | RDS数据库账号 | `string` | `"db_normal_account"` | no |
64+
| <a name="input_db_instance_type"></a> [db\_instance\_type](#input\_db\_instance\_type) | RDS实例规格 | `string` | `"mysql.n2.medium.1"` | no |
65+
| <a name="input_db_password"></a> [db\_password](#input\_db\_password) | 请输入RDS数据库密码。密码长度为8-32位,需包含大写字母、小写字母、数字和特殊字符(如:!@#$%^&*()\_+-=)。如果在本教程中重复配置,请确保 MySQL 数据库密码与模板首次执行时设置的密码完全相同,否则配置结果不可用。 | `string` | n/a | yes |
66+
| <a name="input_ecs_instance_password"></a> [ecs\_instance\_password](#input\_ecs\_instance\_password) | 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)` | `string` | n/a | yes |
67+
| <a name="input_ecs_instance_type"></a> [ecs\_instance\_type](#input\_ecs\_instance\_type) | ECS实例规格 | `string` | `"ecs.t6-c1m2.large"` | no |
68+
| <a name="input_region"></a> [region](#input\_region) | n/a | `string` | `"cn-hangzhou"` | no |
69+
| <a name="input_rocketmq_password"></a> [rocketmq\_password](#input\_rocketmq\_password) | 请输入RocketMQ密码。密码长度为8-32位,需包含大写字母、小写字母、数字和特殊字符(如:!@#$%^&*()\_+-=)。 | `string` | n/a | yes |
70+
| <a name="input_rocketmq_username"></a> [rocketmq\_username](#input\_rocketmq\_username) | 请输入RocketMQ用户名。用户名长度为4-16位,只能包含字母、数字和下划线。 | `string` | `"rmquser"` | no |
71+
<!-- END_TF_DOCS -->
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
provider "alicloud" {
2+
region = var.region
3+
}
4+
5+
data "alicloud_zones" "ecs_zones" {
6+
available_disk_category = "cloud_essd"
7+
available_resource_creation = "VSwitch"
8+
available_instance_type = var.ecs_instance_type
9+
}
10+
11+
data "alicloud_db_zones" "rds_zones" {
12+
engine = "MySQL"
13+
engine_version = "8.0"
14+
instance_charge_type = "PostPaid"
15+
category = "Basic"
16+
db_instance_storage_type = "cloud_essd"
17+
}
18+
19+
resource "random_string" "suffix" {
20+
length = 8
21+
lower = true
22+
upper = false
23+
numeric = false
24+
special = false
25+
}
26+
27+
locals {
28+
common_name = random_string.suffix.id
29+
}
30+
31+
resource "alicloud_vpc" "vpc" {
32+
cidr_block = "192.168.0.0/16"
33+
vpc_name = "vpc-${local.common_name}"
34+
}
35+
36+
resource "alicloud_vswitch" "ecs_vswitch" {
37+
vpc_id = alicloud_vpc.vpc.id
38+
cidr_block = "192.168.1.0/24"
39+
zone_id = data.alicloud_zones.ecs_zones.zones[0].id
40+
vswitch_name = "ecs-vswitch-${local.common_name}"
41+
}
42+
43+
resource "alicloud_vswitch" "rds_vswitch" {
44+
vpc_id = alicloud_vpc.vpc.id
45+
cidr_block = "192.168.2.0/24"
46+
zone_id = data.alicloud_db_zones.rds_zones.zones[0].id
47+
vswitch_name = "rds-vswitch-${local.common_name}"
48+
}
49+
50+
resource "alicloud_security_group" "security_group" {
51+
vpc_id = alicloud_vpc.vpc.id
52+
security_group_name = "sg-${local.common_name}"
53+
}
54+
55+
resource "alicloud_security_group_rule" "allow_ssh" {
56+
type = "ingress"
57+
ip_protocol = "tcp"
58+
nic_type = "intranet"
59+
policy = "accept"
60+
port_range = "22/22"
61+
priority = 1
62+
security_group_id = alicloud_security_group.security_group.id
63+
cidr_ip = "0.0.0.0/0"
64+
}
65+
66+
resource "alicloud_security_group_rule" "allow_web" {
67+
type = "ingress"
68+
ip_protocol = "tcp"
69+
nic_type = "intranet"
70+
policy = "accept"
71+
port_range = "80/80"
72+
priority = 1
73+
security_group_id = alicloud_security_group.security_group.id
74+
cidr_ip = "0.0.0.0/0"
75+
}
76+
77+
data "alicloud_images" "default" {
78+
name_regex = "^aliyun_3_x64_20G_alibase_.*"
79+
most_recent = true
80+
owners = "system"
81+
}
82+
83+
resource "alicloud_ram_user" "ram_user" {
84+
name = "create_by_solution-${local.common_name}"
85+
}
86+
87+
resource "alicloud_ram_access_key" "ramak" {
88+
user_name = alicloud_ram_user.ram_user.name
89+
}
90+
91+
resource "alicloud_ram_user_policy_attachment" "attach_policy_to_user" {
92+
user_name = alicloud_ram_user.ram_user.name
93+
policy_type = "System"
94+
policy_name = "AliyunLogFullAccess"
95+
}
96+
97+
resource "alicloud_instance" "ecs_instance_provider" {
98+
instance_name = "ecs-${local.common_name}"
99+
image_id = data.alicloud_images.default.images[0].id
100+
instance_type = var.ecs_instance_type
101+
system_disk_category = "cloud_essd"
102+
security_groups = [alicloud_security_group.security_group.id]
103+
vswitch_id = alicloud_vswitch.ecs_vswitch.id
104+
password = var.ecs_instance_password
105+
internet_max_bandwidth_out = 5
106+
}
107+
108+
resource "alicloud_ecs_command" "run_command_provider" {
109+
name = "command-1-${local.common_name}"
110+
command_content = base64encode(<<EOF
111+
cat << EOT >> ~/.bash_profile
112+
export MYSQL_HOST=${alicloud_db_instance.rds_instance.connection_string}
113+
export MYSQL_DB=testrmq
114+
export MYSQL_USER=${var.db_account_name}
115+
export MYSQL_PASSWORD=${var.db_password}
116+
export APP_DEMO_ROCKETMQ_ENDPOINT=${alicloud_rocketmq_instance.rocketmq.network_info[0].endpoints[0].endpoint_url}
117+
export APP_DEMO_ROCKETMQ_USERNAME=${var.rocketmq_username}
118+
export APP_DEMO_ROCKETMQ_PASSWORD=${var.rocketmq_password}
119+
export APP_DEMO_USERNAME=${var.app_demo_username}
120+
export APP_DEMO_PASSWORD=${var.app_demo_password}
121+
export IS_CONSUMER=false
122+
123+
EOT
124+
125+
source ~/.bash_profile
126+
127+
curl -fsSL https://help-static-aliyun-doc.aliyuncs.com/install-script/rocketmq-transaction/install.sh|bash
128+
EOF
129+
)
130+
working_dir = "/root"
131+
type = "RunShellScript"
132+
timeout = 3600
133+
}
134+
135+
resource "alicloud_ecs_invocation" "invoke_script_provider" {
136+
instance_id = [alicloud_instance.ecs_instance_provider.id]
137+
command_id = alicloud_ecs_command.run_command_provider.id
138+
timeouts {
139+
create = "15m"
140+
}
141+
depends_on = [
142+
alicloud_db_instance.rds_instance,
143+
alicloud_db_database.rds_database,
144+
alicloud_rocketmq_instance.rocketmq,
145+
alicloud_rocketmq_acl.topic1,
146+
alicloud_rocketmq_acl.consumer_group,
147+
]
148+
}
149+
150+
151+
resource "alicloud_instance" "ecs_instance_consumer" {
152+
instance_name = "ecs-${local.common_name}"
153+
image_id = data.alicloud_images.default.images[0].id
154+
instance_type = var.ecs_instance_type
155+
system_disk_category = "cloud_essd"
156+
security_groups = [alicloud_security_group.security_group.id]
157+
vswitch_id = alicloud_vswitch.ecs_vswitch.id
158+
password = var.ecs_instance_password
159+
internet_max_bandwidth_out = 5
160+
}
161+
162+
resource "alicloud_ecs_command" "run_command_consumer" {
163+
name = "command-1-${local.common_name}"
164+
command_content = base64encode(<<EOF
165+
cat << EOT >> ~/.bash_profile
166+
export MYSQL_HOST=${alicloud_db_instance.rds_instance.connection_string}
167+
export MYSQL_DB=testrmq
168+
export MYSQL_USER=${var.db_account_name}
169+
export MYSQL_PASSWORD=${var.db_password}
170+
export APP_DEMO_ROCKETMQ_ENDPOINT=${alicloud_rocketmq_instance.rocketmq.network_info[0].endpoints[0].endpoint_url}
171+
export APP_DEMO_ROCKETMQ_USERNAME=${var.rocketmq_username}
172+
export APP_DEMO_ROCKETMQ_PASSWORD=${var.rocketmq_password}
173+
export APP_DEMO_USERNAME=${var.app_demo_username}
174+
export APP_DEMO_PASSWORD=${var.app_demo_password}
175+
export IS_CONSUMER=true
176+
177+
EOT
178+
179+
source ~/.bash_profile
180+
181+
curl -fsSL https://help-static-aliyun-doc.aliyuncs.com/install-script/rocketmq-transaction/install.sh|bash
182+
EOF
183+
)
184+
working_dir = "/root"
185+
type = "RunShellScript"
186+
timeout = 3600
187+
}
188+
189+
resource "alicloud_ecs_invocation" "invoke_script_consumer" {
190+
instance_id = [alicloud_instance.ecs_instance_consumer.id]
191+
command_id = alicloud_ecs_command.run_command_consumer.id
192+
timeouts {
193+
create = "15m"
194+
}
195+
depends_on = [
196+
alicloud_db_instance.rds_instance,
197+
alicloud_db_database.rds_database,
198+
alicloud_rocketmq_instance.rocketmq,
199+
alicloud_rocketmq_acl.topic1,
200+
alicloud_rocketmq_acl.consumer_group,
201+
]
202+
}
203+
204+
resource "alicloud_db_instance" "rds_instance" {
205+
instance_type = var.db_instance_type
206+
zone_id = data.alicloud_db_zones.rds_zones.zones[0].id
207+
instance_storage = 50
208+
category = "Basic"
209+
db_instance_storage_type = "cloud_essd"
210+
vswitch_id = alicloud_vswitch.rds_vswitch.id
211+
engine = "MySQL"
212+
vpc_id = alicloud_vpc.vpc.id
213+
engine_version = "8.0"
214+
security_ips = ["192.168.0.0/16"]
215+
}
216+
217+
resource "alicloud_rds_account" "rds_account" {
218+
db_instance_id = alicloud_db_instance.rds_instance.id
219+
account_type = "Normal"
220+
account_name = var.db_account_name
221+
account_password = var.db_password
222+
}
223+
224+
resource "alicloud_db_database" "rds_database" {
225+
character_set = "utf8"
226+
instance_id = alicloud_db_instance.rds_instance.id
227+
name = "testrmq"
228+
}
229+
230+
resource "alicloud_db_account_privilege" "account_privilege" {
231+
privilege = "ReadWrite"
232+
instance_id = alicloud_db_instance.rds_instance.id
233+
account_name = alicloud_rds_account.rds_account.account_name
234+
db_names = [alicloud_db_database.rds_database.name]
235+
}
236+
237+
resource "alicloud_rocketmq_instance" "rocketmq" {
238+
product_info {
239+
msg_process_spec = "rmq.s2.2xlarge"
240+
message_retention_time = "70"
241+
}
242+
243+
sub_series_code = "cluster_ha"
244+
series_code = "standard"
245+
payment_type = "PayAsYouGo"
246+
instance_name = "ROCKETMQ5-${local.common_name}"
247+
service_code = "rmq"
248+
249+
network_info {
250+
vpc_info {
251+
vpc_id = alicloud_vpc.vpc.id
252+
vswitches {
253+
vswitch_id = alicloud_vswitch.ecs_vswitch.id
254+
}
255+
}
256+
internet_info {
257+
internet_spec = "disable"
258+
flow_out_type = "uninvolved"
259+
}
260+
}
261+
acl_info {
262+
acl_types = ["default", "apache_acl"]
263+
default_vpc_auth_free = false
264+
}
265+
266+
}
267+
268+
resource "alicloud_rocketmq_account" "default" {
269+
account_status = "ENABLE"
270+
instance_id = alicloud_rocketmq_instance.rocketmq.id
271+
username = var.rocketmq_username
272+
password = var.rocketmq_password
273+
}
274+
275+
resource "alicloud_rocketmq_topic" "topic1" {
276+
instance_id = alicloud_rocketmq_instance.rocketmq.id
277+
remark = "用于存储和传输订单相关的业务消息"
278+
message_type = "TRANSACTION"
279+
topic_name = "ROCKETMQ_ORDER_TOPIC"
280+
}
281+
282+
resource "alicloud_rocketmq_consumer_group" "consumer_group" {
283+
consumer_group_id = "ROCKETMQ_LOGISTIC_CONSUMER_GROUP"
284+
instance_id = alicloud_rocketmq_instance.rocketmq.id
285+
delivery_order_type = "Concurrently"
286+
consume_retry_policy {
287+
retry_policy = "DefaultRetryPolicy"
288+
max_retry_times = 5
289+
}
290+
}
291+
292+
resource "alicloud_rocketmq_acl" "topic1" {
293+
actions = ["Pub", "Sub"]
294+
instance_id = alicloud_rocketmq_instance.rocketmq.id
295+
username = alicloud_rocketmq_account.default.username
296+
resource_name = alicloud_rocketmq_topic.topic1.topic_name
297+
resource_type = "Topic"
298+
decision = "Allow"
299+
ip_whitelists = ["192.168.0.0/16"]
300+
}
301+
302+
resource "alicloud_rocketmq_acl" "consumer_group" {
303+
actions = ["Sub"]
304+
instance_id = alicloud_rocketmq_instance.rocketmq.id
305+
username = alicloud_rocketmq_account.default.username
306+
resource_name = alicloud_rocketmq_consumer_group.consumer_group.consumer_group_id
307+
resource_type = "Group"
308+
decision = "Allow"
309+
ip_whitelists = ["192.168.0.0/16"]
310+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "web_url" {
2+
description = "示例应用页面地址。(The address of the demo webpage.)"
3+
value = "http://${alicloud_instance.ecs_instance_provider.public_ip}/login"
4+
}

0 commit comments

Comments
 (0)