本篇內(nèi)容主要講解“nacos config中publishConfig的原理及應(yīng)用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“nacos config中publishConfig的原理及應(yīng)用”吧!
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、玉泉網(wǎng)絡(luò)推廣、重慶小程序開(kāi)發(fā)、玉泉網(wǎng)絡(luò)營(yíng)銷、玉泉企業(yè)策劃、玉泉品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供玉泉建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
本文主要研究一下nacos config的publishConfig
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java
@Controller @RequestMapping(Constants.CONFIG_CONTROLLER_PATH) public class ConfigController { private static final Logger log = LoggerFactory.getLogger(ConfigController.class); private static final String NAMESPACE_PUBLIC_KEY = "public"; public static final String EXPORT_CONFIG_FILE_NAME = "nacos_config_export_"; public static final String EXPORT_CONFIG_FILE_NAME_EXT = ".zip"; public static final String EXPORT_CONFIG_FILE_NAME_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; private final transient ConfigServletInner inner; private final transient PersistService persistService; private final transient ConfigSubService configSubService; @Autowired public ConfigController(ConfigServletInner configServletInner, PersistService persistService, ConfigSubService configSubService) { this.inner = configServletInner; this.persistService = persistService; this.configSubService = configSubService; } /** * 增加或更新非聚合數(shù)據(jù)。 * * @throws NacosException */ @RequestMapping(method = RequestMethod.POST) @ResponseBody public Boolean publishConfig(HttpServletRequest request, HttpServletResponse response, @RequestParam("dataId") String dataId, @RequestParam("group") String group, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant, @RequestParam("content") String content, @RequestParam(value = "tag", required = false) String tag, @RequestParam(value = "appName", required = false) String appName, @RequestParam(value = "src_user", required = false) String srcUser, @RequestParam(value = "config_tags", required = false) String configTags, @RequestParam(value = "desc", required = false) String desc, @RequestParam(value = "use", required = false) String use, @RequestParam(value = "effect", required = false) String effect, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "schema", required = false) String schema) throws NacosException { final String srcIp = RequestUtil.getRemoteIp(request); String requestIpApp = RequestUtil.getAppName(request); ParamUtils.checkParam(dataId, group, "datumId", content); ParamUtils.checkParam(tag); MapconfigAdvanceInfo = new HashMap (10); if (configTags != null) { configAdvanceInfo.put("config_tags", configTags); } if (desc != null) { configAdvanceInfo.put("desc", desc); } if (use != null) { configAdvanceInfo.put("use", use); } if (effect != null) { configAdvanceInfo.put("effect", effect); } if (type != null) { configAdvanceInfo.put("type", type); } if (schema != null) { configAdvanceInfo.put("schema", schema); } ParamUtils.checkParam(configAdvanceInfo); if (AggrWhitelist.isAggrDataId(dataId)) { log.warn("[aggr-conflict] {} attemp to publish single data, {}, {}", RequestUtil.getRemoteIp(request), dataId, group); throw new NacosException(NacosException.NO_RIGHT, "dataId:" + dataId + " is aggr"); } final Timestamp time = TimeUtils.getCurrentTime(); String betaIps = request.getHeader("betaIps"); ConfigInfo configInfo = new ConfigInfo(dataId, group, tenant, appName, content); if (StringUtils.isBlank(betaIps)) { if (StringUtils.isBlank(tag)) { persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, false); EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, dataId, group, tenant, time.getTime())); } else { persistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false); EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime())); } } else { // beta publish persistService.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false); EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime())); } ConfigTraceService.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), LOCAL_IP, ConfigTraceService.PERSISTENCE_EVENT_PUB, content); return true; } //...... }
publishConfig根據(jù)入?yún)?gòu)造configAdvanceInfo及configInfo,對(duì)于前者會(huì)執(zhí)行ParamUtils.checkParam(configAdvanceInfo)校驗(yàn)
對(duì)于有betaIps的則執(zhí)行persistService.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false),然后發(fā)布ConfigDataChangeEvent
對(duì)于沒(méi)有betaIps的則判斷tag是否為空,為空則執(zhí)行persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, false),不為空則執(zhí)行persistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);二者都會(huì)發(fā)布ConfigDataChangeEvent
nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java
@Repository public class PersistService { @Autowired private DynamicDataSource dynamicDataSource; private DataSourceService dataSourceService; //...... /** * 寫(xiě)入主表,插入或更新 */ public void insertOrUpdate(String srcIp, String srcUser, ConfigInfo configInfo, Timestamp time, MapconfigAdvanceInfo, boolean notify) { try { addConfigInfo(srcIp, srcUser, configInfo, time, configAdvanceInfo, notify); } catch (DataIntegrityViolationException ive) { // 唯一性約束沖突 updateConfigInfo(configInfo, srcIp, srcUser, time, configAdvanceInfo, notify); } } public void insertOrUpdateTag(final ConfigInfo configInfo, final String tag, final String srcIp, final String srcUser, final Timestamp time, final boolean notify) { try { addConfigInfo4Tag(configInfo, tag, srcIp, null, time, notify); } catch (DataIntegrityViolationException ive) { // 唯一性約束沖突 updateConfigInfo4Tag(configInfo, tag, srcIp, null, time, notify); } } public void insertOrUpdateBeta(final ConfigInfo configInfo, final String betaIps, final String srcIp, final String srcUser, final Timestamp time, final boolean notify) { try { addConfigInfo4Beta(configInfo, betaIps, srcIp, null, time, notify); } catch (DataIntegrityViolationException ive) { // 唯一性約束沖突 updateConfigInfo4Beta(configInfo, srcIp, null, time, notify); } } //...... /** * 添加普通配置信息,發(fā)布數(shù)據(jù)變更事件 */ public void addConfigInfo(final String srcIp, final String srcUser, final ConfigInfo configInfo, final Timestamp time, final Map configAdvanceInfo, final boolean notify) { tjt.execute(new TransactionCallback () { @Override public Boolean doInTransaction(TransactionStatus status) { try { long configId = addConfigInfoAtomic(srcIp, srcUser, configInfo, time, configAdvanceInfo); String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags"); addConfiTagsRelationAtomic(configId, configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant()); insertConfigHistoryAtomic(0, configInfo, srcIp, srcUser, time, "I"); if (notify) { EventDispatcher.fireEvent( new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime())); } } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } return Boolean.TRUE; } }); } /** * 更新配置信息 */ public void updateConfigInfo(final ConfigInfo configInfo, final String srcIp, final String srcUser, final Timestamp time, final Map configAdvanceInfo, final boolean notify) { tjt.execute(new TransactionCallback () { @Override public Boolean doInTransaction(TransactionStatus status) { try { ConfigInfo oldConfigInfo = findConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant()); String appNameTmp = oldConfigInfo.getAppName(); // 用戶傳過(guò)來(lái)的appName不為空,則用持久化用戶的appName,否則用db的;清空appName的時(shí)候需要傳空串 if (configInfo.getAppName() == null) { configInfo.setAppName(appNameTmp); } updateConfigInfoAtomic(configInfo, srcIp, srcUser, time, configAdvanceInfo); String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags"); if (configTags != null) { // 刪除所有tag,然后再重新創(chuàng)建 removeTagByIdAtomic(oldConfigInfo.getId()); addConfiTagsRelationAtomic(oldConfigInfo.getId(), configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant()); } insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, time, "U"); if (notify) { EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime())); } } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } return Boolean.TRUE; } }); } /** * 添加普通配置信息,發(fā)布數(shù)據(jù)變更事件 */ public void addConfigInfo4Tag(ConfigInfo configInfo, String tag, String srcIp, String srcUser, Timestamp time, boolean notify) { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); try { String md5 = MD5.getInstance().getMD5String(configInfo.getContent()); jt.update( "INSERT INTO config_info_tag(data_id,group_id,tenant_id,tag_id,app_name,content,md5,src_ip,src_user," + "gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)", configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp, appNameTmp, configInfo.getContent(), md5, srcIp, srcUser, time, time); if (notify) { EventDispatcher.fireEvent(new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp, time.getTime())); } } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } } /** * 更新配置信息 */ public void updateConfigInfo4Tag(ConfigInfo configInfo, String tag, String srcIp, String srcUser, Timestamp time, boolean notify) { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); try { String md5 = MD5.getInstance().getMD5String(configInfo.getContent()); jt.update( "UPDATE config_info_tag SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE " + "data_id=? AND group_id=? AND tenant_id=? AND tag_id=?", configInfo.getContent(), md5, srcIp, srcUser, time, appNameTmp, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); if (notify) { EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp, time.getTime())); } } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } } /** * 添加普通配置信息,發(fā)布數(shù)據(jù)變更事件 */ public void addConfigInfo4Beta(ConfigInfo configInfo, String betaIps, String srcIp, String srcUser, Timestamp time, boolean notify) { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); try { String md5 = MD5.getInstance().getMD5String(configInfo.getContent()); jt.update( "INSERT INTO config_info_beta(data_id,group_id,tenant_id,app_name,content,md5,beta_ips,src_ip," + "src_user,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)", configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, configInfo.getContent(), md5, betaIps, srcIp, srcUser, time, time); if (notify) { EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, time.getTime())); } } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } } /** * 更新配置信息 */ public void updateConfigInfo4Beta(ConfigInfo configInfo, String srcIp, String srcUser, Timestamp time, boolean notify) { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); try { String md5 = MD5.getInstance().getMD5String(configInfo.getContent()); jt.update( "UPDATE config_info_beta SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE " + "data_id=? AND group_id=? AND tenant_id=?", configInfo.getContent(), md5, srcIp, srcUser, time, appNameTmp, configInfo.getDataId(), configInfo.getGroup(), tenantTmp); if (notify) { EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, time.getTime())); } } catch (CannotGetJdbcConnectionException e) { fatalLog.error("[db-error] " + e.toString(), e); throw e; } } //...... }
insertOrUpdate、insertOrUpdateTag、insertOrUpdateBeta三者的執(zhí)行邏輯都是先執(zhí)行insert操作,捕獲到DataIntegrityViolationException時(shí)執(zhí)行update操作
publishConfig根據(jù)入?yún)?gòu)造configAdvanceInfo及configInfo,對(duì)于前者會(huì)執(zhí)行ParamUtils.checkParam(configAdvanceInfo)校驗(yàn)
對(duì)于有betaIps的則執(zhí)行persistService.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false),然后發(fā)布ConfigDataChangeEvent
對(duì)于沒(méi)有betaIps的則判斷tag是否為空,為空則執(zhí)行persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, false),不為空則執(zhí)行persistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);二者都會(huì)發(fā)布ConfigDataChangeEvent
到此,相信大家對(duì)“nacos config中publishConfig的原理及應(yīng)用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!