作者:畢來生
前言
個人比較看好Spring Cloud Alibaba家族。此系列以Nacos為主題,從Spring、Spring boot、Spring Cloud多個方面逐漸進行示範,源碼解讀。目前來看官方文檔還有待完善。網絡上除了官網外缺少Nacos系列文章。都是零零散散的知識點。如此系列文章哪裡寫的有不周全,錯誤之處。歡迎大家指正。謝謝。
因公衆号排版問題,可能會有代碼顯示不完整,請使用電腦版微信内置浏覽器/複制連結到浏覽器中。
第一篇 :
【Nacos系列第一篇】-Nacos之Spring Discovery 以及Config。
第二篇 :
【Nacos系列第二篇】-Nacos之Spring Boot Discovery。 因大家在工作中逐漸以Spring boot、Spring Cloud為主進行開發。我們接下來會以這兩個為核心示範詳解。
Nacos架構圖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSP9E1TzUFVPVzYE90MnpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZwpmL4kjM5UDM1gTM1ETMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
工程結構
上面說了那麼多,現在先來看一下我們的Spring boot Nacos config工程結構(Windows下示範)
Spring Boot版本:2.1.2.RELEASE
準備工作
1、啟動Nacos(Demo示範環境為Windows下)
2、通路
http://127.0.0.1:8848/nacos/index.html#/configurationManagement?dataId=&group=&appName=&namespace=
進入Nacos配置管理頁面
3、點選左側配置管理->配置清單(右側有一個加号。添加對應資訊),如下圖
以上就是我們要做的準備工作啦。
擷取ConfigService方法
1、通過@NacosInjected注解擷取
@NacosInjected
ConfigService configService;
2、通過NacosFactory傳入Properties擷取
ConfigService configService = NacosFactory.createConfigService(properties);
3、通過NacosFactory傳入ServerAddr擷取
ConfigService configService = NacosFactory.createConfigService(serverAddr);
以上方式均是為反射擷取,NacosFactory建立ConfigService實際上也是調用
ConfigFactory.createConfigService
實作的
/**
* Create config
*
* @param properties
* init param
* @return config
* @throws NacosException
* Exception
*/
public static ConfigService createConfigService(Properties properties) throws NacosException {
return ConfigFactory.createConfigService(properties);
}
附上建立ConfigService源碼
/**
* Create Config
*
* @param ServerAddr
* serverlist
* @return Config
* @throws NacosException
* Exception
*/
public static ConfigService createConfigService(String serverAddr) throws NacosException {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
try {
Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.config.NacosConfigService");
Constructor constructor = driverImplClass.getConstructor(Properties.class);
ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties);
return vendorImpl;
} catch (Throwable e) {
throw new NacosException(-400, e.getMessage());
}
}
代碼示範
附上與普通建立的Spring boot工程不同點(以下示範為通過配置管理代碼示例中相同擷取方式)
ConfigController(新增)
package org.nacos.springboot.controller;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* @author bilaisheng
* @wechat: 878799579
* @date 2019/01/15 19:34
* @description Test Nacos Config
*/
@Controller
@RequestMapping("config")
public class ConfigController {
/**
* @author : bilaisheng
* @wechat: 878799579
* @date : 2019/01/15 19:45
* @return Map
* @throws NacosException
* @throws InterruptedException
*/
@ResponseBody
@RequestMapping("/get")
public Map getConfig() throws NacosException, InterruptedException {
// 用以示範用,頁面傳回資料展示
Map map = new HashMap();
// 服務位址。本機示範故寫localhost。請根據實際情況替換對應IP
String serverAddr = "localhost";
String dataId = "nacos-spring";
String group = "bilaisheng";
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
// 建立ConfigService,此處通過Properties方式進行建立,另一種示範serviceaddr擷取configService.
// 原理上都是通過 ConfigFactory.createConfigService()去進行建立
ConfigService configService = NacosFactory.createConfigService(properties);
// ConfigService configService = NacosFactory.createConfigService(serverAddr);
String content = configService.getConfig(dataId, group, 5000);
System.out.println("config : " + content);
map.put("content", content);
// 添加Listener,用以示範receive擷取資料結果
configService.addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println("recieve : " + configInfo);
}
@Override
public Executor getExecutor() {
return null;
}
});
// 推送config。将原有dataid中資訊替換。
boolean isPublishOk = configService.publishConfig(dataId, group, "publish config content");
System.out.println("isPublishOk : " + isPublishOk);
map.put("isPublishOk", isPublishOk);
Thread.sleep(3000);
content = configService.getConfig(dataId, group, 5000);
System.out.println("Thread sleep 3000ms : " + content);
map.put("Thread sleep 3000ms : ", content);
// 删除指定dataid , group 配置
boolean isRemoveOk = configService.removeConfig(dataId, group);
System.out.println("remove " + dataId + "config is " + isRemoveOk);
Thread.sleep(3000);
content = configService.getConfig(dataId, group, 5000);
System.out.println("content after 5000ms "+content);
Thread.sleep(3000);
return map;
}
}
application.properties
nacos.config.server-addr=127.0.0.1:8848
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
上述内容就是我們在建立好Spring Boot工程結構後增加/調整内容。
示範步驟
1、啟動
BilaishengNacosSpringbootConfigApplication
2、調用
http://localhost:8080/config/get
,此時控制台出現
頁面出現
以上就是我們Spring Boot Config的一個Demo例子