想要提高网站排名应该怎么做,商业网站最佳域名,广州建网站兴田德润可信,写wordpress导航栏我举的例子是#xff1a;在网上购物时#xff0c;我们支付后#xff0c;订单微服务会更新订单状态#xff0c;同时会远程调用购物车微服务清空购物车#xff0c;和调用商品微服务完成商品库存减一。
我们曾经说的事务是只能在本微服务完成回滚#xff0c;意思就是如果过…我举的例子是在网上购物时我们支付后订单微服务会更新订单状态同时会远程调用购物车微服务清空购物车和调用商品微服务完成商品库存减一。
我们曾经说的事务是只能在本微服务完成回滚意思就是如果过程中出现问题需要回滚只有更新订单状态 这一操作会回滚而清空购物车和商品库存减一 这俩操作是远程调用其他微服务的操作是不能回滚的所以我们就需要分布式事务来解决这种问题。
Seata致力于提供高性能和简单易用的分布式事务服务为用户打造一站式的分布式解决方案。
Seata事务管理中有3个重要角色 TC事务协调者维护全局和分支事务的状态协调全局事务提交或回滚。 TM事务管理器定义全局事务范围、开始全局事务、提交或回滚全局事务。 RM资源管理器管理分支事务与TC交谈以注册分支事务和报告分支事务的状态。 seata有俩种模式 XA 在2.3步之前的操作都是对数据库进行预处理只有2.3确定全部成功后才会commit提交。
优点事务强一致性满足acid原则常用数据库都支持实现简单 没有代码介入。
缺点在若干个RM执行sql时需要锁定数据库资源不同的RM用的时间也不同若有一个还没执行完其余的执行完的RM就只能干等着性能很差依赖关系型数据库实现事务。 AT模式 与XA模式不同的是AT模式的1.4步执行sql会立刻提交在1.4执行sql前会基于数据库信息形成一个快照如果后面有RM失败就可以把快照信息写回数据库恢复数据。
缺点在1.4执行提交后并且在第二阶段事务失败需要回滚时期间会有短暂虽然时间很短的数据不一致只满足了最终一致性。 下面我来说一说怎么用Java代码去实现它 首先我们需要去下载seata我用的是docker 部署的用的是1.5.2版本。我们需要把seata注册到nacos中。
我们写一下seata的配置文件 这里面你只需要改nacos的地址、账号密码mysql地址、账号密码。
server:port: 7099spring:application:name: seata-serverlogging:config: classpath:logback-spring.xmlfile: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_logstashconsole: #seata控制台的账号密码user:username: seatapassword: seataseata:config:# support: nacos, consul, apollo, zk, etcd3type: file# type: nacos# nacos:# server-addr: 192.168.1.105:8848# group : DEFAULT_GROUP# namespace: # dataId: shared-seata.yaml# username: nacos# password: nacosregistry:# support: nacos, eureka, redis, zk, consul, etcd3, sofatype: nacosnacos:application: seata-serverserver-addr: host.docker.internal:8848 #nacos 地址group: DEFAULT_GROUPnamespace: username: nacospassword: nacos# server:
# service-port: 8091 #If not configured, the default is ${server.port} 1000security:secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017tokenValidityInMilliseconds: 1800000ignore:urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/loginserver:# service-port: 8091 #If not configured, the default is ${server.port} 1000max-commit-retry-timeout: -1max-rollback-retry-timeout: -1rollback-retry-timeout-unlock-enable: falseenable-check-auth: trueenable-parallel-request-handle: trueretry-dead-threshold: 130000xaer-nota-retry-timeout: 60000enableParallelRequestHandle: truerecovery:committing-retry-period: 1000async-committing-retry-period: 1000rollbacking-retry-period: 1000timeout-retry-period: 1000undo:log-save-days: 7log-delete-period: 86400000session:branch-async-queue-size: 5000 #branch async remove queue sizeenable-branch-async-remove: false #enable to asynchronous remove branchSessionstore:# support: file 、 db 、 redismode: dbsession:mode: dblock:mode: dbdb:datasource: druiddb-type: mysqldriver-class-name: com.mysql.cj.jdbc.Driver#数据库地址url: jdbc:mysql://mysql:3306/seata?rewriteBatchedStatementstrueserverTimezoneUTCuser: rootpassword: 123min-conn: 10max-conn: 100global-table: global_tablebranch-table: branch_tablelock-table: lock_tabledistributed-lock-table: distributed_lockquery-limit: 1000max-wait: 5000# redis:# mode: single# database: 0# min-conn: 10# max-conn: 100# password:# max-total: 100# query-limit: 1000# single:# host: 192.168.150.101# port: 6379metrics:enabled: falseregistry-type: compactexporter-list: prometheusexporter-prometheus-port: 9898bindIpAddr: ${SERVICE_BIND_IP}transport:rpc-tc-request-timeout: 15000enable-tc-server-batch-send-response: falseshutdown:wait: 3thread-factory:boss-thread-prefix: NettyBossworker-thread-prefix: NettyServerNIOWorkerboss-thread-size: 1然后docker启动 其中SEATA_IP最好与你的nacos启动地址一致这里查看nacos地址
如果你nacos和mysql也是用docker部署的就一定要让它们三个加入同一network。
docker run --name seata -p 8099:8099 -p 7099:7099 -e SEATA_IP注册到nacos中的地址 -v 你的配置文件的目录:/seata-server/resources --privilegedtrue --network 你的network名称 -d seataio/seata-server:1.5.2
启动成功后我们就可以在nacos中看到我们的seata服务 然后我们需要在nacos中写一些配置来让我们的微服务能注册到seata中 seata:registry: #TC服务中心的配置微服务根据这些配置去注册中心获取TC服务地址type: nacosnacos: server-addr: 127.0.0.1:8848namespace: group: DEFAULT_GROUPapplication: seata-serverusername: nacospassword: nacos# 事务组名称tx-service-group: hmall service:vgroup-mapping: # 事务组与tc集群的映射关系hmall: defaultgrouplist: default: 127.0.0.1:8099#data-source-proxy-mode: XA #不填默认就是AT 然后我们就可以去稍微改一改我们的Java代码了
首先为我们的购物车微服务、订单微服务、商品微服务添加seata依赖 dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-seata/artifactId/dependency
还要在这三个微服务的配置文件中导入nacos中的seata配置
spring:application:name: *** # 服务名称profiles:active: dev # 开发环境cloud:nacos:server-addr: localhost:8848 # nacos地址config:file-extension: yamlshared-configs: #seata配置 - data-id: shared-seata.yamldiscovery:cluster-name: public 在网上购物时我们支付后订单微服务会更新订单状态同时会远程调用购物车微服务清空购物车和调用商品微服务完成商品库存减一。 这一操作是订单微服务中的方法找到改service层方法加上GlobalTransactional注解 OverrideGlobalTransactionalpublic Long createOrder(OrderFormDTO orderFormDTO) {.......}
我们还要找到购物车微服务的清空购物车的service方法和商品微服务的库存减一的service方法为它们加上Transactional注解.
然后我们就可以启动我们的微服务了我们每成功启动并注册到seata中seata的启动日志就会在最下面打印这几行日志 由此可知我们微服务成功启动了并注册到了seata中。