xuhy
2024-09-25 ca825fdfe67223b2eb8a3f9c14b4e60e2339efc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.ruoyi.other.api.factory;
 
import com.ruoyi.common.core.domain.R;
import com.ruoyi.other.api.domain.TUserSite;
import com.ruoyi.other.api.feignClient.UserSiteClient;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * 商品服务降级处理
 * 
 * @author ruoyi
 */
@Component
public class UserSiteFallbackFactory implements FallbackFactory<UserSiteClient> {
    private static final Logger log = LoggerFactory.getLogger(UserSiteFallbackFactory.class);
 
    @Override
    public UserSiteClient create(Throwable throwable) {
        log.error("调用失败:{}", throwable.getMessage());
        return new UserSiteClient() {
    
            @Override
            public R<List<Integer>> getSiteIds(Long userId) {
                return R.fail("获取用户站点失败:" + throwable.getMessage());
            }
    
            @Override
            public R addUserSite(List<TUserSite> userSite) {
                // 手动进行全局事务回滚
                try {
                    GlobalTransactionContext.getCurrent().rollback();
                } catch (TransactionException e) {
                    throw new RuntimeException(e);
                }
                return R.fail("添加用户站点失败:" + throwable.getMessage());
            }
    
            @Override
            public R delUserSite(Long userId) {
                // 手动进行全局事务回滚
                try {
                    GlobalTransactionContext.getCurrent().rollback();
                } catch (TransactionException e) {
                    throw new RuntimeException(e);
                }
                return R.fail("删除用户站点失败:" + throwable.getMessage());
            }
        };
    }
}