springboot 2.0.0.RELEASE +spring cloud Finchley.M7 +springcloud bus+kafka实现配置动态刷新

1.服务端的配置信息

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,

适用于2.0的配置是这样的:

management.endpoints.web.exposure.include=*

接下来2.0的坑还有一个就是management.endpoints.web.exposure.include=*不支持yml格式,会报错,

所以有了bootstrap.properties文件:

management.endpoints.web.exposure.include=*


或者:

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

gradle依赖如下:

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文件:

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

运行成功后,会出现一些端点,此是与1.x不一样的地方:

2.接下来是客户端:

配置文件:

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

猜你喜欢

转载自my.oschina.net/u/2263272/blog/1634010