Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的全部环境提供了一个中心化的外部配置。java
部署分布式配置管理分为如下4部git
spring.application.name=config-server # config server port server.port=8887 # use file system backend spring.profiles.active=native # url of file system where configuration files stored spring.cloud.config.server.native.searchLocations=file:////root/config-repo
Spring Cloud Config提供本地存储配置的方式。咱们只须要设置属性spring.profiles.active=native
,Config Server会默认从应用的src/main/resource
目录下检索配置文件。也能够经过spring.cloud.config.server.native.searchLocations=
file:////root/config-repo性来指定配置文件的位置,这样的话就能够将全部的配置文件统一管理。固然也能够从git上获取。spring
目录config-repo用于存放全部的配置文件bootstrap
spring.application.name=config-server # config server port server.port=8887 # use file system backend spring.profiles.active=native # url of file system where configuration files stored spring.cloud.config.server.native.searchLocations=file:////root/config-repo # 配置服务注册中心 eureka.client.serviceUrl.defaultZone=http://test:111@localhost:8761/eureka/,http://test:111@1ocalhost:8762/eureka/ # 在eurake注册的名称是IP地址+端口 eureka.instance.preferIpAddress=true eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
将config-servefr注册服务该怎么作呢?很简单在对应的pom文件中添加以下配置:服务器
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
spring.application.name=zuul spring.cloud.config.profile=configInfo spring.cloud.config.label=devs eureka.client.serviceUrl.defaultZone=http://test:111@localhost:8761/eureka/,http://test:111@localhost:8762/eureka/ # 在eurake注册的名称是IP地址+端口 eureka.instance.preferIpAddress=true eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=config-server
这里须要格外注意:上面这些属性必须配置在bootstrap.properties
中,config部份内容才能被正确加载。由于config的相关配置会先于application.properties
,而bootstrap.properties
的加载也是先于application.properties
。app
其中,经过eureka.client.serviceUrl.defaultZone
参数指定服务注册中心,用于服务的注册与发现,再将spring.cloud.config.discovery.enabled
参数设置为true,开启经过服务来访问Config Server的功能,最后利用spring.cloud.config.discovery.serviceId
参数来指定Config Server注册的服务名。这里的spring.application.name
和spring.cloud.config.profile
用来定位config-server的资源。分布式
客户端对应的pom文件中须要添加以下配置this
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
spring.datasource.url=jdbc\:db2\://localhost\:50000/hrxtdev spring.datasource.username= spring.datasource.password= spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver\ config.server.url=http://localhost:8080 server.port =8180 server.context-path =/log
@Component @ConfigurationProperties(prefix = "config.server") public class ConfigServerProperties{ private String url; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }