# springcloud-eureka-feign-mybatis-seata ### 整合步奏
阿新 • • 發佈:2021-06-11
springcloud-eureka-feign-mybatis-seata
整合步奏
- 1.下載seata-server,修改seate-server配置
- 2.client端(你自己的專案)拷貝seate-server中的file.conf, registry.conf 加入自己專案
- 3.資料來源代理設定
- 4.建立資料庫表
- 5.啟動註冊中心,啟動seate-server,啟動client
技術選型及版本
註冊中心:eureka
服務間呼叫:feign
持久層:mybatis
資料庫:mysql 5.7.20
Springboot:2.1.8.RELEASE
Springcloud:Greenwich.SR2
jdk:1.8
seata:0.8
file.conf
## transaction log store store { ## store mode: file、db mode = "db" 修改這裡,表明事務資訊用db儲存 ## file store 當mode=db時,此部分配置就不生效了,這是mode=file的配置 file { dir = "sessionStore" # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions max-branch-session-size = 16384 # globe session size , if exceeded throws exceptions max-global-session-size = 512 # file buffer size , if exceeded allocate new buffer file-write-buffer-cache-size = 16384 # when recover batch read size session.reload.read_size = 100 # async, sync flush-disk-mode = async } ## database store mode=db時,事務日誌儲存會儲存在這個配置的資料庫裡 db { ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc. datasource = "dbcp" ## mysql/oracle/h2/oceanbase etc. db-type = "mysql" driver-class-name = "com.mysql.jdbc.Driver" url = "jdbc:mysql://116.62.62.26/seat-server" 修改這裡 user = "root" 修改這裡 password = "root" 修改這裡 min-conn = 1 max-conn = 3 global.table = "global_table" branch.table = "branch_table" lock-table = "lock_table" query-limit = 100 } }
由於此demo我們使用db模式儲存事務日誌,所以,我們要建立三張表:global_table,branch_table,lock_table 資料表在下載的seat-server/conf中
由於儲存undo_log是在業務庫中,所以在每個業務庫中,還要建立undo_log表 資料表在下載的seat-server/conf中
由於我自定義了事務組名稱,所以這裡也做了修改:
service { #vgroup->rgroup # seata版本1.0.0後客戶端file.conf檔案中vgroup_mapping要改成vgroupMapping vgroup_mapping.fsp_tx_group = "default" 修改這裡,fsp_tx_group這個事務組名稱是我自定義的,一定要與client端的這個配置一致!否則會報錯! #only support single node default.grouplist = "127.0.0.1:8091" #degrade current not support enableDegrade = false #disable disable = false #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent max.commit.retry.timeout = "-1" max.rollback.retry.timeout = "-1" }
registry.conf
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka" 修改這裡,指明註冊中心使用什麼
nacos {
serverAddr = "localhost"
namespace = ""
cluster = "default"
}
eureka {
serviceUrl = "http://localhost:10086/eureka" 修改這裡
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = "0"
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
如果是在windows下啟動seata-server,現在已經完成配置修改了,等eureka啟動後,就可以啟動seata-server了:執行/bin/seata-server.bat即可。預設埠8091
application.yml
所有專案的application.yml大致相同
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:10086/eureka/
feign:
hystrix:
enabled: false
client:
config:
default:
connectTimeout: 5000
readTimeout: 10000
logging:
level:
io:
seata: info
mybatis:
mapperLocations: classpath:mapper/*.xml #配置mapper.xml地址
typeAliasesPackage: com.order.pojo #別名
server:
port: 8080
spring:
application:
name: order-server
cloud:
alibaba:
seata:
tx-service-group: fsp_tx_group
#這個fsp_tx_group自定義命名很重要,server,client都要保持一致
# vgroupMapping.fsp_tx_group = "default" 在這裡有使用
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/order?useUnicode=ture&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
配置資料來源代理
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 Admin
*/
@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();
}
}
@Override
@GlobalTransactional //開啟seate事務控制
public void create(Order order) {
//本地方法
orderDao.create(order);
//遠端方法
accountClient.decrease(new BigDecimal(10));
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.account.mapper.AccountDao" ><!-- 關聯的dao介面-->
<resultMap id="BaseResultMap" type="com.account.pojo.Account" ><!--關聯的實體類-->
<id column="id" property="id" jdbcType="BIGINT" />
<result column="user_id" property="userId" jdbcType="BIGINT" />
<result column="total" property="total" jdbcType="DECIMAL" />
<result column="used" property="used" jdbcType="DECIMAL" />
<result column="residue" property="residue" jdbcType="DECIMAL" />
</resultMap>
<update id="decrease">
UPDATE account SET residue = residue - #{money},used = used + #{money} where user_id = #{userId};
</update>
</mapper>
import org.mybatis.spring.annotation.MapperScan;
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;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@MapperScan("com.account.mapper")
@EnableDiscoveryClient
@EnableFeignClients
public class AccountServerApplication {
public static void main(String[] args) {
SpringApplication.run(AccountServerApplication.class, args);
}
}