springcloud整合分布式事务LCN

java1234

共 11708字,需浏览 24分钟

 ·

2020-12-24 00:48

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

  作者 |  Neil-King

来源 |  urlify.cn/ZRruey

66套java从入门到精通实战课程分享

一、创建eureka注册中心

a、pom文件

 1 
 2         1.8
 3         Greenwich.SR2
 4     

 5 
 6     
 7         
 8             org.springframework.boot
 9             spring-boot-starter-web
10         

11         
12             org.springframework.cloud
13             spring-cloud-starter-netflix-eureka-server
14         

15 
16         
17             org.springframework.boot
18             spring-boot-starter-test
19             test
20         

21     

22 
23     
24         
25             
26                 org.springframework.cloud
27                 spring-cloud-dependencies
28                 ${spring-cloud.version}
29                 <type>pomtype>
30                 import
31             

32         

33     

b、properties文件

server.port=8761
 
#禁止将自己注册到注册中心
eureka.client.register-with-eureka=false
eureka.instance.hostname=localhost
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.server.enable-self-preservation=false
spring.cloud.config.discovery.enabled=true

c、在启动类上加上@EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudEurekaApplication.class, args);
    }
 
}

d、启动该服务

二、创建微服务A

a、pom文件

 1 
 2         1.8
 3         Greenwich.SR2
 4     

 5 
 6     
 7         
 8             org.springframework.boot
 9             spring-boot-starter-data-jpa
10         

11         
12             org.springframework.boot
13             spring-boot-starter-web
14         

15         
16             org.springframework.cloud
17             spring-cloud-starter-netflix-eureka-client
18         

19 
20         
21             mysql
22             mysql-connector-java
23             runtime
24         

25         
26             org.springframework.boot
27             spring-boot-starter-test
28             test
29         

30         
31             com.alibaba
32             druid
33             1.0.9
34         

35         
36             org.springframework.cloud
37             spring-cloud-starter-openfeign
38         

39 

b、properties文件

spring.application.name=springcloud-aservice
server.port=8080
 
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
 
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/yzh?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
 
#################jpa配置####################
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

c、在启动类上添加@EnableEurekaClient注解

d、创建entity

 1 @Entity
 2 public class Orders implements Serializable{
 3 
 4     private static final long serialVersionUID = 3295617400035010415L;
 5 
 6     @Id
 7     @GeneratedValue(strategy = GenerationType.IDENTITY)
 8     private Integer orderId;
 9 
10     private Integer itemId;
11 
12     private Integer price;
13 
14     public Integer getOrderId() {
15         return orderId;
16     }
17 
18     public void setOrderId(Integer orderId) {
19         this.orderId = orderId;
20     }
21 
22     public Integer getItemId() {
23         return itemId;
24     }
25 
26     public void setItemId(Integer itemId) {
27         this.itemId = itemId;
28     }
29 
30     public Integer getPrice() {
31         return price;
32     }
33 
34     public void setPrice(Integer price) {
35         this.price = price;
36     }
37 }

e、创建dao

public interface OrderDao extends JpaRepository {
}


f、创建service

 1 @Service
 2 public class OrderService {
 3 
 4     @Autowired
 5     private OrderDao orderDao;
 6     @Autowired
 7     private InventoryService inventoryService;
 8 
 9     @Transactional
10     public Orders addOrder(){
11         Orders orders = new Orders();
12         orders.setItemId(100);
13         orders.setPrice(2000);
14 
15         Orders save = orderDao.save(orders);
16 
17         inventoryService.updateInventory(100,9);
18 
19         return save;
20     }
21 }

g、创建feign调用

 1 @FeignClient(value = "springcloud-bservice",fallback = InventoryServiceFallback.class)
 2 public interface InventoryService {
 3 
 4     @GetMapping(value = "updateInventory")
 5      Object updateInventory(@RequestParam(value = "itemId") Integer itemId,@RequestParam(value = "itemNum") Integer itemNum);
 6 
 7 }
 8 
 9 //回调类
10 public class InventoryServiceFallback implements InventoryService{
11     @Override
12     public Object updateInventory(Integer itemId, Integer itemNum) {
13         return 0;
14     }
15 }

h、在启动类上添加@EnableFeignClients注解

i、创建web

@RestController
public class OrdersController {
 
    @Autowired
    private OrderService orderService;
 
    @RequestMapping("/addOrders")
    public Object addOrders(){
        return orderService.addOrder();
    }
}

三、创建微服务B

a、pom文件

 1 
 2         1.8
 3         Greenwich.SR2
 4     

 5 
 6     
 7         
 8             org.springframework.boot
 9             spring-boot-starter-data-jpa
10         

11         
12             org.springframework.boot
13             spring-boot-starter-web
14         

15         
16             org.springframework.cloud
17             spring-cloud-starter-netflix-eureka-client
18         

19 
20         
21             mysql
22             mysql-connector-java
23             runtime
24         

25         
26             org.springframework.boot
27             spring-boot-starter-test
28             test
29         

30 

b、properties文件

spring.application.name=springcloud-bservice
server.port=8081
 
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
 
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/yzh?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
 
#################jpa配置####################
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

c、在启动类上添加@EnableEurekaClient注解

