官方链接与文档 写道
https://spring.io/guides/gs/centralized-configuration/
Spring cloud config Server基于Spring boot构建,
- 建议下载使用官方的Eclipse ( sts-3.7.2.RELEASE)
- 下载config server sample 的源码,并导入到eclipse工程中,gs-centralized-configuration-complete\configuration-service
- 修改配置文件
- 启动程序
配置文件 maven文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>configuration-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.8.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.properties:
server.port=8888
spring.application.name=configServer
#spring.cloud.config.server.git.uri=${HOME}/Desktop/config
spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=svn://IP:port/project/config
spring.cloud.config.server.svn.username=lvdccyb
spring.cloud.config.enabled=true
spring.cloud.config.server.svn.password=lvdccyb@126.com
spring.main.show-banner=false
其中这3项表面使用svn,并需要注意,远程url可用。
spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=svn://IP:port/project/config
spring.cloud.config.server.svn.username=lvdccyb
运行结果(部分内容去掉):
http://localhost:8888/env
{
"profiles": [
"subversion"
],
"bootstrap": {},
"servletContextInitParams": {},
"configServerClient": {
"spring.cloud.config.enabled": "false"
},
"applicationConfig: [classpath:/application.properties]": {
"spring.cloud.config.server.svn.password": "******",
"spring.cloud.config.server.svn.uri": "svn://X.Y.Z.U/project/config",
"server.port": "8888",
"spring.profiles.active": "subversion",
"spring.main.show-banner": "false",
"spring.cloud.config.enabled": "true",
"spring.cloud.config.server.svn.username": "lvdccyb",
"spring.application.name": "configServer"
},
"defaultProperties": {
"spring.application.name": "bootstrap"
}
}
http://localhost:8888/configuration-client/default 注意上面地址:对应的svn地址一般为: svn://IP:PORT/{$配置的uri}/trunk/configuration-client.properties 而且这个地址,需要仔细阅读spring 官方文档对application,profile,label的解释,以及路径的寻找优先级,上述地址缺省配置时,可以写成多种多样: http://localhost:8888/configuration-client/default/trunk http://localhost:8888/configuration-client/trunk/trunk http://localhost:8888/configuration-client/trunk 甚至如下: http://localhost:8888/configuration-client/abc 这里需要多一级trunk目录,而不是直接使用配置文件。 { "name": "configuration-client", "profiles": [ "default" ], "label": "trunk", "propertySources": [ { "name": "svn://IP:PORT/{$配置的uri}/trunk/configuration-client.properties", "source": { "IamApplicationName": "true", "message": "Hello World,I am spring client" } } ] }
—————————————————————————————————————————————
Config 客户端:
application.properties文件
#这个需要与配置文件对应起来
spring.application.name=configuration-client
spring.cloud.config.uri=http://localhost:8888
启动程序后,访问:
http://localhost:8080/message
可以看到:
Hello World,I am spring client
这个与前面SERVER端显示的内容一样,说明配置已经生效了。