Apollo2.1.0+Springboot使用OpenApI 全球观天下
(资料图)
依赖管理
org.springframework.cloud spring-cloud-starter-bootstrap 3.1.3 com.ctrip.framework.apollo apollo-client 2.1.0 com.ctrip.framework.apollo apollo-openapi 2.1.0
application.properties
# Apolloapp.id=MORE-USER-USEapollo.cache-dir=./ManyUser/apollo/cacheapollo.bootstrap.enabled=true#apollo.bootstrap.namespaces=application.propertiesapollo.meta=http://localhost:8080apollo.bootstrap.eagerLoad.enabled=trueapollo.config-service=http://localhost:8080env=DEVapollo.use.admin.token=809d168da6f3baa39c02c1d376a0cc9cec852fa0fa8c6e2cff9b33e276d3738aapollo.use.portal.url=http://localhost:8070
apollo-env.properties
dev.meta=http://localhost:8080
启动类 加入 @EnableApolloConfig
/** * @description: 启动类 * @author: GuoTong * @createTime: 2023-06-21 20:31 * @since JDK 1.8 OR 11 **/@SpringBootApplication@EnableApolloConfigpublic class AppStartClass { public static void main(String[] args) { SpringApplication.run(AppStartClass.class, args); }}
常规配置中心集成使用
package com.gton.user.config;import com.ctrip.framework.apollo.model.ConfigChangeEvent;import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue;import com.gton.user.io.Resp;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.util.ArrayList;import java.util.List;/** * @description: Apollo同时还增加了几个新的Annotation来简化在Spring环境中的使用。 * @author: GuoTong * @createTime: 2023-06-24 12:30 * @since JDK 1.8 OR 11 **/@Component@Slf4jpublic class MyApolloConfig { /** * ApolloJsonValue annotated on fields example, the default value is specified as empty list - [] *
* jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}] */ @ApolloJsonValue("${jsonBeanProperty:[]}") private List anotherJsonBeans = new ArrayList<>(); @Value("${batch:100}") private String batch; //config change listener for namespace application @ApolloConfigChangeListener(value = "application") private void apolloChange(ConfigChangeEvent changeEvent) { //update injected value of batch if it is changed in Apollo if (changeEvent.isChanged("batch")) { batch = changeEvent.getChange("batch").getNewValue(); } log.info(changeEvent.getNamespace() + "变化的key=>" + changeEvent.changedKeys() + ";==1"); } @ApolloConfigChangeListener(value = "order") private void someOnChange(ConfigChangeEvent changeEvent) { //update injected value of batch if it is changed in Apollo if (changeEvent.isChanged("anotherJsonBeans")) { log.info(changeEvent.getChange("anotherJsonBeans").getOldValue()); log.error(changeEvent.getChange("anotherJsonBeans").getNewValue()); } log.info(anotherJsonBeans.toString()); log.info(changeEvent.getNamespace() + "变化的key=>" + changeEvent.changedKeys() + ";==1"); }}
Apollo 的开放平台使用
package com.gton.user.handler;import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient;import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO;import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;import com.ctrip.framework.apollo.openapi.dto.OpenPageDTO;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.apache.commons.lang3.StringUtils;import org.springframework.core.io.ClassPathResource;import java.io.IOException;import java.io.InputStream;import java.util.List;import java.util.Properties;/** * @description: Apollo开放平台操作 * @author: GuoTong * @createTime: 2023-06-24 14:10 * @since JDK 1.8 OR 11 **/public class ApolloOpenApiOperator { private ApolloOpenApiClient apolloOpenApiClient; private static ApolloOpenApiOperator apolloOpenApiOperator = null; public static ApolloOpenApiOperator getApolloOpenApiOperator() throws IOException { if (apolloOpenApiOperator == null) { synchronized (ApolloOpenApiOperator.class) { if (apolloOpenApiOperator == null) { return new ApolloOpenApiOperator(); } } } return apolloOpenApiOperator; } public ApolloOpenApiOperator(ApolloOpenApiClient apolloOpenApiClient) { this.apolloOpenApiClient = apolloOpenApiClient; } public ApolloOpenApiOperator() throws IOException { initApolloOpenApiOperator(); } private void initApolloOpenApiOperator() throws IOException { // 获取配置文件 ClassPathResource classPathResource = new ClassPathResource("application.properties"); InputStream inputStream = classPathResource.getInputStream(); // 使用properties加载 Properties properties = new Properties(); properties.load(inputStream); String token = properties.getProperty("apollo.use.admin.token"); String portalUrl = properties.getProperty("apollo.use.portal.url"); if (StringUtils.isNotEmpty(token)) { this.apolloOpenApiClient = ApolloOpenApiClient .newBuilder(). withConnectTimeout(60000). withToken(token). withPortalUrl(portalUrl). withReadTimeout(60000) .build(); } } public ApolloOpenApiClient getApolloOpenApiClient() { return this.apolloOpenApiClient; } /** * Description: 创建NamSpace * * @param openAppNamespaceKey * @author: GuoTong * @date: 2023-06-24 15:04:44 * @return:void */ public void crateNameSpace(OpenAppNamespaceKey openAppNamespaceKey) { OpenAppNamespaceDTO openAppNamespaceDTO = new OpenAppNamespaceDTO(); openAppNamespaceDTO.setAppId(openAppNamespaceKey.getAppId()); openAppNamespaceDTO.setName(openAppNamespaceKey.getSpaceName()); openAppNamespaceDTO.setComment(openAppNamespaceKey.getSpaceDesc()); openAppNamespaceDTO.setPublic(openAppNamespaceKey.spaceIsPub); apolloOpenApiClient.createAppNamespace(openAppNamespaceDTO); } /** * Description: 下发Apollo * * @param doIssueApolloKey * @author: GuoTong * @date: 2023-06-24 14:35:52 * @return:void */ public void doIssueApollo(DoIssueApolloKey doIssueApolloKey) { OpenItemDTO openItemDTO = new OpenItemDTO(); openItemDTO.setKey(doIssueApolloKey.getKey()); openItemDTO.setValue(doIssueApolloKey.getValue()); openItemDTO.setComment(doIssueApolloKey.getDesc()); openItemDTO.setType(doIssueApolloKey.handlerValueType(doIssueApolloKey.getValueType())); openItemDTO.setDataChangeCreatedBy(doIssueApolloKey.getOperator()); if (doIssueApolloKey.isModifiedOrInsert()) { openItemDTO.setDataChangeLastModifiedBy(doIssueApolloKey.getOperator()); } // 下发到Apollo apolloOpenApiClient.createOrUpdateItem( doIssueApolloKey.getAppId(), doIssueApolloKey.getEnv(), doIssueApolloKey.getClusterName(), doIssueApolloKey.getNamespaceName(), openItemDTO ); } /** * Description: 发布整个NameSpace;推荐下发完成之后统一调一次,不要多次的下发操作,每一次下发都调用 * * @param publishApolloKey * @author: GuoTong * @date: 2023-06-24 14:56:43 * @return:void */ public void executorPublish(PublishApolloKey publishApolloKey) { // apollo下发的数据执行发布 NamespaceReleaseDTO namespaceReleaseDTO = new NamespaceReleaseDTO(); //配置版本名称 namespaceReleaseDTO.setReleaseTitle(publishApolloKey.getReleaseTitle()); // 发布说明 namespaceReleaseDTO.setReleaseComment(publishApolloKey.getReleaseDescription()); // 操作员 namespaceReleaseDTO.setReleasedBy(publishApolloKey.getReleaseOperator()); apolloOpenApiClient.publishNamespace(publishApolloKey.getAppId(), publishApolloKey.getEnv(), publishApolloKey.getClusterName(), publishApolloKey.getNamespaceName(), namespaceReleaseDTO); } /** * Description: 移除Apollo的配置 * String appId, String env, String clusterName, String namespaceName, String key, * String operator * * @param removeKeys * @author: GuoTong * @date: 2023-06-24 15:27:07 * @return:void */ public void executorRemove(DoIssueApolloKey removeKeys) { apolloOpenApiClient.removeItem(removeKeys.getAppId(), removeKeys.getEnv(), removeKeys.getClusterName(), removeKeys.getNamespaceName(), removeKeys.getKey(), removeKeys.getOperator()); } /** * queryApolloKey many * * @param baseApolloKey * @author: GuoTong * @date: 2023-06-24 14:56:43 * @return:void */ public List queryApolloKey(BaseApolloKey baseApolloKey, int page, int size) { if (page <= 1) { page = 0; } if (size <= 0) { size = 10; } OpenPageDTO itemsByNamespace = apolloOpenApiClient.findItemsByNamespace(baseApolloKey.getAppId(), baseApolloKey.getEnv(), baseApolloKey.getClusterName(), baseApolloKey.getNamespaceName(), page, size); List content = itemsByNamespace.getContent(); return content; } /** * queryApolloKey one * * @param baseApolloKey * @author: GuoTong * @date: 2023-06-24 14:56:43 * @return:void */ public OpenItemDTO queryApolloKeyOne(DoIssueApolloKey baseApolloKey) { OpenItemDTO itemsByNamespace = apolloOpenApiClient.getItem(baseApolloKey.getAppId(), baseApolloKey.getEnv(), baseApolloKey.getClusterName(), baseApolloKey.getNamespaceName(), baseApolloKey.getKey()); return itemsByNamespace; } @Data public static class OpenAppNamespaceKey { private String appId; private String spaceName; private String spaceDesc; private boolean spaceIsPub = false; // 默认值 DEV private String env = "DEV"; private String clusterName; private String namespaceName; } @Data @NoArgsConstructor @AllArgsConstructor public static class DoIssueApolloKey extends BaseApolloKey { private String key; private String value; // 默认值 Apollo private String operator = "Apollo"; private String desc; private String valueType; // 是否是修改 private boolean modifiedOrInsert; /** * Description: 查询 Portal web的前端得出 * * @author: GuoTong * @date: 2023-06-24 14:46:42 * */ public int handlerValueType(String valueType) { switch (valueType) { case "String": return 0; case "Number": return 1; case "Boolean": return 2; case "JSON": return 3; } // 未匹配就默认使用String的值 return 0; } } @Data @NoArgsConstructor @AllArgsConstructor public static class BaseApolloKey { protected String appId; // 默认值 DEV protected String env = "DEV"; // 默认值 default protected String clusterName = "default"; protected String namespaceName; } @Data public static class PublishApolloKey extends BaseApolloKey { // 发布时候的标题 private String releaseTitle; // 发布时候的操作员 private String releaseOperator; // 发布时候的DESC private String releaseDescription; }}
配合Controller使用
package com.gton.user.controller;import com.alibaba.fastjson2.JSON;import com.alibaba.fastjson2.JSONObject;import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;import com.gton.user.annotation.SwaggerScanClass;import com.gton.user.handler.ApolloOpenApiOperator;import com.gton.user.io.Resp;import io.swagger.annotations.ApiOperation;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.PutMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.io.IOException;import java.util.List;import java.util.Map;/** * 多租户表(apollo)表控制层 * * @author 郭童 * @since 2023-06-22 16:28:10 */@RestController@RequestMapping("apollo")@SwaggerScanClasspublic class ApolloOperatorController { /** * 分页查询数据 namespaceName * * @param * @return 所有数据 */ @PostMapping("/queryLimit") @ApiOperation("查询Apollo配置") public Resp> queryPage( @RequestParam("appId") String appId, @RequestParam("clusterName") String clusterName, @RequestParam("env") String env, @RequestParam("namespaceName") String namespaceName ) throws IOException { ApolloOpenApiOperator apolloOpenApiOperator = ApolloOpenApiOperator.getApolloOpenApiOperator(); ApolloOpenApiOperator.BaseApolloKey baseApolloKey = new ApolloOpenApiOperator.BaseApolloKey(); baseApolloKey.setAppId(appId); baseApolloKey.setClusterName(clusterName); baseApolloKey.setEnv(env); baseApolloKey.setNamespaceName(namespaceName); List openItemDTOS = apolloOpenApiOperator.queryApolloKey(baseApolloKey, 0, 10); return Resp.Ok(openItemDTOS); } /** * 通过主键查询单条数据 * * @param key apollo-key * @return 单条数据 */ @GetMapping("/queryOne/{key}") public Resp selectOne(@PathVariable("key") String key, @RequestParam("appId") String appId, @RequestParam("clusterName") String clusterName, @RequestParam("env") String env, @RequestParam("namespaceName") String namespaceName) throws IOException { ApolloOpenApiOperator apolloOpenApiOperator = ApolloOpenApiOperator.getApolloOpenApiOperator(); ApolloOpenApiOperator.DoIssueApolloKey baseApolloKey = new ApolloOpenApiOperator.DoIssueApolloKey(); baseApolloKey.setAppId(appId); baseApolloKey.setEnv(env); baseApolloKey.setNamespaceName(namespaceName); baseApolloKey.setClusterName(clusterName); baseApolloKey.setKey(key); OpenItemDTO openItemDTO = apolloOpenApiOperator.queryApolloKeyOne(baseApolloKey); return Resp.Ok(openItemDTO); } /** * 新增数据 到Apollo * * @param doIssueApolloKey 实体对象 * @return 新增结果 */ @PostMapping("/save") public Resp insert(@RequestBody Map doIssueApolloKey) throws IOException { String json = JSON.toJSONString(doIssueApolloKey); ApolloOpenApiOperator.DoIssueApolloKey doIssueApolloData = JSONObject.parseObject(json, ApolloOpenApiOperator.DoIssueApolloKey.class); ApolloOpenApiOperator apolloOpenApiOperator = ApolloOpenApiOperator.getApolloOpenApiOperator(); doIssueApolloData.setModifiedOrInsert(false); apolloOpenApiOperator.doIssueApollo(doIssueApolloData); return Resp.Ok(doIssueApolloData.getKey()); } /** * 修改数据 * * @param doIssueApolloKey 实体对象 * @return 修改结果 */ @PutMapping("/update") public Resp update(@RequestBody Map doIssueApolloKey) throws IOException { String json = JSON.toJSONString(doIssueApolloKey); ApolloOpenApiOperator.DoIssueApolloKey doIssueApolloData = JSONObject.parseObject(json, ApolloOpenApiOperator.DoIssueApolloKey.class); ApolloOpenApiOperator apolloOpenApiOperator = ApolloOpenApiOperator.getApolloOpenApiOperator(); doIssueApolloData.setModifiedOrInsert(true); apolloOpenApiOperator.doIssueApollo(doIssueApolloData); return Resp.Ok(doIssueApolloData.getKey()); } /** * Description: 移出Apollo已下发数据 * * @param key * @param appId * @param clusterName * @param env * @param namespaceName * @author: GuoTong * @date: 2023-06-24 15:42:24 * @return:com.gton.user.io.Resp */ @GetMapping("/dels") public Resp delete(@RequestParam("key") String key, @RequestParam("appId") String appId, @RequestParam("clusterName") String clusterName, @RequestParam("env") String env, @RequestParam("namespaceName") String namespaceName) throws IOException { ApolloOpenApiOperator apolloOpenApiOperator = ApolloOpenApiOperator.getApolloOpenApiOperator(); ApolloOpenApiOperator.DoIssueApolloKey removeKeys = new ApolloOpenApiOperator.DoIssueApolloKey(); removeKeys.setAppId(appId); removeKeys.setEnv(env); removeKeys.setNamespaceName(namespaceName); removeKeys.setClusterName(clusterName); removeKeys.setKey(key); apolloOpenApiOperator.executorRemove(removeKeys); return Resp.Ok(removeKeys.getKey()); }}
-
Apollo2.1.0+Springboot使用OpenApI 全球观天下依赖管理```xmlorg springframework cloudspring-cloud-starter-boot
-
【全球新要闻】汪小菲与大S打官司又输了!为什么大S总能拿捏汪小菲-当前通讯6月23日,大S跟汪小菲有关抚养费一事的那场官司判决书曝光判决书显示,
-
海南考生看过来!乌海中考时间定了!-世界今头条2023年乌海市初中学业水平文化课考试将于6月26日(星期一)至28日(星期
-
古诗、古籍、古画……古人的端午习俗在这些细节中留存央视新闻客户端岁岁端阳,今又端阳,盛夏粽艾分外香。每逢端午,我们吃
-
环球即时:新荣威i5官图发布!增540度全景影像 7月上市6月21日,荣威官方发布了全新荣威i5官图,新车将于7月上市。新荣威i5采
-
【世界独家】烟台市莱山区第二实验幼儿园开展端午节主题教育活动生活是教育的源泉,节日是教育的契机。为弘扬中华民族传统文化,让孩子
-
每日信息:隔离霜好还是隔离乳好_隔离乳和隔离霜的区别1、质地不一样。2、乳一般指乳液,乳比霜要稀一点清爽一点。3、看你的
-
意媒:尤文准备1200万欧报价帕里西,苏莱可能加入交易据全尤文报道,尤文图斯准备1200万欧元报价帕里西,苏莱可能加入交易。
-
莫西–格鲁西人(关于莫西–格鲁西人的简介)_当前信息1、莫西–格鲁西人,西非民族集团之一。本文关于莫西–格鲁西人的简介
-
江南社会历史评论 第4期1、《江南社会历史评论(第4期)》是2012年12月商务印书馆出版的图书。
-
世界快播:赶集网找工作兼职(赶集网兼职)您好,现在汉格来为大家解答以上的问题。赶集网找工作兼职,赶集网兼职
-
现挤羊奶能喝吗(喝现挤羊奶得病)1、喝现挤牛奶得病可能是少部分的情况,现挤牛奶的卫生问题,如果是正
-
李强会见法国总统马克龙 最新当地时间6月22日下午,国务院总理李强在巴黎爱丽舍宫会见法国总统马克
-
“超人”亨利卡维尔有望成为007新片邦德扮演者:试镜效果棒极了 环球热门经典特工片007系列正在筛选下一任詹姆斯邦德人选,导演马丁坎贝尔日前
-
上海中心气象台发布暴雨蓝色预警及雷电黄色预警上海中心气象台2023年06月23日21时00分发布雷电黄色预警[Ⅲ级 较重]。
-
tuple 和数组区别元组(tuple)和数组(array)都是Python中用于存储多个值的数据结构,
-
出行注意!假期最后一天广州还有局部大暴雨雨后广州天气凉快了些,今天最高气温30 6℃,比昨天低了不少。端午假期
-
20万买台纯电轿跑,哪吒S是样子货吗?作为一款中大型纯电动车型,哪吒S(图片|配置|询价)2023款纯电520后驱Li
-
世界要闻:各级工会积极做好高温天气作业职工劳动保护工作发放藿香正气水等防暑降温用品;露天作业避开高温时段,缩短劳动者连续
-
阿媒:送餐员给梅西家送餐时激动落泪,梅西的儿子马特奥拥抱了他 世界消息直播吧6月23日讯据阿根廷媒体TN报道,一位来自阿根廷富内斯当地的油条
-
吃什么?去哪儿玩?数说端午消费_快资讯央视网消息:2023年端午节恰逢毕业季,不仅大家的出行意愿上升,节日消
-
35岁女演员定居美国,呼吁国人都来看一看,在当地组建高端华人圈 快播报6月22日,女主持人姜尘晒出了一则在美国的生活近况,自从去年来到美国
-
滚动:助警宝展翅高飞 让警营更有温度大河网讯“三年磨一剑,六月试锋芒”。每年的6月,“高考”一词必然成
-
天天关注:莉莉丝祭坛位置介绍莉莉丝祭坛位置在哪?莉莉丝祭坛是分布在暗黑破坏神4地图当中的很重要的
-
三方共赢!辽宁超新星被租借弱旅 他能成下个高诗岩?-今日热议三方共赢!辽宁超新星被租借弱旅他能成下个高诗岩?,高诗岩,cba,吴昌泽
-
每日热文:会声会影x8下载地址_春暖会花开x8有你地址1、就算你有别人的帐号。2、好像也改不了密码。本文就为大家分享到这里
-
双倍余额递减法例题公式_双倍余额递减法例题1、2007年前三月是按2006年的方式计提的,所以是=860×40%×3 12=86
-
热点!中国游客深夜被扔意大利街头!旅行社:当地司机不熟路 且不能接受加班跟团赴欧洲12日游,结果深夜被扔在意大利街头,一群游客站在路边等候。
-
刺梅刺梅,大戟科大戟属植物,也称多浆植物,原产地为非洲马达加斯加,是一
-
【全球聚看点】i57300hq相当于什么水平_i5 7300hq1、理论和解锁功耗下是8250U。2、但是实际上TDP及散热等情况来看。3、