seata本地配置以及搭配nacos进行配置

seata本地配置以及搭配nacos进行配置

注:本文seata版本:seata-server-1.4.2,nacos版本为:nacos-server-2.0.1

一:基于本地配置
1、下载seata-server-1.4.2,打开conf文件夹,编辑registry.config,将registry以及config的type全部改为file,启动seata
在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/e8b23056e43b4dde91e0f32307ac9b3e.png在这里插入图片描述
2、项目中引入依赖

    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-spring-boot-starter</artifactId>
        <version>${seata.spring.boot.starter.version}</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        <version>${spring.cloud.starter.alibaba.seata.version}</version>
        <exclusions>
            <exclusion>
                <groupId>io.seata</groupId>
                <artifactId>seata-spring-boot-starter</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

3、yml中进行配置

seata:
  enabled: true
  application-id: test
  tx-service-group: my_test_tx_group
  enable-auto-data-source-proxy: true
  service:
    vgroup-mapping:
      my_test_tx_group: default
    #disable-global-transaction: false
    default:
      grouplist: 127.0.0.1:8091
    #enable-degrade: false
  config:
    type: file
    file:
      name: file.conf
  registry:
    type: file
    file:
      name: file.conf

4、消费者调用接口时,需要在请求头中加入header,此请求头中的xid是用于分布式事务的上下文关联

headers.put(RootContext.KEY_XID.toLowerCase(), GlobalTransactionContext.getCurrentOrCreate().getXid());

5、生产者需配置一个过滤器,用于接收上文传递的xid

package com.future.dubbo.provider.config;

import io.seata.core.context.RootContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class SeataFilter implements Filter {
   
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
   
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
   
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String xid = req.getHeader(RootContext.KEY_XID.toLowerCase());
        boolean isBind = false;
        if (StringUtils.isNotBlank(xid)) {
   
            RootContext.bind(xid)<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值