Foreword
After reconstitution micro-services, each service requires the deployment of a number of instances, each instance is not possible to manually modify the configuration is modified, so use springboot-config. Wanted to use git, or due to internal problems can only use svn, the process of setting up a configuration center record
Create a directory to upload SVN configuration
This step would not elaborate ... the ultimate directory of http: //....../config-repo/trunk/**/**--dev.yml,http: // ..... ./config-repo/trunk/**/**--prod.yml
Here on the trunk line to distinguish the directory and development, and finally the dev and prod suffix is also used to distinguish the environment (the default is because svn trunk, this layer must have a catalog, in fact, this layer is not necessary)
config-server
Configuration Center is also a service, pom file needs to be introduced
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
application.yml following documents, use svn if the Spring. profiles.active: Subversion this must be specified
Server:
Port: 8888
Spring:
file application:
name:-config-Service
Profiles:
Active: # Subversion using the svn
Cloud:
config:
Server:
the svn:
URI: HTTP: // ** #svn address
search-paths: "{application} " use # {application} placeholder must be added to "" search folder does not recognize or
username: User
password: pwd
default-label: Trunk
Eureka:
Client:
-Service-URL:
defaultzone: http://0.0.0.0:8761/eureka /
# allow / actuator / bus-refresh the interface is called external, manually refresh config
Management:
Endpoints:
Web:
Exposure:
the include: "*"
config-server startup class services need to add @EnableConfigServer comment
So far the server build better, try to access the http can start: // 0.0.0.0: 8888 / (profile name) to see if you can normally access
Warehouse configuration file is converted into a web interface, access can refer to the following rules:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
In neo-config-dev.properties as an example, its application is neo-config, profile is dev. client may choose to read the corresponding configuration parameter according to fill
configserver every time reading from the latest version of svn
The client (micro server)
Add dependent
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Each service configuration files need named bootstrap.yml, springboot priority will read the name of the configuration file at startup
the Spring:
the Application:
name: entry-Service
Profiles:
the Active: Subversion
Cloud:
config:
name: {the Application}
label: Trunk
Discovery:
Enabled: to true
Service-the above mentioned id: service name service-config # registry
profile: dev # specify the configuration environment file
Eureka:
Client:
serviceUrl:
defaultzone: http://0.0.0.0:8761/eureka/
As the configuration file, you need to specify the id of the distribution center is in fact config-server of application.name, and then use the svn the Spring. Profiles.active: Subversion This must also be specified
Starting to read from the configuration center After the configuration, in the use configuration classes with the injection @Value @RefreshScope , and access / actuator / refresh can refresh configuration
Two problems encountered:
1. Because the data item is a multi-source, using a custom DataSource Data source configuration, with @Bean injection.
Hikari SpringBoot 2.0 above using the default connection pool, connection pool once started, can not edit HikariDataSource, so refresh the data sources associated with the refresh configuration, then error.
Caused by: java.lang.IllegalStateException: The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes.
Solution: add comments @RefreshScope on DataSource custom, or use spring.scloud.refresh.extra-refreshable configuration can be specified classname list.
2. Use / refresh message 404, SpringBoot 2.0 after the interface / actuator / refresh, and must request permission to post