Configuration Center Introduction
config server to read configuration from the local git or warehouse, it may be placed on a module arranged in management.
Configuring High Availability config server
The config-server configuration to a eureka-server service in unified management, config-server can be configured to a cluster:
New config server module
a) introducing a dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
b) profile application.yml
spring: cloud: config: server: native: search-locations: classpath:/shared Search # -locations: File: E: // the Java / Local Disk profiles: active: native application: name: config-server # The eureka server instance is registered to the service in management eureka: client: ServiceUrl: defaultZone: http://localhost:8761/eureka/
Build shared folder in the project resource in and create a new file config-client-dev.yml, add the following
server: port: 8783 foo: foo version 1
c) Start class annotate
@EnableConfigServer
New config client module
a) introducing a dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
b) the configuration file, bootstrap.yml
spring: application: name: config-client cloud: config: fail-fast: true discovery: enabled: true serviceId: config-server profiles: active: dev eureka: client: ServiceUrl: defaultZone: http://localhost:8761/eureka/
The {spring.application.name} - {profiles.active} config-server module name to the shared directory query profile
c) Start class is not to add another comment
d) use, and configured to use the same local
@SpringBootApplication @RestController @EnableEurekaClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${foo}") String foo; @RequestMapping(value = "/foo") public String hi(){ return foo; } }
test
Enter the address in the browser: HTTP: // localhost: 8783 / foo
Use Spring Cloud Bus refresh configuration
If we want to modify the configuration file, it does not restart the project with immediate effect, you need to use the spring cloud bus refresh configuration.
Modify config client module
Only need to modify the config-client module, you can achieve dynamic refresh
a) introducing a dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
b) modify the configuration file, add the following, first install RabbitMQ,
rabbitmq: host: localhost port: 15672 username: guest password: guest Publisher -confirms: to true Virtual -host: / # first security policy is set to false
management: security: enabled: false
c) adding annotations @RefreshScope class configured in the use of
@RestController @RefreshScope public class ConfigClientApplication { @Value("${foo}") String foo; @RequestMapping(value = "/foo") public String hi(){ return foo; } }
test
Config-client-dev.yml modify the configuration files in config-server project directory shared, then send post request http: // localhost: 8781 / bus / refresh, then HTTP: // localhost: 8783 / foo , you will find profiles modify the content in force