Apollo配置参数读取后再做替换

1.先描述业务场景:
在Apollo上做配置,然后读取配置,然后做替换

2.一路坎坷:
2.1.在Apollo上配置,但是vlue包含有占位符“${}”,获取配置报错;

Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'loanName' in value

2.2.去掉$后,不能替换

3.最终代码

    public static final char DEFAULT_ESCAPE = '$';
    /**
     * Constant for the default variable prefix.
     */
    public static final StrMatcher DEFAULT_PREFIX = StrMatcher.stringMatcher("{");
    /**
     * Constant for the default variable suffix.
     */
    public static final StrMatcher DEFAULT_SUFFIX = StrMatcher.stringMatcher("}");

    public static void main(String[] args) {
        Map<Object, Object> paramMap = Maps.newHashMap();
        paramMap.put("username", "张三");
        paramMap.put("loanName", "手机银行");
        StrSubstitutor strSubstitutor = new StrSubstitutor(StrLookup.mapLookup(paramMap), DEFAULT_PREFIX, DEFAULT_SUFFIX, DEFAULT_ESCAPE);
        String message = strSubstitutor.replace("姓名:{username}, 产品名称:'{loanName}");
        System.out.println("message= " + message);

    }
### Feign 中集成 Apollo 进行配置管理 在分布式系统中,统一配置中心的作用至关重要。通过引入携程开源的 Apollo 配置管理中心[^1],可以在微服务架构下实现动态化、集中化的配置管理。以下是关于如何在 Feign 客户端中集成 Apollo 的具体方法。 #### 一、依赖引入 为了使 Feign 能够支持 Apollo 动态配置功能,首先需要在项目的 `pom.xml` 文件中添加必要的依赖项: ```xml <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.9.0</version> </dependency> <!-- Spring Cloud OpenFeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 上述代码片段展示了所需的 Maven 依赖项,用于连接 Apollo 和启用 Feign 支持。 --- #### 二、Apollo 初始化配置 在应用启动类上增加注解以激活 Apollo 自动注入的功能: ```java @SpringBootApplication @EnableApolloConfig @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 此部分代码实现了两个主要目标:一是启用了 Apollo 配置加载;二是注册了 Feign 客户端的支持能力[^3]。 --- #### 三、配置文件设置 创建或修改 `application.properties` 或者 `bootstrap.properties` 文件来指定 Apollo 的基础参数。例如: ```properties app.id=your-service-name apollo.meta=http://localhost:8070 apollo.bootstrap.enabled=true apollo.cacheDir=/tmp/apollo/ ``` 这些属性定义了应用程序 ID、Apollo Server 地址以及缓存路径等必要信息。 --- #### 四、Feign 接口声明与 Apollo 结合 下面是一个简单的 Feign 接口示例,展示如何利用 Apollo 提供的服务地址和其他动态配置数据: ```java @FeignClient(name = "${service.name}", url = "${service.url}") public interface ExampleService { @GetMapping("/example") String getExample(); } ``` 在此处 `${}` 占位符会被 Apollo 加载的实际值替换掉,从而达到灵活调整远程调用的目标位置的目的[^4]。 --- #### 五、刷新机制支持 如果希望当 Apollo 上面的某些键发生变化时能够立即生效,则可以通过以下方式完成热更新逻辑处理: ```yaml management: endpoints: web: exposure: include: refresh ``` 接着,在业务层或者控制器层面加入如下操作即可触发重新读取最新版本的数据源: ```java @RestController @RequestMapping("/api/v1/example") public class ExampleController { private final ExampleService exampleService; @Autowired public ExampleController(ExampleService exampleService) { this.exampleService = exampleService; } @PostMapping("/refresh") @RefreshScope public ResponseEntity<String> handleRefresh() { return ResponseEntity.ok("Configuration refreshed!"); } } ``` 以上步骤确保了即使是在运行状态下也可以轻松应对各种突发性的变更需求。 --- ### 总结 综上所述,借助 Apollo 实现 Feign 的外部化配置不仅简化了开发流程还增强了系统的可维护性和扩展性。同时需要注意的是,实际部署过程中可能还需要考虑安全性加固措施比如 SSL/TLS 加密传输等等额外因素[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值