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 implements ApiServerService { @Resource private RedisTemplate 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 params) { String serverName = (String)params.get("serverName"); IPage page = this.page( new Query().getPage(params), new QueryWrapper() .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().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 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 ls = new ArrayList<>(); List 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); } }