In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use RefreshScope to refresh configuration information in SpringCloud. The content of the article is of high quality, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.
We have a configuration class:
@Component("smsTemplateConfig")@ConfigurationProperties(prefix = "zt.sms")@Data//Note the following comment@RefreshScope@Slf4j public class SmsTemplateConfig { /** * Send SMS */ private Boolean send; /** * Number of SMS digits */ private Integer msgCodeDigits; @PostConstruct private void initialize() { log.info("SmsTemplateConfig initialized - send: {},msgCodeDigits: {}", send, msgCodeDigits); }}
This configuration class is used in the following classes:
@Componentpublic class MsgUtils { @Autowired private SmsTemplateConfig config; public SmsTemplateConfig getConfig() { return config; }}
This configuration class is related to SMS configuration. Then, we modify this configuration information in the configuration center, so that the service can hot-load the configuration information through SpringCloud's RefreshScope class. The code is as follows:
RefreshScope.refresh("smsTemplateConfig");
The principle of RefreshScope is that it will destroy the instance named smsTemplateConfig first, and then reinitialize it when smsTemplateConfig is used again.
Then, the problem comes, MsgUtils this class is a single instance, in the container will only be initialized once, the container will not help us inject a new smsTemplateConfig instance, then smsTemplateConfig is destroyed, how to re-initialize it?
solve
Popularize two concepts:
1. The bean lifecycle in Spring can be identified by the Scope class. The lifecycle (subclass of Scope) includes Singleton, Prototype, Request, Session, and SpringCloud has added Thread and Refresh. Singleton and Prototype are separate classes, and the others are subclass of Scope. Beans with different lifecycles are managed by their corresponding Scope.
Spring uses proxy mechanisms for non-Singleton Scodes.
Based on the above two concepts, we SmsTemplateConfig this class above the @RefreshScope annotation, this annotation indicates that the Scope of this class is Refresh, that is, every time we call smsTemplateConfig this instance, in fact, it is called its proxy class.
With this conclusion, we need to see what logic is added to the proxy class. The specific process is too complicated. Here, we directly say the conclusion: Every time we execute the method of the original object through the proxy object, we must obtain the original object. Here, our original object smsTemplateConfig, its life cycle is refresh, so it is managed by Refresh Scope, so we need to obtain its corresponding Refresh Scope first, and then obtain the corresponding instance through Refresh Scope. At this step, The answer is soon to be revealed. Since the corresponding instance is obtained by RefreshScope, then I have to look at the logic obtained by RefreshScope. Here is also a direct conclusion. There is a cache inside the RefreshScope class that caches all instances with a life cycle of refresh. If the cache does not hit, the corresponding instance will be regenerated. In the previous paragraph, we destroyed the instance of smsTemplateConfig. Then it will be regenerated here, that is, the smsTemplateConfig instance will be reinitialized.
How to use RefreshScope to refresh configuration information in SpringCloud is shared here. I hope the above content can help you to learn more. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.