当前位置: 首页 >> 财务 > 内容页
Apollo2.1.0+Springboot使用OpenApI 环球关注
来源:博客园     时间:2023-06-24 17:20:49


【资料图】

依赖管理

                    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());    }}
[page]
标签:

广告

X 关闭

广告

X 关闭