<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實例的監聽器會生效。