|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.plugin.rate.limiter.centre.support; |
| 19 | + |
| 20 | +import com.alibaba.nacos.api.common.Constants; |
| 21 | +import com.alibaba.nacos.api.config.ConfigService; |
| 22 | +import com.alibaba.nacos.api.exception.NacosException; |
| 23 | +import com.alibaba.nacos.client.config.listener.impl.PropertiesListener; |
| 24 | +import com.alibaba.nacos.spring.util.NacosUtils; |
| 25 | +import org.minbox.framework.api.boot.common.exception.ApiBootException; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.springframework.util.Assert; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +import java.util.Properties; |
| 32 | + |
| 33 | +/** |
| 34 | + * Nacos Config Support |
| 35 | + * |
| 36 | + * @author:恒宇少年 - 于起宇 |
| 37 | + * <p> |
| 38 | + * DateTime:2019-05-06 18:02 |
| 39 | + * Blog:http://blog.yuqiyu.com |
| 40 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 41 | + * Gitee:https://gitee.com/hengboy |
| 42 | + * GitHub:https://github.com/hengboy |
| 43 | + */ |
| 44 | +public class NacosRateLimiterConfigCentre extends AbstractRateLimiterConfigCentre { |
| 45 | + /** |
| 46 | + * logger instance |
| 47 | + */ |
| 48 | + static Logger logger = LoggerFactory.getLogger(NacosRateLimiterConfigCentre.class); |
| 49 | + /** |
| 50 | + * Nacos Config Service |
| 51 | + */ |
| 52 | + private ConfigService configService; |
| 53 | + /** |
| 54 | + * rate limiter config properties |
| 55 | + */ |
| 56 | + private Properties configProperties; |
| 57 | + |
| 58 | + public NacosRateLimiterConfigCentre(ConfigService configService) { |
| 59 | + this.configService = configService; |
| 60 | + |
| 61 | + // check configService not null |
| 62 | + Assert.notNull(configService, "ConfigService is required."); |
| 63 | + |
| 64 | + // load config data from nacos |
| 65 | + String configData = loadConfigData(); |
| 66 | + |
| 67 | + // convert config data to properties |
| 68 | + this.configProperties = toProperties(configData); |
| 69 | + logger.info("ApiBoot RateLimiter nacos config properties load complete."); |
| 70 | + |
| 71 | + // Enable monitoring of receiving configuration changes |
| 72 | + addConfigChangeListener(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * get QPS config from nacos |
| 77 | + * for example properties: |
| 78 | + * 1. /user/list -> .user.list: 5 |
| 79 | + * 2. /user/detail -> .user.detail: 10 |
| 80 | + * |
| 81 | + * @param configKey config key |
| 82 | + * @return qps |
| 83 | + * @throws ApiBootException |
| 84 | + */ |
| 85 | + @Override |
| 86 | + public Long getQps(String configKey) throws ApiBootException { |
| 87 | + try { |
| 88 | + String propertyKey = formatPropertyKey(configKey); |
| 89 | + String qps = this.configProperties.getProperty(propertyKey); |
| 90 | + if (!StringUtils.isEmpty(qps)) { |
| 91 | + return Long.valueOf(qps); |
| 92 | + } |
| 93 | + } catch (Exception e) { |
| 94 | + logger.error(e.getMessage(), e); |
| 95 | + } |
| 96 | + return DEFAULT_QPS; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * set qps config to nacos |
| 101 | + * |
| 102 | + * @param configKey config key |
| 103 | + * @param QPS QPS value |
| 104 | + * @throws ApiBootException |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public void setQps(String configKey, Long QPS) throws ApiBootException { |
| 108 | + try { |
| 109 | + String propertyKey = formatPropertyKey(configKey); |
| 110 | + // update local cache config |
| 111 | + this.configProperties.setProperty(propertyKey, String.valueOf(QPS)); |
| 112 | + // convert properties to string |
| 113 | + String configData = fromProperties(this.configProperties); |
| 114 | + if (!StringUtils.isEmpty(configData)) { |
| 115 | + // execute publish config |
| 116 | + this.configService.publishConfig(DATA_ID, Constants.DEFAULT_GROUP, configData); |
| 117 | + logger.info("ApiBoot RateLimiter nacos config publish successfully."); |
| 118 | + } |
| 119 | + } catch (Exception e) { |
| 120 | + throw new ApiBootException(e.getMessage(), e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * load rate limiter config data |
| 126 | + * |
| 127 | + * @return config data |
| 128 | + * @throws ApiBootException ApiBoot Exception |
| 129 | + */ |
| 130 | + protected String loadConfigData() throws ApiBootException { |
| 131 | + try { |
| 132 | + return configService.getConfig(DATA_ID, Constants.DEFAULT_GROUP, NacosUtils.DEFAULT_TIMEOUT); |
| 133 | + } catch (NacosException e) { |
| 134 | + logger.error(e.getMessage(), e); |
| 135 | + } |
| 136 | + throw new ApiBootException("Load ApiBoot RateLimiter config data fail."); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * add config change listener |
| 141 | + * If you modify the configuration through the nacos configuration center, |
| 142 | + * listen to the changes and update the content Properties. |
| 143 | + */ |
| 144 | + protected void addConfigChangeListener() throws ApiBootException { |
| 145 | + try { |
| 146 | + configService.addListener(DATA_ID, Constants.DEFAULT_GROUP, new PropertiesListener() { |
| 147 | + /** |
| 148 | + * Update local properties |
| 149 | + * @param properties Changed configuration properties |
| 150 | + */ |
| 151 | + @Override |
| 152 | + public void innerReceive(Properties properties) { |
| 153 | + configProperties = properties; |
| 154 | + logger.info("Update local current RateLimiter configuration is complete,content:{}", properties.toString()); |
| 155 | + } |
| 156 | + }); |
| 157 | + } catch (Exception e) { |
| 158 | + logger.error("Configuration change listener failed to open.", e); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments