免费个人域名网站,排名优化专家,wordpress 大数据备份,江苏网站建设找哪家一、SpringBoot集成1.发布配置信息设置本机为DEV环境#xff1a;Linux在/opt/settings/server.properties增加配置envDEV#xff0c;windows在c:\opt\settings\server.properties图1 在apollo portal上新建项目后#xff0c;默认就有了application命名空间。在DEV环境下新建…一、SpringBoot集成1.发布配置信息设置本机为DEV环境Linux在/opt/settings/server.properties增加配置envDEVwindows在c:\opt\settings\server.properties 图1 在apollo portal上新建项目后默认就有了application命名空间。在DEV环境下新建一个名为test_namespace的命名空间。在application上发布spring.applicaton.nameapollo_demoserver.port9000。在test_name上发布namename1, valuevalue2。2.引入依赖org.springframework.bootspring-boot-staterorg.springframework.bootspring-boot-starter-weborg.projectlomboklombok${lombok.version}providedcom.ctrip.framework.apolloapollo-client${apollo.version}3.项目配置在项目的application.properties(applicaiton.yml)或者/META-INF/app.properties填入app.idappId(在apollo-portal上新建项目时填写的appId表示获取的是那个配置项目的配置信息)。resources目录下新建apollo-env.properties填写各个环境的meta server地址 图2 3) 或者不在项目配置apollo-env.properties而是直接在application.properties指定apollo.metaip:port的方式来执行需要读取配置的的服务使用application命名空间的配置信息来启动SpringBoot应用 入口方法增加EnableApolloConfig注解SpringBootApplicationEnableApolloConfigpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}如果不使用EnableApolloConfig注解可以在application.properties里面配置apollo.bootstrap.enabledtrue效果一样。 使用apollo.bootstrap.namespaces application,test_namespace可以指定命名空间。启动项目 图3 可以看到应用在启动前从配置中心获取配置信息来启动应用。 EnableApolloConfig默认是从application命名空间获取配置的相当于EnableApolloConfig(application).。4.获取配置application命名空间配置信息 java bean:ComponentEnableApolloConfigGetterSetterToStringpublic class AppNamespace {Value(${spring.application.name:})private String name;Value(${server.port:})private String value;}java bean:ComponentEnableApolloConfig(CASE.test_namespace)GetterSetterToStringpublic class TestNamespace {Value(${name})private String name;Value(${value})private String value;}使用:RestControllerpublic class DemoController {Autowiredprivate TestNamespace demo;Autowiredprivate AppNamespace application;ApolloConfigprivate Config appConfig;ApolloConfig(CASE.test_namespace)private Config testConfig1;private Config testConfig2 ConfigService.getConfig(CASE.test_namespace);}以上两种方式获取配置信息的值会跟配置中心的更改同步(1秒内)还可以使用ConfigurationProperties来获取配置信息但这种方式不会同步更新需要额外的编码配置才能实现具体查看官方文档。5.其他ApolloJsonValue注解作用相当于Value将JSON字符串转成对象。ApolloConfigChangeListener注解:ApolloConfigChangeListenerprivate void someOnChange(ConfigChangeEvent changeEvent) {//update injected value of batch if it is changed in Apolloif (changeEvent.isChanged(key)) {System.out.println(config.getIntProperty(key, ));}}ApolloConfigChangeListener相当于ApolloConfigChangeListener(application)相当于Config config ConfigService.getAppConfig();config.addChangeListener(new ConfigChangeListener() {Overridepublic 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实例的监听器会生效。2.其他