houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
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
104
105
106
107
108
109
110
111
112
113
114
115
package com.iailab.module.data.gateway.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iailab.module.data.common.utils.PageUtils;
import com.iailab.module.data.common.utils.Query;
import com.iailab.framework.common.constant.CacheConstant;
import com.iailab.framework.common.constant.GlobalConstants;
import com.iailab.module.data.gateway.dao.ApiServerDao;
import com.iailab.module.data.gateway.entity.ApiServerEntity;
import com.iailab.module.data.gateway.service.ApiServerService;
import com.iailab.module.data.gateway.vo.SysGatewayRoute;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2022年07月15日 14:03:00
 */
@Service("apiServerService")
public class ApiServerServiceImpl extends ServiceImpl<ApiServerDao, ApiServerEntity> implements ApiServerService {
 
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
 
    private final int MINS = 60;
 
    private String filterJson = "[{\"args\":[{\"key\":\"parts\",\"value\":\"1\"}],\"name\":\"StripPrefix\"},{\"args\":[{\"key\":\"key-resolver\",\"value\":\"#{@authorizedApiKeyResolver}\"},{\"key\":\"redis-rate-limiter.replenishRate\",\"value\":1},{\"key\":\"redis-rate-limiter.burstCapacity\",\"value\":60},{\"key\":\"redis-rate-limiter.requestedTokens\",\"value\":requestedTokensValue}],\"name\":\"RequestRateLimiter\"}]";
 
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String serverName = (String)params.get("serverName");
 
        IPage<ApiServerEntity> page = this.page(
                new Query<ApiServerEntity>().getPage(params),
                new QueryWrapper<ApiServerEntity>()
                        .like(StringUtils.isNotBlank(serverName),"server_name", serverName)
                        .orderByDesc("create_time")
        );
        return new PageUtils(page);
    }
 
    @Override
    public void add(ApiServerEntity apiServerEntity) {
        apiServerEntity.setId(UUID.randomUUID().toString().replace("-", ""));
        this.save(apiServerEntity);
//        this.addRouteRedis();
    }
 
    @Override
    public void update(ApiServerEntity apiServerEntity) {
        this.updateById(apiServerEntity);
//        this.addRouteRedis();
    }
 
    @Override
    public void deleteById(String id) {
        this.getBaseMapper().delete(new QueryWrapper<ApiServerEntity>().eq("id", id));
    }
 
    @Override
    public ApiServerEntity getInfoById(String id) {
        return this.getById(id);
    }
 
    @Override
    public int cheack(ApiServerEntity apiServerEntity) {
        String id = apiServerEntity.getId();
        String serverName = apiServerEntity.getServerName();
 
        QueryWrapper<ApiServerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(StringUtils.isNotBlank(id), "id", id);
        queryWrapper.and(wrapper -> wrapper.eq("server_name", serverName));
        return (int)this.count(queryWrapper);
    }
 
    @Override
    public void addRouteRedis() {
        List<SysGatewayRoute> ls = new ArrayList<>();
        List<ApiServerEntity> list = getBaseMapper().selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(list)) {
            ls = list.stream().map(item -> {
                SysGatewayRoute sysGatewayRoute = new SysGatewayRoute();
                sysGatewayRoute.setId(item.getId());
                sysGatewayRoute.setRouterId(item.getServerName());
                sysGatewayRoute.setName(item.getServerName());
                sysGatewayRoute.setUri(item.getServerAddress());
                sysGatewayRoute.setPredicates("[{\"args\":[\"/" + item.getServerName() +"/**\"],\"name\":\"Path\"}]");
                sysGatewayRoute.setFilters("[{\"args\":[{\"key\":\"parts\",\"value\":\"1\"}],\"name\":\"StripPrefix\"}]");
                if (item.getLimitMin() != null && item.getLimitMin().compareTo(MINS) <= 0) {
                    String requestedTokensValue = (new BigDecimal(MINS)).divide(new BigDecimal(item.getLimitMin()), 0, BigDecimal.ROUND_FLOOR).toString();
                    sysGatewayRoute.setFilters(filterJson.replace("requestedTokensValue", requestedTokensValue));
                }
                sysGatewayRoute.setStatus(item.getStatus());
                return sysGatewayRoute;
            }).collect(Collectors.toList());
        }
        redisTemplate.opsForValue().set(CacheConstant.GATEWAY_ROUTES, JSON.toJSONString(ls));
        //刷新网关
        redisTemplate.convertAndSend(GlobalConstants.REDIS_TOPIC_NAME, GlobalConstants.LODER_ROUDER_HANDLER);
    }
}