d、创建entity

 1 @Entity
 2 @Table(name = "tb_inventory")
 3 public class TbInventory implements Serializable{
 4     private static final long serialVersionUID = 4171468306443543867L;
 5 
 6     @Id
 7     @GeneratedValue(strategy = GenerationType.IDENTITY)
 8     private Integer inventoryId;
 9 
10     private Integer itemId;
11 
12     private Integer itemnum;
13 
14     public Integer getInventoryId() {
15         return inventoryId;
16     }
17 
18     public void setInventoryId(Integer inventoryId) {
19         this.inventoryId = inventoryId;
20     }
21 
22     public Integer getItemId() {
23         return itemId;
24     }
25 
26     public void setItemId(Integer itemId) {
27         this.itemId = itemId;
28     }
29 
30     public Integer getItemnum() {
31         return itemnum;
32     }
33 
34     public void setItemnum(Integer itemnum) {
35         this.itemnum = itemnum;
36     }
37 }

e、创建dao

public interface TbInventoryDao extends JpaRepository {
}


f、创建service

 1 @Service
 2 public class TbInventoryService {
 3 
 4     @Autowired
 5     private TbInventoryDao tbInventoryDao;
 6 
 7 
 8     @Transactional
 9     public TbInventory updateInventory(Integer itemId,Integer itemNum){
10         TbInventory tbInventory = new TbInventory();
11         tbInventory.setItemId(itemId);
12         tbInventory.setItemnum(itemNum);
13         TbInventory save = tbInventoryDao.save(tbInventory);
14         System.err.println(1/0);
15         return  save;
16 
17     }
18 }

g、创建web

@RestController
public class TbInventoryController {
 
    @Autowired
    private TbInventoryService tbInventoryService;
 
    @GetMapping("updateInventory")
    public Object updateInventory(@RequestParam Integer itemId, @RequestParam Integer itemNum){
        return tbInventoryService.updateInventory(itemId,itemNum);
    }
}

四、引入LCN

a、下载地址:https://github.com/codingapi/tx-lcn/releases

(此处下载的是版本5.0.2.RELEASE)

 b、将项目txlcn-tm导入idea,并修改properties文件

spring.application.name=tx-manager
server.port=7970
 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tx-manager?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
 
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.use-generated-keys=true
 
#tx-lcn.logger.enabled=true
# TxManager Host Ip
#tx-lcn.manager.host=127.0.0.1
# TxClient连接请求端口
#tx-lcn.manager.port=8070
# 心跳检测时间(ms)
#tx-lcn.manager.heart-time=15000
# 分布式事务执行总时间
#tx-lcn.manager.dtx-time=30000
#参数延迟删除时间单位ms
#tx-lcn.message.netty.attr-delay-time=10000
#tx-lcn.manager.concurrent-level=128
# 开启日志
#tx-lcn.logger.enabled=true
#logging.level.com.codingapi=debug
#redis 主机
#spring.redis.host=127.0.0.1
#redis 端口
#spring.redis.port=6379
#redis 密码
#spring.redis.password=

    • # 给出信息都是默认值
      关于详细配置说明见TM配置

    • application.properties 加载顺序如下:
      0、命令行启动参数指定
      1、file:./config/(当前jar目录下的config目录)
      2、file:./(当前jar目录)
      3、classpath:/config/(classpath下的config目录)
      4、classpath:/(classpath根目录)
      发布的二进制可执行Jar包含一个默认配置文件(也就是4),可按需要覆盖默认配置

c、创建MySQL数据库, 名称为: tx-manager、然后创建数据表

CREATE TABLE `t_tx_exception`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `unit_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `mod_id` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `transaction_state` tinyint(4) NULL DEFAULT NULL,
  `registrar` tinyint(4) NULL DEFAULT NULL,
  `remark` varchar(4096) NULL DEFAULT  NULL,
  `ex_state` tinyint(4) NULL DEFAULT NULL COMMENT '0 未解决 1已解决',
  `create_time` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

d、将项目改造成springcloud项目,并注册到eureka中

 1  
 2         1.8
 3         Finchley.RELEASE
 4         2.1.7.RELEASE
 5     

 6 
 7 
 8             org.springframework.boot
 9             spring-boot-starter-data-redis
10             ${spring-boot.version}
11         

12 
13         
14             org.springframework.boot
15             spring-boot-starter-mail
16             ${spring-boot.version}
17         

18  
19             org.springframework.boot
20             spring-boot-starter-data-jpa
21             ${spring-boot.version}
22         

23         
24             org.springframework.cloud
25             spring-cloud-starter-netflix-eureka-server
26         

注意:为了防止版本冲突需要知道springboot的版本为2.1.x,并且设置springcloud的版本为Finchley.RELEASE,还要在properties文件中新增配置:spring.cloud.compatibility-verifier.enabled=false

c、将项目注册到eureka中

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

d、在启动类上添加@EnableEurekaClient注解

五、开始整合

a、在服务A、B的pom文件中引入坐标

 1 
 2             com.codingapi.txlcn
 3             txlcn-tc
 4             5.0.2.RELEASE
 5             
 6                 
 7                     org.springframework.boot
 8                     *
 9                 

10             

11         

12 
13         
14             com.codingapi.txlcn
15             txlcn-txmsg-netty
16             5.0.2.RELEASE
17             
18                 
19                     org.springframework.boot
20                     *
21                 

22             

23         

注意:为了防止springboot的jar包冲突,需要将里面关于springboot的所有jar包排除掉。

b、在服务A、B的启动类上添加注解@EnableDistributedTransaction启动分布式事务

c、在服务A中的需要分布式事务方法上面添加@LcnTransaction,服务B中添加@TxcTransaction注解。

六、启动、测试

a、依次启动eureka注册中心,txlcn-tm,服务A、B

 

 b、调用服务




粉丝福利:Java从入门到入土学习路线图

???

?长按上方微信二维码 2 秒


感谢点赞支持下哈 

浏览 18
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报