转载

基于SpringCloudAlibaba微服务架构nacos整合分布式事务框架seata(原名fescar)

基于SpringCloudAlibaba微服务架构nacos整合分布式事务框架seata(原名fescar)

1.拉去seata官方的seata-server ( github地址 )

https://github.com/seata/seata/releases/download/v0.9.0/seata-server-0.9.0.tar.gz
复制代码

2.解压 seata-server-0.9.0.tar.gz

tar -zxvf seata-server-0.9.0.tar.gz
复制代码

3.进入seata/conf目录修改register.conf配置文件

registry {
	#基于naocs   serverAdd配置对应的nacos地址 namespace命名空间
	#另外还可以基于file 、eureka、redis、zk、consul、etcd3、sofa等可以做相关配置,这里就不一一操作了
  type = "nacos"
  nacos {
    serverAddr = "192.168.1.19:8848"
    namespace = "public"
    cluster = "default"
  }
}
config {
  type = "nacos"
  nacos {
    serverAddr = "192.168.1.19:8848"
    namespace = "public"
    cluster = "default"
  }
}
复制代码

4.修改nacos-config.txt

service.vgroup_mapping.vendor-system-fescar-service=default   #vendor-system-fescar-service配置事务组名,跟应用服务上配置一致,后面会提到
service.vgroup_mapping.vendor-position-fescar-service=default
复制代码

5.进入bin目录,修改启动脚本seata-server.sh ,防止发生内存泄漏问题

-XX:MaxDirectMemorySize=2048m

6.在naocs服务页面添加,值默认default(或者执行nacos-conf.sh脚本)

基于SpringCloudAlibaba微服务架构nacos整合分布式事务框架seata(原名fescar)

7.启动脚本文件,注意seata默认取的是服务器的内网ip,那么外部服务无法注册到seata-server上。那么就必须在启动参数指定外网ip "-h 47.125.114.58"

sh seata-server.sh -h 47.125.114.58

服务启动成功

基于SpringCloudAlibaba微服务架构nacos整合分布式事务框架seata(原名fescar)

8.可以在nacos服务页面看到,seata服务注册成功

基于SpringCloudAlibaba微服务架构nacos整合分布式事务框架seata(原名fescar)

9.项目vendor-system配置 添加依赖

<!--seata事务组件-->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
复制代码

10.配置事务组

spring:
  cloud:
    alibaba:
      seata:
        tx-service-group: vendor-system-fescar-servic
复制代码

11.配置代理数据源,

@Configuration
@Import(DataSourcesProperties.class)
public class DataSourceProxyAutoConfiguration {

    private DataSourcesProperties dataSourcesProperties;

    public DataSourceProxyAutoConfiguration(DataSourcesProperties dataSourcesProperties) {
        this.dataSourcesProperties = dataSourcesProperties;
    }

    /**
     * create proxy datasource
     * @return
     */
    @Primary
    @Bean(name = "dataSource")
    public DataSource getDatasource(){
        HikariDataSource build = DataSourceBuilder.create().url(dataSourcesProperties.getUrl())
                .password(dataSourcesProperties.getPassword())
                .username(dataSourcesProperties.getUsername())
                .type(dataSourcesProperties.getType())
                .driverClassName(dataSourcesProperties.getDriverClassName()).build();
        return new DataSourceProxy(build);
    }

}
复制代码

12.配置fegin远程调用时xid的传输

public class FescarRequestInterceptor implements RequestInterceptor {

    private static final String FESCAR_HEADER_NAME = "Fescar-Xid";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String xid = RootContext.getXID();
        if(StringUtils.isNoneBlank(xid)){
            requestTemplate.header(FESCAR_HEADER_NAME,xid);
        }
    }
}
复制代码
原文  https://juejin.im/post/5dd7cdadf265da7e1f1ac032
正文到此结束
Loading...