|
| 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.autoconfigure.ratelimiter; |
| 19 | + |
| 20 | +import com.alibaba.boot.nacos.config.autoconfigure.NacosConfigAutoConfiguration; |
| 21 | +import com.alibaba.boot.nacos.config.properties.NacosConfigProperties; |
| 22 | +import com.alibaba.nacos.api.NacosFactory; |
| 23 | +import com.alibaba.nacos.api.config.ConfigService; |
| 24 | +import com.alibaba.nacos.api.exception.NacosException; |
| 25 | +import org.minbox.framework.api.boot.plugin.rate.limiter.centre.RateLimiterConfigCentre; |
| 26 | +import org.minbox.framework.api.boot.plugin.rate.limiter.centre.support.NacosRateLimiterConfigCentre; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 30 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 32 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 33 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | + |
| 37 | +import java.util.Objects; |
| 38 | +import java.util.Properties; |
| 39 | + |
| 40 | +import static com.alibaba.nacos.api.PropertyKeyConst.*; |
| 41 | + |
| 42 | +/** |
| 43 | + * Nacos Implementation of Distributed Configuration Center Used by ApiBoot |
| 44 | + * |
| 45 | + * @author:恒宇少年 - 于起宇 |
| 46 | + * <p> |
| 47 | + * DateTime:2019-05-07 09:30 |
| 48 | + * Blog:http://blog.yuqiyu.com |
| 49 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 50 | + * Gitee:https://gitee.com/hengboy |
| 51 | + * GitHub:https://github.com/hengboy |
| 52 | + */ |
| 53 | +@Configuration |
| 54 | +@ConditionalOnClass(NacosConfigProperties.class) |
| 55 | +@EnableConfigurationProperties({NacosConfigProperties.class}) |
| 56 | +@AutoConfigureAfter(NacosConfigAutoConfiguration.class) |
| 57 | +public class ApiBootRateLimiterNacosConfigConfiguration { |
| 58 | + /** |
| 59 | + * logger instance |
| 60 | + */ |
| 61 | + static Logger logger = LoggerFactory.getLogger(ApiBootRateLimiterNacosConfigConfiguration.class); |
| 62 | + /** |
| 63 | + * nacos config service name |
| 64 | + */ |
| 65 | + static final String NACOS_CONFIG_SERVICE_NAME = "nacosConfigService"; |
| 66 | + /** |
| 67 | + * Nacos Config Properties |
| 68 | + */ |
| 69 | + private NacosConfigProperties nacosConfigProperties; |
| 70 | + |
| 71 | + public ApiBootRateLimiterNacosConfigConfiguration(NacosConfigProperties nacosConfigProperties) { |
| 72 | + this.nacosConfigProperties = nacosConfigProperties; |
| 73 | + logger.info("ApiBoot RateLimiter nacos config centre is instantiated."); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Initialize Nacos Configuration Center |
| 78 | + * |
| 79 | + * @return RateLimiterConfigCentre |
| 80 | + * @see NacosRateLimiterConfigCentre |
| 81 | + */ |
| 82 | + @Bean |
| 83 | + @ConditionalOnMissingBean |
| 84 | + public RateLimiterConfigCentre nacosRateLimiterConfigCentre(@Qualifier(NACOS_CONFIG_SERVICE_NAME) ConfigService configService) { |
| 85 | + return new NacosRateLimiterConfigCentre(configService); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * init ConfigService Instance |
| 90 | + * |
| 91 | + * @return NacosConfigService |
| 92 | + * @throws NacosException Nacos Exception |
| 93 | + */ |
| 94 | + @Bean |
| 95 | + @ConditionalOnMissingBean(name = NACOS_CONFIG_SERVICE_NAME) |
| 96 | + public ConfigService nacosConfigService() throws NacosException { |
| 97 | + Properties properties = new Properties(); |
| 98 | + properties.put(SERVER_ADDR, Objects.toString(nacosConfigProperties.getServerAddr(), "")); |
| 99 | + properties.put(ENCODE, Objects.toString(nacosConfigProperties.getEncode(), "")); |
| 100 | + properties.put(NAMESPACE, Objects.toString(nacosConfigProperties.getNamespace(), "")); |
| 101 | + properties.put(ACCESS_KEY, Objects.toString(nacosConfigProperties.getAccessKey(), "")); |
| 102 | + properties.put(SECRET_KEY, Objects.toString(nacosConfigProperties.getSecretKey(), "")); |
| 103 | + properties.put(CONTEXT_PATH, Objects.toString(nacosConfigProperties.getContextPath(), "")); |
| 104 | + properties.put(ENDPOINT, Objects.toString(nacosConfigProperties.getEndpoint(), "")); |
| 105 | + return NacosFactory.createConfigService(properties); |
| 106 | + } |
| 107 | +} |
0 commit comments