GatewayServiceDiscoveryController.java 7.6 KB
Newer Older
Z
zengqiao 已提交
1 2 3 4 5 6
package com.xiaojukeji.kafka.manager.web.api.versionone.gateway;

import com.alibaba.fastjson.JSON;
import com.xiaojukeji.kafka.manager.common.annotations.ApiLevel;
import com.xiaojukeji.kafka.manager.common.bizenum.gateway.GatewayConfigKeyEnum;
import com.xiaojukeji.kafka.manager.common.constant.ApiLevelContent;
Z
zengqiao 已提交
7
import com.xiaojukeji.kafka.manager.common.entity.Result;
Z
zengqiao 已提交
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
import com.xiaojukeji.kafka.manager.common.entity.ao.gateway.*;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.GatewayConfigDO;
import com.xiaojukeji.kafka.manager.common.entity.vo.gateway.GatewayConfigVO;
import com.xiaojukeji.kafka.manager.common.utils.ListUtils;
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
import com.xiaojukeji.kafka.manager.service.service.gateway.GatewayConfigService;
import com.xiaojukeji.kafka.manager.common.constant.ApiPrefix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zengqiao
 * @date 20/7/27
 */
@Api(tags = "GATEWAY-服务发现相关接口(REST)")
@RestController
@RequestMapping(ApiPrefix.GATEWAY_API_V1_PREFIX)
public class GatewayServiceDiscoveryController {
    @Autowired
    private GatewayConfigService gatewayConfigService;

    @ApiLevel(level = ApiLevelContent.LEVEL_VIP_1)
    @ApiOperation(value = "获取指定集群服务地址", notes = "")
    @RequestMapping(value = "discovery/address", method = RequestMethod.GET)
    @ResponseBody
    public String getKafkaBootstrapServer(@RequestParam("clusterId") Long clusterId) {
        if (ValidateUtils.isNull(clusterId)) {
            return "";
        }
        GatewayConfigDO configDO = gatewayConfigService.getByTypeAndName(
                GatewayConfigKeyEnum.SD_CLUSTER_ID.getConfigType(),
                String.valueOf(clusterId)
        );
        if (ValidateUtils.isNull(configDO)) {
            return "";
        }
        return configDO.getValue();
    }

    @ApiLevel(level = ApiLevelContent.LEVEL_VIP_1)
    @ApiOperation(value = "获取集群服务地址", notes = "")
    @RequestMapping(value = "discovery/init", method = RequestMethod.GET)
    @ResponseBody
Z
zengqiao 已提交
57
    public Result<String> getAllKafkaBootstrapServers() {
Z
zengqiao 已提交
58 59 60
        KafkaBootstrapServerConfig config =
                gatewayConfigService.getKafkaBootstrapServersConfig(Long.MIN_VALUE);
        if (ValidateUtils.isNull(config) || ValidateUtils.isNull(config.getClusterIdBootstrapServersMap())) {
Z
zengqiao 已提交
61
            return Result.buildFailure("call init kafka bootstrap servers failed");
Z
zengqiao 已提交
62
        }
Z
zengqiao 已提交
63 64 65
        if (ValidateUtils.isEmptyMap(config.getClusterIdBootstrapServersMap())) {
            return Result.buildSuc();
        }
Z
zengqiao 已提交
66
        return Result.buildSuc(JSON.toJSONString(config.getClusterIdBootstrapServersMap()));
Z
zengqiao 已提交
67 68 69 70 71 72
    }

    @ApiLevel(level = ApiLevelContent.LEVEL_IMPORTANT_2)
    @ApiOperation(value = "获取集群服务地址", notes = "")
    @RequestMapping(value = "discovery/update", method = RequestMethod.GET)
    @ResponseBody
Z
zengqiao 已提交
73
    public Result<String> getBootstrapServersIfNeeded(@RequestParam("versionNumber") long versionNumber) {
Z
zengqiao 已提交
74 75 76
        KafkaBootstrapServerConfig config =
                gatewayConfigService.getKafkaBootstrapServersConfig(versionNumber);
        if (ValidateUtils.isNull(config) || ValidateUtils.isNull(config.getClusterIdBootstrapServersMap())) {
Z
zengqiao 已提交
77
            return Result.buildFailure("call update kafka bootstrap servers failed");
Z
zengqiao 已提交
78
        }
Z
zengqiao 已提交
79 80 81
        if (ValidateUtils.isEmptyMap(config.getClusterIdBootstrapServersMap())) {
            return Result.buildSuc();
        }
Z
zengqiao 已提交
82
        return Result.buildSuc(JSON.toJSONString(new GatewayConfigVO(
Z
zengqiao 已提交
83 84 85 86 87 88 89 90 91
                String.valueOf(config.getVersion()),
                JSON.toJSONString(config.getClusterIdBootstrapServersMap())
        )));
    }

    @ApiLevel(level = ApiLevelContent.LEVEL_IMPORTANT_2)
    @ApiOperation(value = "最大并发请求数", notes = "")
    @RequestMapping(value = "discovery/max-request-num", method = RequestMethod.GET)
    @ResponseBody
Z
zengqiao 已提交
92
    public Result<String> getMaxRequestNum(@RequestParam("versionNumber") long versionNumber) {
Z
zengqiao 已提交
93 94
        RequestQueueConfig config = gatewayConfigService.getRequestQueueConfig(versionNumber);
        if (ValidateUtils.isNull(config)) {
Z
zengqiao 已提交
95
            return Result.buildFailure("call get request queue size config failed");
Z
zengqiao 已提交
96
        }
Z
zengqiao 已提交
97 98 99
        if (ValidateUtils.isNull(config.getMaxRequestQueueSize())) {
            return Result.buildSuc();
        }
Z
zengqiao 已提交
100
        return Result.buildSuc(JSON.toJSONString(
Z
zengqiao 已提交
101 102 103 104 105 106 107 108 109 110 111
                new GatewayConfigVO(
                        String.valueOf(config.getVersion()),
                        String.valueOf(config.getMaxRequestQueueSize())
                )
        ));
    }

    @ApiLevel(level = ApiLevelContent.LEVEL_IMPORTANT_2)
    @ApiOperation(value = "最大APP请求速率", notes = "")
    @RequestMapping(value = "discovery/appId-rate", method = RequestMethod.GET)
    @ResponseBody
Z
zengqiao 已提交
112
    public Result<String> getAppIdRate(@RequestParam("versionNumber") long versionNumber) {
Z
zengqiao 已提交
113 114
        AppRateConfig config = gatewayConfigService.getAppRateConfig(versionNumber);
        if (ValidateUtils.isNull(config)) {
Z
zengqiao 已提交
115
            return Result.buildFailure("call get app rate config failed");
Z
zengqiao 已提交
116
        }
Z
zengqiao 已提交
117 118 119
        if (ValidateUtils.isNull(config.getAppRateLimit())) {
            return Result.buildSuc();
        }
Z
zengqiao 已提交
120
        return Result.buildSuc(JSON.toJSONString(
Z
zengqiao 已提交
121 122 123 124 125 126 127 128 129 130 131
                new GatewayConfigVO(
                        String.valueOf(config.getVersion()),
                        String.valueOf(config.getAppRateLimit())
                )
        ));
    }

    @ApiLevel(level = ApiLevelContent.LEVEL_IMPORTANT_2)
    @ApiOperation(value = "最大IP请求速率", notes = "")
    @RequestMapping(value = "discovery/ip-rate", method = RequestMethod.GET)
    @ResponseBody
Z
zengqiao 已提交
132
    public Result getIpRate(@RequestParam("versionNumber") long versionNumber) {
Z
zengqiao 已提交
133 134
        IpRateConfig config = gatewayConfigService.getIpRateConfig(versionNumber);
        if (ValidateUtils.isNull(config)) {
Z
zengqiao 已提交
135
            return Result.buildFailure("call get ip rate config failed");
Z
zengqiao 已提交
136
        }
Z
zengqiao 已提交
137 138 139
        if (ValidateUtils.isNull(config.getIpRateLimit())) {
            return Result.buildSuc();
        }
Z
zengqiao 已提交
140
        return Result.buildSuc(JSON.toJSONString(
Z
zengqiao 已提交
141 142 143 144 145 146 147 148 149 150 151
                new GatewayConfigVO(
                        String.valueOf(config.getVersion()),
                        String.valueOf(config.getIpRateLimit())
                )
        ));
    }

    @ApiLevel(level = ApiLevelContent.LEVEL_IMPORTANT_2)
    @ApiOperation(value = "最大SP请求速率", notes = "")
    @RequestMapping(value = "discovery/sp-limit", method = RequestMethod.GET)
    @ResponseBody
Z
zengqiao 已提交
152
    public Result<String> getSpLimit(@RequestParam("versionNumber") long versionNumber) {
Z
zengqiao 已提交
153 154 155
        SpRateConfig config =
                gatewayConfigService.getSpRateConfig(versionNumber);
        if (ValidateUtils.isNull(config) || ValidateUtils.isNull(config.getSpRateMap())) {
Z
zengqiao 已提交
156
            return Result.buildFailure("call update kafka bootstrap servers failed");
Z
zengqiao 已提交
157
        }
Z
zengqiao 已提交
158 159 160
        if (ValidateUtils.isEmptyMap(config.getSpRateMap())) {
            return Result.buildSuc();
        }
Z
zengqiao 已提交
161 162 163 164 165
        List<String> strList = new ArrayList<>();
        for (Map.Entry<String, Long> entry: config.getSpRateMap().entrySet()) {
            strList.add(entry.getKey() + "#" + String.valueOf(entry.getValue()));
        }

Z
zengqiao 已提交
166
        return Result.buildSuc(JSON.toJSONString(new GatewayConfigVO(
Z
zengqiao 已提交
167 168 169 170 171
                String.valueOf(config.getVersion()),
                ListUtils.strList2String(strList)
        )));
    }
}