Seata

Seata is an open source distributed transaction solution that delivers high performance and easy to use distributed transaction services under a microservices architecture.

Installation

mysql:

1
2
3
4
5
6
7
docker run -d  -p 3306:3306 --restart=always --name mysql \
-e MYSQL_ROOT_PASSWORD=Aa654321 \
-e MYSQL_DATABASE=seata-server \
-e MYSQL_USER=seata \
-e MYSQL_PASSWORD=Aa123456 \
mysql:5.7.24 \
--character-set-server=utf8 --collation-server=utf8_general_ci --lower_case_table_names=1

https://seata.io/zh-cn/docs/ops/deploy-by-docker.html

#Copying out configs from docker container:

1
2
3
docker run -d -p 8091:8091 -p 7091:7091  --name seata-server seataio/seata-server:latest
docker cp seata-serve:/seata-server/resources ./config
docker rm -vf seata-server

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
#Nacos:
#https://github.com/nacos-group/nacos-docker/tree/master/example
docker run -d --name nacos-server -p 8848:8848 -p 9848:9848 -e MODE=standalone nacos/nacos-server:v2.1.0

#Cluster
#Create Dataabse:
https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql
docker run -d --name nacos-cluster \
-p 8848:8848 \
-p 9848:9848 \
-e TZ=Asia/Shanghai \
--env SPRING_DATASOURCE_PLATFORM=mysql \
--env MYSQL_SERVICE_HOST=192.168.101.82 \
--env MYSQL_SERVICE_DB_NAME=nacos-server \
--env MYSQL_SERVICE_USER=root \
--env MYSQL_SERVICE_PASSWORD=Aa123#@! \
nacos/nacos-server:v2.1.0
#集群模式下Seata连接会报错:server is DOWNnow, detailed error message: Optional[Distro protocol is not initialized],先用standalone模式启动,后面再解决。

#Browser, login with nacos/nacos
http://192.168.101.82:8848/nacos/#/login

#Seata:
#Create Database:
https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql
docker run -d --name seata-server \
-p 8091:8091 \
-p 7091:7091 \
-e SEATA_IP=192.168.101.82 \
-e SEATA_PORT=8091 \
-v /data/vagrant/boxes/docker/seata/config:/seata-server/resources \
seataio/seata-server:1.5.1
#Browser, login with seata/seata
http://192.168.101.82:7091/


#Sentinel Dashboard
#https://www.cnblogs.com/wintersoft/p/11235192.html
wget https://github.com/alibaba/Sentinel/releases/download/1.8.4/sentinel-dashboard-1.8.4.jar
java -Dserver.port=8082 \
-Dcsp.sentinel.dashboard.server=192.168.101.82:8082 \
-Dproject.name=sentinel-dashboard \
-jar sentinel-dashboard-1.8.4.jar

