spring Cloud-config(配置管理)
1.Config Server
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
pom配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.chry</groupId> 6 <artifactId>springcloud.helloworld.config.server</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <packaging>jar</packaging> 9 <name>helloworld.config.server</name> 10 <description>Demo Config Server</description> 11 12 <parent> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-parent</artifactId> 15 <version>1.5.3.RELEASE</version> 16 <relativePath/> <!-- lookup parent from repository --> 17 </parent> 18 19 <properties> 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <java.version>1.8</java.version> 23 </properties> 24 25 <dependencies> 26 <!--eureka server --> 27 <dependency> 28 <groupId>org.springframework.cloud</groupId> 29 <artifactId>spring-cloud-starter-eureka</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.cloud</groupId> 33 <artifactId>spring-cloud-starter-eureka-server</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.springframework.cloud</groupId> 37 <artifactId>spring-cloud-config-server</artifactId> 38 </dependency> 39 <!-- spring boot test--> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-test</artifactId> 43 <scope>test</scope> 44 </dependency> 45 </dependencies> 46 47 <dependencyManagement> 48 <dependencies> 49 <dependency> 50 <groupId>org.springframework.cloud</groupId> 51 <artifactId>spring-cloud-dependencies</artifactId> 52 <version>Dalston.RC1</version> 53 <type>pom</type> 54 <scope>import</scope> 55 </dependency> 56 </dependencies> 57 </dependencyManagement> 58 59 <build> 60 <plugins> 61 <plugin> 62 <groupId>org.springframework.boot</groupId> 63 <artifactId>spring-boot-maven-plugin</artifactId> 64 </plugin> 65 </plugins> 66 </build> 67 68 <repositories> 69 <repository> 70 <id>spring-milestones</id> 71 <name>Spring Milestones</name> 72 <url>https://repo.spring.io/milestone</url> 73 <snapshots> 74 <enabled>false</enabled> 75 </snapshots> 76 </repository> 77 </repositories> 78 79 </project> 80 81 pom.xml
3.Config server的配置文件appication.yml , 注意配置文件的url是GIT服务器的仓库地址, searchPaths配置文件所在的文件夹在仓库中的路径, 在server端不需要指定具体配置文件名, 因为具体的配置文件是什么有作用(也就是client)决定。
1 eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://localhost:8761/eureka/ 5 server: 6 port: 8888 7 8 spring: 9 cloud: 10 config: 11 server: 12 git: 13 uri: https://git.oschina.net/chrywhy/test 14 searchPaths: spring-cloud/helloworldConfig 15 application: 16 name: config-server
2.config client
创建config client不需要额外的注解配置
1.pom配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.chry</groupId> 5 <artifactId>Springcloud.helloworld.config.client</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <name>Springcloud.helloworld.config.client</name> 8 <packaging>jar</packaging> 9 <description>Demo Spring Config Client</description> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>1.5.3.RELEASE</version> 15 <relativePath/> <!-- lookup parent from repository --> 16 </parent> 17 18 <properties> 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-eureka</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-config</artifactId> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-test</artifactId> 40 <scope>test</scope> 41 </dependency> 42 </dependencies> 43 44 <dependencyManagement> 45 <dependencies> 46 <dependency> 47 <groupId>org.springframework.cloud</groupId> 48 <artifactId>spring-cloud-dependencies</artifactId> 49 <version>Dalston.RC1</version> 50 <type>pom</type> 51 <scope>import</scope> 52 </dependency> 53 </dependencies> 54 </dependencyManagement> 55 56 <build> 57 <plugins> 58 <plugin> 59 <groupId>org.springframework.boot</groupId> 60 <artifactId>spring-boot-maven-plugin</artifactId> 61 </plugin> 62 </plugins> 63 </build> 64 65 <repositories> 66 <repository> 67 <id>spring-milestones</id> 68 <name>Spring Milestones</name> 69 <url>https://repo.spring.io/milestone</url> 70 <snapshots> 71 <enabled>false</enabled> 72 </snapshots> 73 </repository> 74 </repositories> 75 76 77 </project> 78 79 pom.xml
2.创建一个spring boot应用作为client 无需额外的配置,平常的spring boot启动类
3.配置文件bootstrap.yml (或者bootstrap.properties)
spring: application: name: config-client cloud: config: label: master profile: dev uri: http://localhost:8888/ server: port: 8881
应用的名字是config-client(这就是将要用于组装前面Config Server一节中题到的application)
profile采用dev
GIT分支用master
url是config server的地址
应用的配置文件名以如下方式组成:{application}-{profile}.properties(或者{application}-{profile}.yml)。
==》我们这个应用的配置文件名就是config-client-dev.properties.
综server和client所述。最终我们要创建的配置文件地址为:
spring-cloud/helloworldConfig/config-client-dev.properties
转载自:http://www.cnblogs.com/chry/p/7250584.html