Apollo配置中心 + springboot 简单使用
引入依赖
< dependency>
< groupId> com.ctrip.framework.apollo</ groupId>
< artifactId> apollo-client</ artifactId>
< version> 1.1.0</ version>
</ dependency>
添加配置
apollo.bootstrap.enabled=true
# will inject 'application', 'FX.apollo' and 'application.yml' namespaces in bootstrap phase
apollo.bootstrap.namespaces=application
apollo.bootstrap.eagerLoad.enabled=true
#配置configservice地址
apollo.meta=http://localhost:8091
# test
app.id=SampleApp
简单使用
import com. ctrip. framework. apollo. Config;
import com. ctrip. framework. apollo. spring. annotation. ApolloConfig;
import lombok. extern. slf4j. Slf4j;
import org. springframework. beans. factory. annotation. Value;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
import org. springframework. scheduling. annotation. EnableScheduling;
import org. springframework. scheduling. annotation. Scheduled;
@SpringBootApplication
@EnableScheduling
@Slf4j
public class OppositeApplication {
@Value ( "${test:1}" )
public String test;
@Value ( "${test1:1}" )
public String test1;
@ApolloConfig ( "application" )
private Config anotherConfig;
@Scheduled ( cron = "0/1 * * * * ?" )
public void test ( ) {
log. info ( "定时输出配置:test:{}" , test) ;
log. info ( "定时输出配置:test1:{}" , test1) ;
log. info ( "定时输出配置:anotherConfig测试test:{}" , anotherConfig. getProperty ( "test" , "test" ) ) ;
log. info ( "定时输出配置:anotherConfig测试test1:{}" , anotherConfig. getProperty ( "test1" , "test1" ) ) ;
}
public static void main ( String[ ] args) {
SpringApplication. run ( OppositeApplication. class , args) ;
}
}