1.服務端的配置信息java
application.yml server: port: 8888 spring: application: name: config-server cloud: config: server: git: uri: https://github.com/gholly/configProperties searchPaths: configPath username: xxx password: xxx label: master profile: master stream: kafka: binder: zk-nodes: localhost:2181 brokers: localhost:9092 bus: enabled: true
因爲2.0的改動較大,/bus/refresh所有整合到actuador裏面了,因此以前1.x的management.security.enabled所有失效,不適用於2.0,node
適用於2.0的配置是這樣的:git
management.endpoints.web.exposure.include=*
接下來2.0的坑還有一個就是management.endpoints.web.exposure.include=*不支持yml格式,會報錯,github
因此有了bootstrap.properties文件:web
management.endpoints.web.exposure.include=*
或者:spring
management: endpoints: web: exposure: include: bus-refresh
gradle依賴以下:bootstrap
buildscript { ext { springBootVersion = '2.0.0.RELEASE' } repositories { mavenCentral() maven { url "https://repo.spring.io/milestone" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.gholly.config.client' version = '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/milestone" } } ext { springCloudVersion = 'Finchley.M7' } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-actuator') compile('org.springframework.cloud:spring-cloud-starter-config') compile('org.springframework.cloud:spring-cloud-starter-bus-kafka') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
java文件:app
@EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class,args); } }
運行成功後,會出現一些端點,此是與1.x不同的地方:maven
2.接下來是客戶端:spring-boot
配置文件:
server: port: 8008 spring: cloud: config: uri: http://localhost:8888/ profile: dev label: master stream: kafka: binder: zk-nodes: localhost:2181 brokers: localhost:9092 bus: enabled: true application: name: config
bootstrap.properties文件:
management.endpoints.web.exposure.include=*
依賴:
buildscript { ext { springBootVersion = '2.0.0.RELEASE' } repositories { mavenCentral() maven { url "https://repo.spring.io/milestone" } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.cmbchina.csc.config.client' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() maven { url "https://repo.spring.io/milestone" } } ext { springCloudVersion = 'Finchley.M7' } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.cloud:spring-cloud-starter-config') compile('org.springframework.cloud:spring-cloud-starter-bus-kafka') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
java文件:
@RestController @RefreshScope @SpringBootApplication public class ClientApplication { public static void main(String[] args) { SpringApplication.run(ClientApplication.class,args); } @Value("${test}") public String hh; @RequestMapping("/hh") public String test(){ return hh; } }
3.測試階段:
當二者運行成功以後,訪問接口http://localhost:8008/hh
而後更改git上的配置文件,更改後,發現接口內容不變,而後以post方式調取localhost:8008/actuator/bus-refresh實現配置的動態刷新,雖然返回結果爲空,但確實是動態刷新了
刷新後的接口:
服務端在刷新接口產生的的日誌:
2018-03-13 15:26:16.761 INFO 2157 --- [container-0-C-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@33b22c4c: startup date [Tue Mar 13 15:26:16 CST 2018]; root of context hierarchy 2018-03-13 15:26:16.794 INFO 2157 --- [container-0-C-1] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$a2393a66] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-03-13 15:26:16.882 INFO 2157 --- [container-0-C-1] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default 2018-03-13 15:26:16.884 INFO 2157 --- [container-0-C-1] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@21fa3947: startup date [Tue Mar 13 15:26:16 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@33b22c4c 2018-03-13 15:26:16.922 INFO 2157 --- [container-0-C-1] o.s.boot.SpringApplication : Started application in 0.194 seconds (JVM running for 2752.366) 2018-03-13 15:26:16.922 INFO 2157 --- [container-0-C-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@21fa3947: startup date [Tue Mar 13 15:26:16 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@33b22c4c 2018-03-13 15:26:16.922 INFO 2157 --- [container-0-C-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@33b22c4c: startup date [Tue Mar 13 15:26:16 CST 2018]; root of context hierarchy 2018-03-13 15:26:16.981 INFO 2157 --- [container-0-C-1] o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed [] 2018-03-13 15:26:20.563 INFO 2157 --- [nio-8888-exec-6] .c.s.e.MultipleJGitEnvironmentRepository : Fetched for remote master and found 1 updates 2018-03-13 15:26:20.593 INFO 2157 --- [nio-8888-exec-6] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4bf8a208: startup date [Tue Mar 13 15:26:20 CST 2018]; root of context hierarchy 2018-03-13 15:26:20.658 INFO 2157 --- [nio-8888-exec-6] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/46/w1byj6gx4zz761fq7680m6fm0000gy/T/config-repo-3946134186696501765/configPath/config-dev.yml 2018-03-13 15:26:20.659 INFO 2157 --- [nio-8888-exec-6] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4bf8a208: startup date [Tue Mar 13 15:26:20 CST 2018]; root of context hierarchy 2018-03-13 15:26:22.086 INFO 2157 --- [nio-8888-exec-7] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@53fe9755: startup date [Tue Mar 13 15:26:22 CST 2018]; root of context hierarchy 2018-03-13 15:26:22.107 INFO 2157 --- [nio-8888-exec-7] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/46/w1byj6gx4zz761fq7680m6fm0000gy/T/config-repo-3946134186696501765/configPath/config-dev.yml 2018-03-13 15:26:22.108 INFO 2157 --- [nio-8888-exec-7] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@53fe9755: startup date [Tue Mar 13 15:26:22 CST 2018]; root of context hierarchy
客戶端在刷新接口時產生的日誌:
2018-03-13 15:26:16.760 INFO 2169 --- [nio-8008-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@713b760d: startup date [Tue Mar 13 15:26:16 CST 2018]; root of context hierarchy 2018-03-13 15:26:16.787 INFO 2169 --- [nio-8008-exec-3] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$3c8b2b60] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2018-03-13 15:26:16.848 INFO 2169 --- [nio-8008-exec-3] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888 2018-03-13 15:26:20.661 INFO 2169 --- [nio-8008-exec-3] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config, profiles=[dev], label=master, version=7947aa4d0cfd81366754064f976f15085380ab31, state=null 2018-03-13 15:26:20.661 INFO 2169 --- [nio-8008-exec-3] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/gholly/configProperties/configPath/config-dev.yml'}]} 2018-03-13 15:26:20.666 INFO 2169 --- [nio-8008-exec-3] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default 2018-03-13 15:26:20.668 INFO 2169 --- [nio-8008-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@38aee00c: startup date [Tue Mar 13 15:26:20 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@713b760d 2018-03-13 15:26:20.688 INFO 2169 --- [nio-8008-exec-3] o.s.boot.SpringApplication : Started application in 3.961 seconds (JVM running for 2676.204) 2018-03-13 15:26:20.688 INFO 2169 --- [nio-8008-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@38aee00c: startup date [Tue Mar 13 15:26:20 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@713b760d 2018-03-13 15:26:20.688 INFO 2169 --- [nio-8008-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@713b760d: startup date [Tue Mar 13 15:26:16 CST 2018]; root of context hierarchy 2018-03-13 15:26:20.754 INFO 2169 --- [nio-8008-exec-3] o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed [config.client.version, test]
以上示例的代碼位置: https://github.com/gholly/springCloudConfigServer
https://github.com/gholly/springCloudConfigClient