spring cloud config 用svn做配置文件仓库

刚接触spring cloud 的 ;做了一个svn做配置文件仓库demo;


创建一个spring boot工程作为 spring  cloud config  server ;下面是pom.xml文件;

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <parent>  
  8.         <groupId>org.springframework.boot</groupId>  
  9.         <artifactId>spring-boot-starter-parent</artifactId>  
  10.         <version>1.2.3.RELEASE</version>  
  11.         <relativePath /> <!-- lookup parent from repository -->  
  12.     </parent>  
  13.   
  14.     <groupId>com.honkib.springcloud</groupId>  
  15.     <artifactId>spring-cloud-config-server</artifactId>  
  16.     <version>1.0-SNAPSHOT</version>  
  17.   
  18.     <dependencyManagement>  
  19.         <dependencies>  
  20.             <dependency>  
  21.                 <groupId>org.springframework.cloud</groupId>  
  22.                 <artifactId>spring-cloud-config</artifactId>  
  23.                 <version>1.0.4.RELEASE</version>  
  24.                 <type>pom</type>  
  25.                 <scope>import</scope>  
  26.             </dependency>  
  27.         </dependencies>  
  28.     </dependencyManagement>  
  29.     <dependencies>  
  30.         <dependency>  
  31.             <groupId>org.springframework.cloud</groupId>  
  32.             <artifactId>spring-cloud-starter-config</artifactId>  
  33.         </dependency>  
  34.   
  35.         <!--表示为web工程-->  
  36.         <dependency>  
  37.             <groupId>org.springframework.boot</groupId>  
  38.             <artifactId>spring-boot-starter-web</artifactId>  
  39.         </dependency>  
  40.   
  41.         <!--暴露各种指标-->  
  42.         <dependency>  
  43.             <groupId>org.springframework.boot</groupId>  
  44.             <artifactId>spring-boot-starter-actuator</artifactId>  
  45.         </dependency>  
  46.   
  47.         <dependency>  
  48.             <groupId>org.springframework.cloud</groupId>  
  49.             <artifactId>spring-cloud-config-server</artifactId>  
  50.         </dependency>  
  51.           
  52.          <!--config server need read svn-->  
  53.         <dependency>  
  54.             <groupId>org.tmatesoft.svnkit</groupId>  
  55.             <artifactId>svnkit</artifactId>  
  56.             <version>1.8.10</version>  
  57.         </dependency>  
  58.   
  59.     </dependencies>  
  60.   
  61.     <build>  
  62.         <plugins>  
  63.             <plugin>  
  64.                 <groupId>org.springframework.boot</groupId>  
  65.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  66.             </plugin>  
  67.         </plugins>  
  68.     </build>  
  69.   
  70. </project>  

configserver程序结构:spring cloud config 用svn做配置文件仓库


然后创建一个application.java 类

[java] view plain copy
  1. package com.hobkib.cloud.demo;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.cloud.config.server.EnableConfigServer;  
  7. import org.springframework.context.annotation.Configuration;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. /** 
  11.  * Created by liaokailin on 16/4/28. 
  12.  */  
  13.   
  14. @Configuration  
  15. @EnableAutoConfiguration  
  16. @EnableConfigServer  
  17. public class Application {  
  18.   
  19.     public static void main(String[] args) {  
  20.         SpringApplication.run(Application.class, args);  
  21.     }  
  22.       
  23. }  

其中@EnableConfigServer 为作为配置文件服务的注解;


appliction.yml配置文件:

[html] view plain copy
  1. server:  
  2.   port: 8888  
  3.   
  4. spring:  
  5.   cloud:  
  6.     config:  
  7.       enabled: true  
  8.       server:  
  9.         svn:  
  10.           uri: http://192.168.0.66/svn/cloudconfig/  
  11.           username: chenhua  
  12.           password: 123456  
  13.         #git:  
  14.         #  uri: https://github.com/pcf-guides/configuration-server-config-repo  
  15.         default-label: config  
  16.   profiles:  
  17.     active: subversion  
  18.   
  19. logging:  
  20.   levels:  
  21.     org.springframework.boot.env.PropertySourcesLoader: TRACE  
  22.     org.springframework.cloud.config.server: DEBUG  



创建一个svn仓库作为spring cloud 的配置文件库;

spring cloud config 用svn做配置文件仓库spring cloud config 用svn做配置文件仓库


这张图片中配置文件的结构写错了;

配置文件的结构应该是在客户端的 配置文件appliction.preperties文件中的两个字段;(spring.cloud.config.name  和  spring.cloud.config.profile)

       要符合{spring.cloud.config.name}-{spring.cloud.config.profile}.preperties 或者后缀为.yml的配置文件;


创建svn库后一定要创建一个文件夹;(必须的),不知道是是不是读取库的问题;默认情况下,他会去这你提供的svn库uri中的trunk文件夹下读取(可以配置这个文件夹的名称。配置成空默认去trunk文件夹查找配置文件),配置文件不能放到svn的根目录下(默认去trunk的文件夹下找配置文件);


