Nacos或者Config是怎么實現(xiàn)配置熱刷新的?
- 前言
- 問題1. 如何實現(xiàn)配置熱刷新
- 1. @RefreshScope原理
- 2. ContextRefresher.refresh()
- 3. RefreshScope.refreshAll()
- 4. 模擬造輪子
- 問題2. Nacos客戶端如何實時監(jiān)聽到Nacos服務(wù)端配置更新了
- 1. Apollo 實現(xiàn)方式
- 2. 什么是DeferredResult
- 3. 模擬造輪子
- 總結(jié)
前言
文中大致介紹實現(xiàn)技術(shù)的關(guān)鍵點(diǎn),以及如何模仿造個簡易輪子(造輪子很重要,只有自己想著造輪子,才會問出很多原理問題),具體源碼細(xì)節(jié),請拿著文中的關(guān)鍵詞自行g(shù)oogle,然后跟著debug即可。
問題1. 如何實現(xiàn)配置熱刷新重點(diǎn) Nacos原理:
- 1.在需要熱刷新的Bean上使用Spring Cloud原生注解 @RefreshScope
- 2.當(dāng)有配置更新的時候調(diào)用contextRefresher.refresh()
代碼如下:
- @RestController
- @RequestMapping("/config")
- @RefreshScope // 重點(diǎn)
- public class ConfigController {
- @Value("${laker.name}") // 待刷新的屬性
- private String lakerName;
- @RequestMapping("/get")
- public String get() {
- return lakerName;
- }
- ...
- }
1. @RefreshScope原理
@RefreshScope位于spring-cloud-context,源碼注釋如下:
可將@Bean定義放入org.springframework.cloud.context.scope.refresh.RefreshScope中。用這種方式注解的Bean可以在運(yùn)行時刷新,并且使用它們的任何組件都將在下一個方法調(diào)用前獲得一個新實例,該實例將完全初始化并注入所有依賴項。
要清楚RefreshScope,先要了解Scope
Scope(org.springframework.beans.factory.config.Scope)是Spring 2.0開始就有的核心的概念
RefreshScope(org.springframework.cloud.context.scope.refresh), 即@Scope("refresh")是spring cloud提供的一種特殊的scope實現(xiàn),用來實現(xiàn)配置、實例熱加載。
類似的有:
- RequestScope:是從當(dāng)前web request中獲取實例的實例
- SessionScope:是從Session中獲取實例的實例
- ThreadScope:是從ThreadLocal中獲取的實例
RefreshScope是從內(nèi)建緩存中獲取的。
2. ContextRefresher.refresh()
當(dāng)有配置更新的時候,觸發(fā)ContextRefresher.refresh
RefreshScope 刷新過程
入口在ContextRefresher.refresh
- public synchronized Set<String> refresh() {
- ① Map<String, Object> before = extract(this.context.getEnvironment().getPropertySources());
- ② updateEnvironment();
- ④ Set<String> keys = changes(before, ③extract(this.context.getEnvironment().getPropertySources())).keySet();
- ⑤ this.context.publishEvent(new EnvironmentChangeEvent(this.context, keys));
- ⑥ this.scope.refreshAll();
- }
①提取標(biāo)準(zhǔn)參數(shù)(SYSTEM,JNDI,SERVLET)之外所有參數(shù)變量
②把原來的Environment里的參數(shù)放到一個新建的Spring Context容器下重新加載,完事之后關(guān)閉新容器(重點(diǎn):可以去debug跟蹤下,實際上是重啟了個SpringApplication)
③提起更新過的參數(shù)(排除標(biāo)準(zhǔn)參數(shù))
④比較出變更項
⑤發(fā)布環(huán)境變更事件
⑥RefreshScope用新的環(huán)境參數(shù)重新生成Bean,重新生成的過程很簡單,清除refreshscope緩存幷銷毀Bean,下次就會重新從BeanFactory獲取一個新的實例(該實例使用新的配置)
3. RefreshScope.refreshAll()
RefreshScope.refreshAll方法實現(xiàn),即上面的第⑥步調(diào)用:
- public void refreshAll() {
- super.destroy();
- this.context.publishEvent(new RefreshScopeRefreshedEvent());
- }
RefreshScope類中有一個成員變量 cache,用于緩存所有已經(jīng)生成的 Bean,在調(diào)用 get 方法時嘗試從緩存加載,如果沒有的話就生成一個新對象放入緩存,并通過 getBean 初始化其對應(yīng)的 Bean:
- public Object get(String name, ObjectFactory<?> objectFactory) {
- BeanLifecycleWrapper value = this.cache.put(name, new BeanLifecycleWrapper(name, objectFactory));
- this.locks.putIfAbsent(name, new ReentrantReadWriteLock());
- try {
- return value.getBean();
- }
- catch (RuntimeException e) {
- this.errors.put(name, e);
- throw e;
- }
- }
所以在銷毀時只需要將整個緩存清空,下次獲取對象時自然就可以重新生成新的對象,也就自然綁定了新的屬性:
- public void destroy() {
- List<Throwable> errors = new ArrayList<Throwable>();
- Collection<BeanLifecycleWrapper> wrappers = this.cache.clear();
- for (BeanLifecycleWrapper wrapper : wrappers) {
- try {
- Lock lock = this.locks.get(wrapper.getName()).writeLock();
- lock.lock();
- try {
- wrapper.destroy();
- }
- finally {
- lock.unlock();
- }
- }
- catch (RuntimeException e) {
- errors.add(e);
- }
- }
- if (!errors.isEmpty()) {
- throw wrapIfNecessary(errors.get(0));
- }
- this.errors.clear();
- }
清空緩存后,下次訪問對象時就會重新創(chuàng)建新的對象并放入緩存了。
而在清空緩存后,它還會發(fā)出一個 RefreshScopeRefreshedEvent 事件,在某些 Spring Cloud 的組件中會監(jiān)聽這個事件并作出一些反饋。
4. 模擬造輪子
這里我們就可以模擬造個熱更新的輪子了;
代碼以及配置如下:
- 項目依賴spring-cloud-context
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-context</artifactId>
- </dependency>
- 配置bean
- @Component
- @RefreshScope
- public class User {
- @Value("${laker.name}")
- private String name;
- ...
- }
- 刷新接口以及查看接口
- @RestController
- @RequestMapping("/config")
- public class ConfigController {
- @Autowired
- User user;
- @Autowired
- ContextRefresher contextRefresher;
- @RequestMapping("/get")
- public String get() {
- return user.getName();
- }
- @RequestMapping("/refresh")
- public String[] refresh() {
- Set<String> keys = contextRefresher.refresh();
- return keys.toArray(new String[keys.size()]);
- }
- application.yml
- laker:
- name: laker
操作流程如下:
1.瀏覽器http://localhost:8080/config/get - 瀏覽器結(jié)果:laker
2.修改application.yml里面內(nèi)容為:
- laker:
- name: lakerupdate
3.瀏覽器http://localhost:8080/config/refresh - 瀏覽器結(jié)果:laker.name
4.瀏覽器http://localhost:8080/config/get - 瀏覽器結(jié)果:lakerupdate(未重新啟動,實現(xiàn)了配置更新)
問題2. Nacos客戶端如何實時監(jiān)聽到Nacos服務(wù)端配置更新了
這里可以去看下Nacos源碼,使用的是長輪詢,什么是長輪詢以及其其他替代協(xié)議?
- RocketMQ
- Nacos
- Apollo
- Kafka
自己花了幾個小時去看Nacos長輪詢源碼,太多了不太好理解,有興趣的自行g(shù)oogle。一般我們都是基于Spring Boot的后臺了,各種google后,發(fā)現(xiàn)Apollo實現(xiàn)較為簡單,所以直接拿Apollo的代碼借鑒。
1. Apollo 實現(xiàn)方式
實現(xiàn)方式如下:
- 客戶端會發(fā)起一個Http請求到Config Service的notifications/v2接口,也就是NotificationControllerV2,參見RemoteConfigLongPollService
- NotificationControllerV2不會立即返回結(jié)果,而是通過Spring DeferredResult把請求掛起
- 如果在60秒內(nèi)沒有該客戶端關(guān)心的配置發(fā)布,那么會返回Http狀態(tài)碼304給客戶端
- 如果有該客戶端關(guān)心的配置發(fā)布,NotificationControllerV2會調(diào)用DeferredResult的setResult方法,傳入有配置變化的namespace信息,同時該請求會立即返回。客戶端從返回的結(jié)果中獲取到配置變化的namespace后,會立即請求Config Service獲取該namespace的最新配置。
解讀下:
- 關(guān)鍵詞DeferredResult,使用這個特性來實現(xiàn)長輪詢
- 超時返回的時候,是返回的狀態(tài)碼Http Code 304
釋義:自從上次請求后,請求的網(wǎng)頁未修改過。服務(wù)器返回此響應(yīng)時,不會返回網(wǎng)頁內(nèi)容,進(jìn)而節(jié)省帶寬和開銷。
2. 什么是DeferredResult
異步支持是在Servlet 3.0中引入的,簡單來說,它允許在請求接收器線程之外的另一個線程中處理HTTP請求。
從Spring 3.2開始可用的DeferredResult有助于將長時間運(yùn)行的計算從http-worker線程卸載到單獨(dú)的線程。
盡管另一個線程將占用一些資源來進(jìn)行計算,但不會阻止工作線程,并且可以處理傳入的客戶端請求。
異步請求處理模型非常有用,因為它有助于在高負(fù)載期間很好地擴(kuò)展應(yīng)用程序,尤其是對于IO密集型操作。
DeferredResult是對異步Servlet的封裝
具體可以參考我在CSDN寫的Spring Boot 使用DeferredResult實現(xiàn)長輪詢
這里借助互聯(lián)網(wǎng)上的一個圖就更清晰些。
Servlet異步流程圖
接收到request請求之后,由tomcat工作線程從HttpServletRequest中獲得一個異步上下文AsyncContext對象,然后由tomcat工作線程把AsyncContext對象傳遞給業(yè)務(wù)處理線程,同時tomcat工作線程歸還到工作線程池,這一步就是異步開始。在業(yè)務(wù)處理線程中完成業(yè)務(wù)邏輯的處理,生成response返回給客戶端。
3. 模擬造輪子
這里我們通過使用 Spring Boot 來簡單的模擬一下如何通過 Spring Boot DeferredResult 來實現(xiàn)長輪詢服務(wù)推送的。
代碼如下,僅供參考:
- /**
- * 模擬Config Service通知客戶端的長輪詢實現(xiàn)原理
- */
- @RestController
- @RequestMapping("/config")
- public class LakerConfigController {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
- //guava中的Multimap,多值map,對map的增強(qiáng),一個key可以保持多個value
- private Multimap<String, DeferredResult<String>> watchRequests = Multimaps.synchronizedSetMultimap(HashMultimap.create());
- /**
- * 模擬長輪詢
- */
- @RequestMapping(value = "/get/{dataId}")
- public DeferredResult<String> watch(@PathVariable("dataId") String dataId) {
- logger.info("Request received");
- ResponseEntity<String>
- NOT_MODIFIED_RESPONSE = new ResponseEntity<>(HttpStatus.NOT_MODIFIED);
- // 超時時間30s 返回 304 狀態(tài)碼告訴客戶端當(dāng)前命名空間的配置文件并沒有更新
- DeferredResult<String> deferredResult = new DeferredResult<>(30 * 1000L, NOT_MODIFIED_RESPONSE);
- //當(dāng)deferredResult完成時(不論是超時還是異常還是正常完成),移除watchRequests中相應(yīng)的watch key
- deferredResult.onCompletion(() -> {
- logger.info("remove key:" + dataId);
- watchRequests.remove(dataId, deferredResult);
- });
- deferredResult.onTimeout(() -> {
- logger.info("onTimeout()");
- });
- watchRequests.put(dataId, deferredResult);
- logger.info("Servlet thread released");
- return deferredResult;
- }
- /**
- * 模擬發(fā)布配置
- */
- @RequestMapping(value = "/update/{dataId}")
- public Object publishConfig(@PathVariable("dataId") String dataId) {
- if (watchRequests.containsKey(dataId)) {
- Collection<DeferredResult<String>> deferredResults = watchRequests.get(dataId);
- Long time = System.currentTimeMillis();
- //通知所有watch這個namespace變更的長輪訓(xùn)配置變更結(jié)果
- for (DeferredResult<String> deferredResult : deferredResults) {
- //deferredResult一旦執(zhí)行了setResult()方法,就說明DeferredResult正常完成了,會立即把結(jié)果返回給客戶端
- deferredResult.setResult(dataId + " changed:" + time);
- }
- }
- return "success";
- }
- }
操作流程如下:
為了簡便我用瀏覽器模擬,實際用Java Http Client,例如:okhttp、Apache http client等
正常流程:
- client1瀏覽器http://localhost:8080/config/get/laker,阻塞中ing
- client2瀏覽器http://localhost:8080/config/update/laker,返回success
- client1瀏覽器http://localhost:8080/config/get/laker,返回laker changed:1611022736865
超時流程:
- client1瀏覽器http://localhost:8080/config/get/laker,阻塞中ing
- 30s后
- client1瀏覽器,返回http code 304
在這里插入圖片描述
總結(jié)
- Nacos使用長輪詢解決了實時監(jiān)聽遠(yuǎn)端配置變更
- Nacos使用spring-cloud-context的@RefreshScope和ContextRefresher.refresh實現(xiàn)了配置熱刷新
參考:
- https://ctripcorp.github.io/apollo/#/zh/README
- https://blog.csdn.net/liuccc1/article/details/87002916
- https://blog.csdn.net/wangxindong11/article/details/78591396
- https://blog.csdn.net/u012410733/article/details/107119457
- https://www.cnblogs.com/javastack/p/12049139.html