-
Notifications
You must be signed in to change notification settings - Fork 35
/
dts-cache-synchronization.yml
451 lines (449 loc) · 15.8 KB
/
dts-cache-synchronization.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 该模版通过DTS实现MySQL与Redis的缓存同步,包括VPC、ECS、RDS、Redis实例创建及安全配置,同时设置消费组确保数据一致性。
en: This template achieves cache synchronization between MySQL and Redis via DTS,
encompassing the creation and security configuration of VPC, ECS, RDS, and Redis
instances, alongside the establishment of consumer groups to ensure data consistency.
Parameters:
ZoneId:
Type: String
Label:
en: VSwitch Availability Zone
zh-cn: 交换机可用区
AssociationProperty: ALIYUN::ECS::Instance::ZoneId
InstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 实例类型
Description:
en: 'Fill in the specifications that can be used under the VSwitch availability
zone;</b></font><br>general specifications:<font color=''red''><b>ecs.c5.large</b></font><br>note:
a few zones do not support general specifications<br>see detail: <a href=''https://www.alibabacloud.com/help/en/doc-detail/25378.html''
target=''_blank''><b><font color=''blue''>Instance Specification Family.</font></a></b>'
zh-cn: 填写VSwitch可用区下可使用的规格;<br>通用规格:<font color='red'><b>ecs.c5.large</b></font><br>注:可用区可能不支持通用规格<br>规格详见:<a
href='https://help.aliyun.com/document_detail/25378.html' target='_blank'><b><font
color='blue'>实例规格族。</font></a></b>
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
SystemDiskCategory:
Type: String
Label:
en: System Disk Type
zh-cn: 系统盘类型
Description:
en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'
zh-cn: '<font color=''blue''><b>可选值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盘</font>]<br>[cloud_ssd: <font color=''green''>SSD云盘</font>]<br>[cloud_essd: <font color=''green''>ESSD云盘</font>]<br>[cloud: <font color=''green''>普通云盘</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盘</font>]'
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
LocaleKey: DiskCategory
InstanceType: ${InstanceType}
ZoneId: ${ZoneId}
InstancePassword:
Type: String
Label:
en: Instance Password
zh-cn: 实例密码
Description:
en: Server login password, Length 8-30, must contain three(Capital letters,
lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol
in).
zh-cn: 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
ConstraintDescription:
en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers,
()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
zh-cn: 长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
MinLength: 8
MaxLength: 30
NoEcho: true
DBName:
Type: String
Label:
en: DB Name
zh-cn: 数据库名
Description:
en: Name of database.
zh-cn: RDS数据库名。
ConstraintDescription:
en: Consist of 2 to 64 characters of lowercase letters, underline. Must begin
with a letter and be end with an alphanumeric character.
zh-cn: 由 2 到 64 个小写字母组成,下划线。必须以字母开头,以字母数字字符结尾。
Default: demodb
MinLength: 2
MaxLength: 64
RDSDBUser:
Type: String
Label:
en: RDS DB Username
zh-cn: RDS数据库账号
Description:
en: Username of database.
zh-cn: RDS数据库账号。
ConstraintDescription:
en: Consist of 2 to 16 characters of lowercase letters, underline. Must begin
with a letter and be end with an alphanumeric character.
zh-cn: 由 2 到 16 个小写字母组成,下划线。必须以字母开头,以字母数字字符结尾。
Default: demouser123
MinLength: 2
MaxLength: 16
RDSDBPassword:
Type: String
Label:
en: RDS DB Password
zh-cn: RDS数据库密码
Description:
en: RDS database password, consisting of letters, numbers, and underline(_),
is 8 to 32 characters long,Must contain three different types of characters.
zh-cn: RDS数据库密码,由字母、数字、下划线(_)组成,长度为8~32个字符,必须包含3种不同类型的字符。
ConstraintDescription:
en: Consisting of letters, numbers, and underline(_),
is 8 to 32 characters long.
zh-cn: 由字母、数字、下划线(_)组成,长度为8~32个字符。
AssociationProperty: ALIYUN::RDS::Instance::AccountPassword
MinLength: 8
MaxLength: 32
NoEcho: true
DBInstanceClass:
Type: String
Label:
en: Instance Class
zh-cn: 实例规格
AssociationProperty: ALIYUN::RDS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
EngineVersion: "8.0"
Engine: MySQL
Category: HighAvailability
DBInstanceStorageType: cloud_essd
CommodityCode: bards
RedisInstanceClass:
Type: String
Label:
en: Instance Type
zh-cn: 实例规格
Description:
en: Instance specifications, please refer to:<a href='https://www.alibabacloud.com/help/en/doc-detail/26350.html'
target='_blank'><b><font color='blue'>View specifications</font></b></a>
zh-cn: 推荐规格:<b><font color='blue'>redis.master.small.default</font></b><br>更多实例规格,请参考:<a
href='https://help.aliyun.com/document_detail/26350.html' target='_blank'><b><font
color='blue'>查看规格信息</font></b></a>
AssociationProperty: ALIYUN::Redis::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ${ZoneId}
RedisPassword:
Type: String
Label:
en: Instance Password
zh-cn: 实例密码
Description:
en: 'Length 8-32 characters, can contain size letters, Numbers and special symbols,
including:! @ # $ % ^ & * ( ) _ + - ='
zh-cn: 长度8-32个字符,可包含大小字母、数字及特殊符号(包含:!@#$%^&*()_+-=)
ConstraintDescription:
en: '8-32 characters, can contain size letters, Numbers and special symbols,
including:! @ # $ % ^ & * ( ) _ + - ='
zh-cn: 8-32个字符,可包含大小字母、数字及特殊符号(包含:!@#$%^&*()_+-=)
MinLength: '8'
MaxLength: '32'
NoEcho: true
ConsumerGroupUserName:
Type: String
Label:
en: Consumer Group User Name
zh-cn: 消费组账号
Description:
en: The maximum length is 16 characters. The value can contain letters, digits, and underscores (_).
zh-cn: 最长16个字符。可包含英文字母、数字和下划线(_)。
Default: demoUser
ConsumerGroupPassword:
Type: String
Label:
en: Consumer Group Password
zh-cn: 消费组账号的密码
Description:
en: The name contains 8 to 32 characters. Contains at least two types of letters, digits, and special characters.
zh-cn: 长度为8~32个字符。至少包含英文字母、数字和特殊字符中的两种。
MinLength: '8'
MaxLength: '32'
NoEcho: true
Resources:
Vpc:
Type: ALIYUN::ECS::VPC
DependsOn: AliyunDTSDefaultRole
Properties:
CidrBlock: 192.168.0.0/16
VSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Ref: Vpc
VSwitchName:
Fn::Join:
- '-'
- - VSwitch
- StackId
- Ref: ALIYUN::StackId
CidrBlock: 192.168.0.0/24
SecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
VpcId:
Ref: Vpc
SecurityGroupIngress_3306:
Type: ALIYUN::ECS::SecurityGroupIngress
Properties:
SecurityGroupId:
Ref: SecurityGroup
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: intranet
PortRange: 3306/3306
SecurityGroupIngress_22:
Type: ALIYUN::ECS::SecurityGroupIngress
Properties:
SecurityGroupId:
Ref: SecurityGroup
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
NicType: intranet
PortRange: 22/22
EcsInstance:
Type: ALIYUN::ECS::Instance
Properties:
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
SecurityGroupId:
Ref: SecurityGroup
ImageId: centos_7_8
InstanceType:
Ref: InstanceType
SystemDiskCategory:
Ref: SystemDiskCategory
Password:
Ref: InstancePassword
IoOptimized: optimized
InternetMaxBandwidthOut: 80
RdsDBInstance:
Type: ALIYUN::RDS::DBInstance
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
DBInstanceClass:
Ref: DBInstanceClass
DBInstanceStorage: 100
Engine: MySQL
EngineVersion: "8.0"
MasterUserPassword:
Ref: RDSDBPassword
MasterUserType: Super
MasterUsername:
Ref: RDSDBUser
DBMappings:
- CharacterSetName: utf8mb4
DBName:
Ref: DBName
Category: HighAvailability
DBInstanceStorageType: cloud_essd
SecurityIPList: 0.0.0.0/0
AllocatePublicConnection: true
SlaveZoneIds:
- Ref: ZoneId
RandomString:
Type: ALIYUN::RandomString
Properties:
length: 10
character_classes:
- class: lowercase
min: 10
RedisInstance:
Type: ALIYUN::REDIS::Instance
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
InstanceClass:
Ref: RedisInstanceClass
InstanceName: rds_mysql2redis_redis
EngineVersion: "5.0"
Connections:
PublicConnection:
ConnectionPort: 6379
ConnectionString:
Fn::Sub: rds-mysql2redis-${RandomString.value}
Password:
Ref: RedisPassword
Whitelist:
Type: ALIYUN::REDIS::Whitelist
Properties:
InstanceId:
Ref: RedisInstance
SecurityIps: 0.0.0.0/0
SubscriptionInstance:
DependsOn:
- RdsDBInstance
Type: ALIYUN::DTS::SubscriptionInstance
Properties:
SourceEndpointInstanceType: MySQL
Configuration:
DbList:
Fn::Sub: |-
{"${DBName}":{"name":"${DBName}","all":true}}
SubscriptionDataType:
DDL: false
DML: true
SubscriptionInstanceName: Subscription
SourceEndpoint:
InstanceType: RDS
UserName:
Ref: RDSDBUser
Password:
Ref: RDSDBPassword
InstanceID:
Ref: RdsDBInstance
SubscriptionInstanceNetworkType: vpc
SubscriptionInstance:
VPCId:
Ref: Vpc
VSwitchId:
Ref: VSwitch
ConsumerGroup:
Type: ALIYUN::DTS::ConsumerGroup
Properties:
ConsumerGroupPassword:
Ref: ConsumerGroupPassword
ConsumerGroupUserName:
Ref: ConsumerGroupUserName
ConsumerGroupName: mysql2redis_consumer_group
SubscriptionInstanceId:
Ref: SubscriptionInstance
WaitConditionHandle:
Type: ALIYUN::ROS::WaitConditionHandle
Properties: { }
DependsOn:
- Whitelist
- SecurityGroupIngress_22
- SecurityGroupIngress_3306
WaitCondition:
Type: ALIYUN::ROS::WaitCondition
Properties:
Handle:
Ref: WaitConditionHandle
Timeout: 7200
Count: 1
InstallDTSMySQL2Redis:
Type: ALIYUN::ECS::RunCommand
Properties:
InstanceIds:
- Ref: EcsInstance
Type: RunShellScript
Sync: true
Timeout: 7200
CommandContent:
Fn::Sub: |-
#!/bin/bash
yum install -y java-1.8.0-openjdk-devel maven wget unzip
echo 'JAVA_HOME=/usr/lib/jvm/java-1.8.0' >> /etc/profile
echo 'PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
source /etc/profile
wget https://database-plg.oss-cn-beijing.aliyuncs.com/dbbp/mysql2redis/subscribe-demo-master.zip
unzip subscribe-demo-master.zip
rm -rf subscribe-demo-master.zip
cd subscribe-demo-master
FilePath='src/main/java/com/alibaba/dts/demo/DTSMySQL2RedisDemo.java'
sed -i "s/your broker url/${SubscriptionInstance.PublicHost}/" $FilePath
sed -i "s/your dts topic/${SubscriptionInstance.SubscribeTopic}/" $FilePath
sed -i "s/your sid/${ConsumerGroup.ConsumerGroupID}/" $FilePath
sed -i "s/your user name/${ConsumerGroupUserName}/" $FilePath
sed -i "s/your password/${RDSDBPassword}/" $FilePath
timestamp=`date -d "$time" +%s`
sed -i "s/start timestamp/$timestamp/" $FilePath
sed -i "s/your redis url/${RedisInstance.PublicConnectionString}/" $FilePath
sed -i "s/your redis password/${RedisPassword}/" $FilePath
mvn clean install
LOG_FILE="message.log"
nohup java -jar target/subscribe-demo-1.0-SNAPSHOT-jar-with-dependencies.jar >> $LOG_FILE 2>&1 &
while [ ! -f "$LOG_FILE" ]; do
echo "$(date '+%Y-%m-%d %H:%M:%S') Wait for FILE $LOG_FILE to generate..." >> $LOG_FILE
sleep 60
done
echo "$(date '+%Y-%m-%d %H:%M:%S') Program packaging complete." >> $LOG_FILE
if grep -q "Error" "$LOG_FILE"; then
error_message=$(grep "Error" message.log | head -n 1)
${WaitConditionHandle.CurlCli} --data-binary '{"status": "FAILURE", "reason": "An error message is displayed: $error_message. For details, log in to the ECS instance, see /root/subscribe-demo-master/message.log"}'
else
${WaitConditionHandle.CurlCli} --data-binary '{"status": "SUCCESS"}'
fi
DependsOn:
- Whitelist
- SecurityGroupIngress_22
- SecurityGroupIngress_3306
AliyunDTSDefaultRole:
Type: 'ALIYUN::RAM::Role'
Properties:
RoleName: AliyunDTSDefaultRole
Description: DTS默认使用此角色来访问您在其他云产品中的资源。
AssumeRolePolicyDocument:
Version: '1'
Statement:
- Action: 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- dts.aliyuncs.com
MaxSessionDuration: 3600
IgnoreExisting: true
DeletionForce: true
PolicyAttachments:
System:
- AliyunDTSRolePolicy
Outputs:
RdsInstanceInnerConnectionString:
Description:
en: RDS public connection string.
zh-cn: RDS外网连接串。
Value:
Fn::GetAtt:
- RdsDBInstance
- PublicConnectionString
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- ZoneId
- InstanceType
- SystemDiskCategory
- InstancePassword
Label:
default: ECS
- Parameters:
- DBInstanceClass
- DBName
- RDSDBUser
- RDSDBPassword
Label:
default: RDS
- Parameters:
- RedisInstanceClass
- RedisPassword
Label:
default: Redis
- Parameters:
- ConsumerGroupUserName
- ConsumerGroupPassword
Label:
default: Dts
TemplateTags:
- acs:technical-solution:database:实时同步RDS与Redis构建缓存一致性-tech_solu_21