潘志宝
5 天以前 2780e6717df31ee605dd8ce525afcb43914ca2de
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.iailab.framework.tenant.core.service;
 
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.iailab.framework.common.pojo.CommonResult;
import com.iailab.framework.common.util.cache.CacheUtils;
import com.iailab.module.system.api.tenant.TenantApi;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
 
import com.iailab.module.system.api.tenant.dto.TenantDataSourceConfigRespDTO;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
 
import java.time.Duration;
import java.util.List;
 
import static com.iailab.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
 
/**
 * Tenant 框架 Service 实现类
 *
 * @author iailab
 */
@RequiredArgsConstructor
public class TenantFrameworkServiceImpl implements TenantFrameworkService {
 
    private final TenantApi tenantApi;
 
    /**
     * 针对 {@link #getTenantIds()} 的缓存
     */
    private final LoadingCache<Object, List<Long>> getTenantIdsCache = buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<Object, List<Long>>() {
 
                @Override
                public List<Long> load(Object key) {
                    return tenantApi.getTenantIdList().getCheckedData();
                }
 
            });
 
    /**
     * 针对 {@link #validTenant(Long)} 的缓存
     */
    private final LoadingCache<Long, CommonResult<Boolean>> validTenantCache = buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<Long, CommonResult<Boolean>>() {
 
                @Override
                public CommonResult<Boolean> load(Long id) {
                    return tenantApi.validTenant(id);
                }
 
            });
 
    /**
     * 针对 {@link #getDataSourceProperty(Long)} 的缓存
     */
    private final LoadingCache<Long, DataSourceProperty> dataSourcePropertyCache = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<Long, DataSourceProperty>() {
 
                @Override
                public DataSourceProperty load(Long id) {
                    // 获得租户对应的数据源配置
                    TenantDataSourceConfigRespDTO dataSourceConfig = tenantApi.getTenantDataSourceConfig(id);
                    if (dataSourceConfig == null) {
                        return null;
                    }
                    // 转换成 dynamic-datasource 配置
//                    return new DataSourceProperty()
//                            .setPoolName(dataSourceConfig.getName()).setUrl(dataSourceConfig.getUrl())
//                            .setUsername(dataSourceConfig.getUsername()).setPassword(dataSourceConfig.getPassword());
 
                    DataSourceProperty ds = new DataSourceProperty();
                    ds.setPoolName(dataSourceConfig.getName());
                    ds.setUrl(dataSourceConfig.getUrl());
                    ds.setUsername(dataSourceConfig.getUsername());
                    ds.setPassword(dataSourceConfig.getPassword());
                    return ds;
                }
 
            });
 
    @Override
    @SneakyThrows
    public List<Long> getTenantIds() {
        return getTenantIdsCache.get(Boolean.TRUE);
    }
 
    @Override
    @SneakyThrows
    public void validTenant(Long id) {
        validTenantCache.get(id).checkError();
    }
 
    @Override
    @SneakyThrows
    public DataSourceProperty getDataSourceProperty(Long id) {
        return dataSourcePropertyCache.get(id);
    }
}