长时间出一个技术文章,也不会排版了
就随意记录一下,能帮到忙,是我的荣幸
关于seata1.4.x我也不做了,因为升级1.5版本 出现了一些问题,反正跟着我这边做,保证你能运行
标题已经表明,我将使用docker启动seata1.5版本
首先本地安装docker运行环境,不知道的可以学一下
1.5版本与1.4还是有很多差异的,我只做运行,1.5不在使用registry.conf 与 file.conf , 而是使用的application.yml配置,我们想要发到服务器,或指定nacos地址 ,必须更改配置文件,如下,这边采用了nacos注册中心,并且使用了nacos-config放入seata的配置,seataServer.properties ,自己使用的话,只要更改nacos的两个连接地址即可
- 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: admin
- password: admin
- seata:
- config:
- # support: nacos 、 consul 、 apollo 、 zk 、 etcd3
- type: nacos
- nacos:
- server-addr: http://192.168.1.23:8848
- namespace:
- group: SEATA_GROUP
- username:
- password:
- ##if use MSE Nacos with auth, mutex with username/password attribute
- #access-key: ""
- #secret-key: ""
- data-id: seataServer.properties
- registry:
- # support: nacos 、 eureka 、 redis 、 zk 、 consul 、 etcd3 、 sofa
- type: nacos
- preferred-networks: 30.240.*
- nacos:
- application: seata-server
- server-addr: http://192.168.1.23:8848
- group: SEATA_GROUP
- namespace:
- cluster: default
- username:
- password:
- ##if use MSE Nacos with auth, mutex with username/password attribute
- #access-key: ""
- #secret-key: ""
- security:
- secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
- tokenValidityInMilliseconds: 1800000
- ignore:
- urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
2、打包将配置导入镜像
在application.yml旁边建立Dockerfile文件,内容如下
- FROM seataio/seata-server:1.5.0
- ADD application.yml /seata-server/resources/application.yml
执行docker命令
docker build -t seata-server:1.5.0 .
当前会下载seata,并重新命名seata及版本号,将application.yml同步到镜像中
- [+] Building 3.6s (7/7) FINISHED
- => [internal] load build definition from Dockerfile 0.0s
- => => transferring dockerfile: 31B 0.0s
- => [internal] load .dockerignore 0.0s
- => => transferring context: 2B 0.0s
- => [internal] load metadata for docker.io/seataio/seata-server:1.5.0 2.8s
- => [internal] load build context 0.0s
- => => transferring context: 4.33kB 0.0s
- => CACHED [1/2] FROM docker.io/seataio/seata-server:1.5.0@sha256:1797b0109ed31ef9d1073c77b8a3e89cc1f856f2cac6964b1dbad73a54fcf0fc 0.0s
- => => resolve docker.io/seataio/seata-server:1.5.0@sha256:1797b0109ed31ef9d1073c77b8a3e89cc1f856f2cac6964b1dbad73a54fcf0fc 0.0s
- => [2/2] ADD application.yml /seata-server/resources/application.yml 0.1s
- => exporting to image 0.2s
- => => exporting layers 0.1s
- => => writing image sha256:356fed758bbd523524d5e6ee7c8ef6317a6ef078555032ee17cd608794dc5bd9 0.0s
- => => naming to docker.io/library/seata-server:1.5.0 0.1s
-
- Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
可以在运行时,进入镜像中查看更改后的内容,在resources目录application.yml
===================此时启动seata已经完成了===================
但是,nacos还没有启动,这点很重要,seata不仅要注册到nacos,还要使用config配置
nacos我使用的是2.0.3版本 ,对应的nacos-client版本也必须是2.0.x版本
docker快速启动
docker run --name docker-quick -e MODE=standalone -p 8848:8848 -p 9848:9848 -d nacos/nacos-server:v2.0.3
使用mysql
docker run -d -p 8848:8848 -p 9848:9848 -e PREFER_HOST_MODE=hostname -e MODE=standalone -e SPRING_DATASOURCE_PLATFORM=mysql -e MYSQL_SERVICE_HOST=127.0.0.1 -e MYSQL_SERVICE_PORT=3306 -e MYSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=root -e MYSQL_SERVICE_DB_NAME=nacos_db --name nacos-standalone nacos/nacos-server
启动之后添加seataServer.properties配置
下面是seataServer.properties内容,注意:驱动必须正确
-
- # 存储模式
- store.mode=db
-
- store.db.datasource=druid
- store.db.dbType=mysql
- # 需要根据mysql的版本调整driverClassName
- # mysql8及以上版本对应的driver:com.mysql.cj.jdbc.Driver
- # mysql8以下版本的driver:com.mysql.jdbc.Driver
- store.db.driverClassName=com.mysql.jdbc.Driver
- # 注意根据生产实际情况调整参数host和port
- store.db.url=jdbc:mysql://127.0.0.1:3306/seata_db?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
- # 数据库用户名
- store.db.user=root
- # 用户名密码
- store.db.password=root
- # 微服务里配置与这里一致
- service.vgroupMapping.default_tx_group=default
此时启动seata,就能正常运行了
4、微服务的启动
pom文件的配置
- <!-- 父工程pom -->
- <dependencyManagement>
-
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>2.2.7.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencyManagement>
-
-
-
-
- <!-- 微服务pom -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.reflections</groupId>
- <artifactId>reflections</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>org.reflections</groupId>
- <artifactId>reflections</artifactId>
- <version>0.10.2</version>
- </dependency>
-
- <dependency>
- <groupId>io.seata</groupId>
- <artifactId>seata-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
- </dependency>
application.yml或者bootstrap.yml添加seata的配置
- seata:
- enable: true
- tx-service-group: default_tx_group
- registry:
- type: nacos
- nacos:
- # 非本地请修改具体的地址
- server-addr: http://192.168.1.23:8848
- service:
- disable-global-transaction: false
- vgroupMapping:
- default_tx_group: default
在更新操作的业务代码上添加全局事务注解
@GlobalTransactional
https://gitee.com/seata-io/seata/blob/v1.5.1/script/server/db/mysql.sql#
seata数据库
undo_log表
此时所有的操作已完成,能够正常的使用分布式事务了
在使用新版本,没有足够了解的情况下,会遇到很多很多问题,不管是nacos2.0.3版本的rpc端口问题,还是seata配置的问题,解决起来特别耗费时间,本人研究的也不是很多,但也希望能够帮助大家节省时间,至于其他研究的话,以后再说吧,如果有不对的地方,劳烦大神指点一下,我这边及时更改,避免给其他小伙伴带来困扰,谢谢大家