SpringBoot中使用Apollo

1、使用示例

maven:

<!--apollo-->
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-openapi</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.1.0</version>
</dependency>

application.properties:

#apollo
app.id=10

几种配置Apollo的url的方式:

第一种:apollo-env.properties:

dev.meta=http://192.168.4.32:8080
fat.meta=http://192.168.4.32:8081
uat.meta=http://192.168.4.32:80800
pro.meta=http://172.31.103.238:18080

第一种方式:通过 C:\opt\settings\server.properties 文件指定 env=DEV 自动装配 dev.meta 的 url。
第二种方式:通过指定 jvm启动参数 -Denv=DEV 也可以达到上面效果,服务端则只需要 Java -Denv=DEV -jar xxx.jar

第二种:applicaition.properties

apollo.meta=http://config-service-url

第三种:通过 java 配置文件系统指定配置

启动类添加:System.setProperties("apollo.meta","configService-url");	
jvm 启动参数添加:-Dapollo.meta=http://config-service-url

配置注解解释:

@EnableApolloConfig({"application","tec.base"}):启动项目会在resource寻找Apollo的url配置文件。
@ApolloConfig("tec.base"):将Apollo服务端的中的配置注入这个类中。
@ApolloConfigChangeListener:监听配置中心配置的更新事件,若该事件发生,则调用refreshLoggingLevels方法,处理该事件。

配置类:

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.stereotype.Component;

@Component
@EnableApolloConfig({"application","tec.base"})
public class ApolloApiConfig {

    @ApolloConfig("application")
    private Config config;
	
	@ApolloConfig("tec.base")
private Config configBase;

    @ApolloConfigChangeListener
    private void change(ConfigChangeEvent changeEvent) {
    }
}

使用配置项:

1.@Value( "${server.port}" )
2.config.getProperty("server.port", null);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值