#cat sentinel-dashboard-Dockerfile
FROM openjdk:8
ENV SENTINEL_HOME /opt/sentinel-dashboard
RUN mkdir -p ${SENTINEL_HOME}
COPY ./sentinel-dashboard-1.8.4.jar ${SENTINEL_HOME}
RUN chmod -R +x ${SENTINEL_HOME}/*jar
WORKDIR ${SENTINEL_HOME}
EXPOSE 8080
CMD java ${JAVA_OPTS} -jar -Dcsp.sentinel.dashboard.server=192.168.101.82:8082 -Dproject.name=sentinel-dashboard sentinel-dashboard-1.8.4.jar

#build
docker build -t "dave/sentinel-dashboard:1.8.4" . -f sentinel-dashboard-Dockerfile
#start
docker run -d --name sentinel-dashboard -p 8082:8080 dave/sentinel-dashboard:1.8.4
#Browser, login with sentinel/sentinel
http://192.168.101.82:8082/

application.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
server:
port: 7091

spring:
application:
name: seata-server

logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash

console:
user:
username: seata
password: seata

seata:
config:
# support: nacos, consul, apollo, zk, etcd3
type: nacos
nacos:
server-addr: 192.168.101.82:8848
namespace:
group: SEATA_GROUP
username: nacos
password: nacos

registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
type: nacos
nacos:
application: seata-server
server-addr: 192.168.101.82:8848
group: SEATA_GROUP
namespace:
cluster: default
username: nacos
password: nacos
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.101.82:3306/seata-server?rewriteBatchedStatements=true
user: root
password: Aa123#@!
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

The scripts are located at: https://github.com/seata/seata/tree/develop/script

Demo

https://seata.io/zh-cn/docs/user/quickstart.html

https://github.com/seata/seata-samples/tree/master/dubbo

Nacos

https://nacos.io/zh-cn/docs/quick-start-spring-boot.html

https://github.com/nacos-group/nacos-examples.git

Shardingsphere-proxy

1
2
3
4
5
6
7
8
9
10
11
12
mkdir /data/shardingsphere-proxy/conf /data/ext-lib

wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar -P /data/ext-lib/

docker run -d --name tmp --entrypoint=bash apache/shardingsphere-proxy:5.2.1
docker cp tmp:/opt/shardingsphere-proxy/conf /data/shardingsphere-proxy/conf
docker rm tmp

docker run -d --name shardingsphere-proxy \
-v /data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf \
-v /data/shardingsphere-proxy/ext-lib:/opt/shardingsphere-proxy/ext-lib \
-e PORT=3308 -p13308:3308 apache/shardingsphere-proxy:5.2.1

cat server.yaml:

1
2
3
4
5
6
7
8
9
rules:
- !AUTHORITY
users:
- root@%:Aa1234$#@!
provider:
type: ALL_PERMITTED
- !TRANSACTION
defaultType: XA
providerType: Atomikos

一个库一个配置文件。

config-sharding-test.yaml:

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
schemaName: seata_test

dataSources:
ds_0:
url: jdbc:mysql://192.168.101.82/seata_test?serverTimezone=UTC&useSSL=false
username: root
password: aaa
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1

rules:
- !SHARDING
tables:
service_at:
actualDataNodes: ds_0.service_at_$->{0..2}
tableStrategy:
standard:
shardingColumn: name
shardingAlgorithmName: database_consistent
#shardingAlgorithmName: database_inline
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
service_tm:
actualDataNodes: ds_0.service_tm_$->{0..2}
tableStrategy:
standard:
shardingColumn: aName
shardingAlgorithmName: database_consistent
#shardingAlgorithmName: database_inline
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
shardingAlgorithms:
database_consistent:
type: CONSISTENT_HASH
#database_inline:
# type: INLINE
# props:
# algorithm-expression: service_at_$->{account_id % 3}

keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123

cat config-sharding-account.yaml:

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
schemaName: account_dev

dataSources:
ds_0:
url: jdbc:mysql://rm-3ns38ayfen92i16o6.mysql.rds.aliyuncs.com:3306/account_dev?serverTimezone=UTC&useSSL=false
username: root
password: aaa
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1

rules:
- !SHARDING
tables:
capital_accounting_record:
actualDataNodes: ds_0.capital_accounting_record_$->{0..2}
tableStrategy:
standard:
shardingColumn: account_number
shardingAlgorithmName: database_consistent
#shardingAlgorithmName: database_inline
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
shardingAlgorithms:
database_consistent:
type: CONSISTENT_HASH
#database_inline:
# type: INLINE
# props:
# algorithm-expression: capital_accounting_record_$->{account_id % 3}

keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123

自定义分片规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#https://shardingsphere.apache.org/document/5.2.1/cn/user-manual/shardingsphere-proxy/startup/bin/
1. 实现 `ShardingAlgorithm` 接口定义的算法实现类。
2. 在项目 `resources` 目录下创建 `META-INF/services` 目录。
3. 在 `META-INF/services` 目录下新建文件 `org.apache.shardingsphere.sharding.spi.ShardingAlgorithm`
4. 将实现类的全限定类名写入至文件 `org.apache.shardingsphere.sharding.spi.ShardingAlgorithm`
5. 将上述 Java 文件打包成 jar 包。
6. 将上述 jar 包拷贝至 `ext-lib` 目录。
7. 将上述自定义算法实现类的 Java 文件引用配置在 YAML 文件中:
tableStrategy:
standard:
...
shardingAlgorithmName: database_consistent
...
shardingAlgorithms:
database_consistent:
type: CONSISTENT_HASH

Reference