客户端工程:

客户端的工程结构:

spring cloud config 用svn做配置文件仓库

客户端的配置文件:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.     <modelVersion>4.0.0</modelVersion>  
  6.   
  7.     <groupId>com.honkib.springcloud</groupId>  
  8.     <artifactId>spring-cloud-config-client</artifactId>  
  9.     <version>1.0-SNAPSHOT</version>  
  10.   
  11.   
  12.     <parent>  
  13.         <groupId>org.springframework.boot</groupId>  
  14.         <artifactId>spring-boot-starter-parent</artifactId>  
  15.         <version>1.2.3.RELEASE</version>  
  16.         <relativePath /> <!-- lookup parent from repository -->  
  17.     </parent>  
  18.       
  19.   
  20.   
  21.     <dependencyManagement>  
  22.         <dependencies>  
  23.             <dependency>  
  24.                 <groupId>org.springframework.cloud</groupId>  
  25.                 <artifactId>spring-cloud-starter-parent</artifactId>  
  26.                 <version>1.0.1.RELEASE</version>  
  27.                 <type>pom</type>  
  28.                 <scope>import</scope>  
  29.             </dependency>  
  30.         </dependencies>  
  31.     </dependencyManagement>  
  32.   
  33.   
  34.     <dependencies>  
  35.         <dependency>  
  36.             <groupId>org.springframework.boot</groupId>  
  37.             <artifactId>spring-boot-starter-web</artifactId>  
  38.         </dependency>  
  39.         <dependency>  
  40.             <groupId>org.springframework.cloud</groupId>  
  41.             <artifactId>spring-cloud-config-client</artifactId>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>org.springframework.boot</groupId>  
  45.             <artifactId>spring-boot-starter-actuator</artifactId>  
  46.         </dependency>  
  47.   
  48.         <dependency>  
  49.             <groupId>org.springframework.boot</groupId>  
  50.             <artifactId>spring-boot-starter-test</artifactId>  
  51.             <scope>test</scope>  
  52.         </dependency>  
  53.           
  54.         <dependency>  
  55.             <groupId>org.springframework.boot</groupId>  
  56.             <artifactId>spring-boot-starter-actuator</artifactId>  
  57.         </dependency>  
  58.           
  59.     </dependencies>  
  60.     <repositories>  
  61.         <repository>  
  62.             <id>spring-milestones</id>  
  63.             <name>Spring Milestones</name>  
  64.             <url>http://repo.spring.io/milestone</url>  
  65.             <snapshots>  
  66.                 <enabled>false</enabled>  
  67.             </snapshots>  
  68.         </repository>  
  69.     </repositories>  
  70.     <pluginRepositories>  
  71.         <pluginRepository>  
  72.             <id>spring-milestones</id>  
  73.             <name>Spring Milestones</name>  
  74.             <url>http://repo.spring.io/milestone</url>  
  75.             <snapshots>  
  76.                 <enabled>false</enabled>  
  77.             </snapshots>  
  78.         </pluginRepository>  
  79.     </pluginRepositories>  
  80.   
  81.     <build>  
  82.         <plugins>  
  83.             <plugin>  
  84.                 <groupId>org.springframework.boot</groupId>  
  85.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  86.             </plugin>  
  87.         </plugins>  
  88.     </build>  
  89. </project>  

appliction.java

[java] view plain copy
  1. package com.hobkib.cloud.democlient;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. /** 
  10.  * Created by liaokailin on 16/4/28. 
  11.  */  
  12. @SpringBootApplication  
  13. @RestController  
  14. public class Application {  
  15.     @Value("${mysqldb.datasource.url:World}")  
  16.     String bar;  
  17.   
  18.     @RequestMapping("/")  
  19.     String hello() {  
  20.         return "Hello " + bar + "!";  
  21.     }  
  22.   
  23.     public static void main(String[] args) {  
  24.         SpringApplication.run(Application.class, args);  
  25.     }  
  26. }  

bootstrap.preperties 配置文件:

[html] view plain copy
  1. server.port=8081  
  2. spring.cloud.config.uri=http://localhost:${config.port:8888}  
  3. spring.cloud.config.name=cloud-config  
  4. spring.cloud.config.profile=${config.profile:dev}  
  5.   
  6. spring.application.name=cloud-simple-service  


这样程序就已经完成了;


接下来就是运行程序了;

先启动configserver 程序;

然后再启动client工程;

成功了的话;我们应该能在client程序的日志中看到这么一条日志:

2017-02-16 15:32:54.209  INFO 9836 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='http://192.168.0.66/svn/cloudconfig/config/cloud-config-dev.properties']]]

这就表明已经读取到日志了;

接下来只要在@Value("${key}")就可以获取到配置文件的值了;

或者好用env.getPreperty来获取配置文件信息;


注:很重要的一点;不知道的是什么问题;configserver服务端一定要用yml配置文件,使用preperties配置文件启动不成功;不识别;

还有就是注意svn的文件目录结构;配置文件名称结构