Skip to content

Commit

Permalink
Seata 分布式事务
Browse files Browse the repository at this point in the history
  • Loading branch information
D2C-Cai committed Feb 20, 2021
1 parent 0ca514c commit 0e4aeb8
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

Expand All @@ -23,7 +24,7 @@
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class MemberServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.herring.orders.OrdersClient;
import com.herring.product.ProductClient;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
Expand All @@ -28,6 +31,10 @@ public class MemberService {

@Resource
private MemberMapper memberMapper;
@Resource
private ProductClient productClient;
@Resource
private OrdersClient ordersClient;

@SentinelResource(value = "sayHello", fallback = "sayHelloFail")
public String sayHello() {
Expand All @@ -38,8 +45,12 @@ public String sayHelloFail() {
return "I am sorry, Member! ";
}

@GlobalTransactional(rollbackFor = Exception.class)
public int doUpdate() {
return memberMapper.update();
int result = memberMapper.update();
ordersClient.update();
productClient.update();
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.herring.member.config;

/*
@(#)herring 2021-02-20
Copyright (c) 2011-2021 杭州湖畔网络技术有限公司
保留所有权利
本软件为杭州湖畔网络技术有限公司所有及包含机密信息,须遵守其相关许可证条款进行使用。
Copyright (c) 2011-2021 HUPUN Network Technology CO.,LTD.
All rights reserved.
This software is the confidential and proprietary information of HUPUN
Network Technology CO.,LTD("Confidential Information"). You shall not
disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement you entered into with HUPUN.
Website:http://www.hupun.com
*/

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
* @author: Jackey 2021/2/20
*/
@Configuration
public class DataSourceConfiguration {

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}

@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}

@Bean
public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/mapper/*.xml"));
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ spring:
url: jdbc:mysql://192.168.138.131:3306/herring_data?characterEncoding=UTF-8&useSSL=false
username: root
password: 88021120
hikari:
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
maximum-pool-size: 9

mybatis:
mapper-locations: classpath:/mapper/*.xml

feign:
sentinel:
enabled: true
hystrix:
enabled: false
client:
config:
default:
requestInterceptors:
- com.herring.feign.interceptor.TokenRelayRequestInterceptor
readTimeout: 60000
connectTimeout: 60000

rocketmq:
name-server: 192.168.138.131:9876
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

Expand All @@ -23,7 +24,7 @@
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class OrdersServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public String sayHelloFail() {
}

public int doUpdate() {
return ordersMapper.update();
throw new RuntimeException("server error! ");
// return ordersMapper.update();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.herring.orders.config;

/*
@(#)herring 2021-02-20
Copyright (c) 2011-2021 杭州湖畔网络技术有限公司
保留所有权利
本软件为杭州湖畔网络技术有限公司所有及包含机密信息,须遵守其相关许可证条款进行使用。
Copyright (c) 2011-2021 HUPUN Network Technology CO.,LTD.
All rights reserved.
This software is the confidential and proprietary information of HUPUN
Network Technology CO.,LTD("Confidential Information"). You shall not
disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement you entered into with HUPUN.
Website:http://www.hupun.com
*/

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
* @author: Jackey 2021/2/20
*/
@Configuration
public class DataSourceConfiguration {

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}

@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}

@Bean
public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/mapper/*.xml"));
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ spring:
url: jdbc:mysql://192.168.138.131:3306/herring_data?characterEncoding=UTF-8&useSSL=false
username: root
password: 88021120
hikari:
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
maximum-pool-size: 9

mybatis:
mapper-locations: classpath:/mapper/*.xml

feign:
sentinel:
enabled: true
hystrix:
enabled: false
client:
config:
default:
requestInterceptors:
- com.herring.feign.interceptor.TokenRelayRequestInterceptor
readTimeout: 60000
connectTimeout: 60000

rocketmq:
name-server: 192.168.138.131:9876
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

Expand All @@ -23,7 +24,7 @@
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class ProductServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.herring.product.config;

/*
@(#)herring 2021-02-20
Copyright (c) 2011-2021 杭州湖畔网络技术有限公司
保留所有权利
本软件为杭州湖畔网络技术有限公司所有及包含机密信息,须遵守其相关许可证条款进行使用。
Copyright (c) 2011-2021 HUPUN Network Technology CO.,LTD.
All rights reserved.
This software is the confidential and proprietary information of HUPUN
Network Technology CO.,LTD("Confidential Information"). You shall not
disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement you entered into with HUPUN.
Website:http://www.hupun.com
*/

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
* @author: Jackey 2021/2/20
*/
@Configuration
public class DataSourceConfiguration {

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}

@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}

@Bean
public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/mapper/*.xml"));
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ spring:
url: jdbc:mysql://192.168.138.131:3306/herring_data?characterEncoding=UTF-8&useSSL=false
username: root
password: 88021120
hikari:
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
maximum-pool-size: 9

mybatis:
mapper-locations: classpath:/mapper/*.xml

feign:
sentinel:
enabled: true
hystrix:
enabled: false
client:
config:
default:
requestInterceptors:
- com.herring.feign.interceptor.TokenRelayRequestInterceptor
readTimeout: 60000
connectTimeout: 60000

rocketmq:
name-server: 192.168.138.131:9876
Expand Down

0 comments on commit 0e4aeb8

Please sign in to comment.