Seata 整合学习完成
This commit is contained in:
		@@ -20,7 +20,7 @@
 | 
			
		||||
    <dependencies>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>com.alibaba.cloud</groupId>
 | 
			
		||||
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 | 
			
		||||
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
 | 
			
		||||
        </dependency>
 | 
			
		||||
        <dependency>
 | 
			
		||||
            <groupId>org.springframework.cloud</groupId>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
package com.test;
 | 
			
		||||
 | 
			
		||||
import io.seata.spring.annotation.datasource.EnableAutoDataSourceProxy;
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
 | 
			
		||||
@@ -11,6 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
 * @Create 2023/8/14 16:26
 | 
			
		||||
 */
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
@EnableAutoDataSourceProxy
 | 
			
		||||
public class UserApplication {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        SpringApplication.run(UserApplication.class, args);
 | 
			
		||||
 
 | 
			
		||||
@@ -27,4 +27,15 @@ public class UserController {
 | 
			
		||||
        System.out.println("调用用户服务");
 | 
			
		||||
        return service.getUserById(uid);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/user/remain/{uid}")
 | 
			
		||||
    public int userRemain(@PathVariable("uid") int uid) {
 | 
			
		||||
        return service.getRemain(uid);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/user/borrow/{uid}")
 | 
			
		||||
    public boolean userBorrow(@PathVariable("uid") int uid) {
 | 
			
		||||
        int remain = service.getRemain(uid);
 | 
			
		||||
        return service.setRemain(uid, remain - 1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package com.test.mapper;
 | 
			
		||||
import com.test.entity.User;
 | 
			
		||||
import org.apache.ibatis.annotations.Mapper;
 | 
			
		||||
import org.apache.ibatis.annotations.Select;
 | 
			
		||||
import org.apache.ibatis.annotations.Update;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: UserMapper
 | 
			
		||||
@@ -15,4 +16,10 @@ import org.apache.ibatis.annotations.Select;
 | 
			
		||||
public interface UserMapper {
 | 
			
		||||
    @Select("select * from DB_USER where uid = #{uid}")
 | 
			
		||||
    User getUserById(int uid);
 | 
			
		||||
 | 
			
		||||
    @Select("select book_count from DB_USER where uid = #{uid}")
 | 
			
		||||
    int getUserBookRemain(int uid);
 | 
			
		||||
 | 
			
		||||
    @Update("update DB_USER set book_count = #{count} where uid = #{uid}")
 | 
			
		||||
    int updateBookCount(int uid, int count);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,4 +11,8 @@ import com.test.entity.User;
 | 
			
		||||
 */
 | 
			
		||||
public interface UserService {
 | 
			
		||||
    User getUserById(int uid);
 | 
			
		||||
 | 
			
		||||
    int getRemain(int uid);
 | 
			
		||||
 | 
			
		||||
    boolean setRemain(int uid, int count);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -24,4 +24,15 @@ public class UserServiceImpl implements UserService {
 | 
			
		||||
    public User getUserById(int uid) {
 | 
			
		||||
        return mapper.getUserById(uid);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getRemain(int uid) {
 | 
			
		||||
        return mapper.getUserBookRemain(uid);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean setRemain(int uid, int count) {
 | 
			
		||||
        return mapper.updateBookCount(uid, count) > 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,20 @@ spring:
 | 
			
		||||
      discovery:
 | 
			
		||||
        # 配置Nacos注册中心地址
 | 
			
		||||
        server-addr: localhost:8848
 | 
			
		||||
        namespace: dd668135-0bfe-489f-ab2b-24aefb21d156
 | 
			
		||||
    sentinel:
 | 
			
		||||
      transport:
 | 
			
		||||
        # 添加监控页面地址即可
 | 
			
		||||
        dashboard: localhost:8858
 | 
			
		||||
seata:
 | 
			
		||||
  # 注册
 | 
			
		||||
  registry:
 | 
			
		||||
    # 使用Nacos
 | 
			
		||||
    type: nacos
 | 
			
		||||
    nacos:
 | 
			
		||||
      # 使用Seata的命名空间,这样才能正确找到Seata服务,由于组使用的是SEATA_GROUP,配置默认值就是,就不用配了
 | 
			
		||||
      namespace: c0495138-b5b3-440d-8f1c-dc8cc9ba5fbc
 | 
			
		||||
      username: nacos
 | 
			
		||||
      password: nacos
 | 
			
		||||
  # 配置
 | 
			
		||||
  config:
 | 
			
		||||
    type: nacos
 | 
			
		||||
    nacos:
 | 
			
		||||
      namespace: c0495138-b5b3-440d-8f1c-dc8cc9ba5fbc
 | 
			
		||||
      username: nacos
 | 
			
		||||
      password: nacos
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user