<div align=center>java
图1git
<div align=left>github
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-stater</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <!-- apollo客户端 --> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>${apollo.version}</version> </dependency>
<div align=center>web
图2spring
<div align=left> 3) 或者不在项目配置apollo-env.properties,而是直接在application.properties指定apollo.meta=ip:port的方式来执行须要读取配置的的服务bootstrap
@EnableApolloConfig
注解@SpringBootApplication @EnableApolloConfig public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
若是不使用@EnableApolloConfig
注解,能够在application.properties里面配置apollo.bootstrap.enabled=true
,效果同样。 使用apollo.bootstrap.namespaces = application,test_namespace
能够指定命名空间。windows
启动项目:app
<div align=center>ide
图3spring-boot
<div align=left> 能够看到应用在启动前从配置中心获取配置信息来启动应用。 `@EnableApolloConfig`默认是从application命名空间获取配置的,至关于`@EnableApolloConfig("application")`.。
application命名空间配置信息 java bean:
@Component @EnableApolloConfig @Getter @Setter @ToString public class AppNamespace { @Value("${spring.application.name:}") private String name; @Value("${server.port:}") private String value; }
java bean:
@Component @EnableApolloConfig("CASE.test_namespace") @Getter @Setter @ToString public class TestNamespace { @Value("${name}") private String name; @Value("${value}") private String value; }
使用:
@RestController public class DemoController { @Autowired private TestNamespace demo; @Autowired private AppNamespace application; @ApolloConfig private Config appConfig; @ApolloConfig("CASE.test_namespace") private Config testConfig1; private Config testConfig2 = ConfigService.getConfig("CASE.test_namespace"); }
以上两种方式获取配置信息的值,会跟配置中心的更改同步(1秒内);还能够使用@ConfigurationProperties
来获取配置信息,但这种方式不会同步更新,须要额外的编码配置才能实现,具体查看官方文档。
@ApolloJsonValue
注解,做用至关于@Value
,将JSON字符串转成对象。
@ApolloConfigChangeListener
注解::
@ApolloConfigChangeListener private void someOnChange(ConfigChangeEvent changeEvent) { //update injected value of batch if it is changed in Apollo if (changeEvent.isChanged("key")) { System.out.println(config.getIntProperty("key", "")); } }
@ApolloConfigChangeListener
至关于@ApolloConfigChangeListener("application")
至关于:
Config config = ConfigService.getAppConfig(); config.addChangeListener(new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { System.out.println("Changes for namespace " + changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType())); } } });
若是同时以两种方式绑定changeListener的方式,只有ConfigService实例的监听器会生效。