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


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

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

    
    
        4.0.0
    
        
            org.springframework.boot
            spring-boot-starter-parent
            1.2.3.RELEASE
             
        
    
        com.honkib.springcloud
        spring-cloud-config-server
        1.0-SNAPSHOT
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-config
                    1.0.4.RELEASE
                    pom
                    import
                
            
        
        
            
                org.springframework.cloud
                spring-cloud-starter-config
            
    
            
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
            
                org.springframework.cloud
                spring-cloud-config-server
            
            
             
            
                org.tmatesoft.svnkit
                svnkit
                1.8.10
            
    
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    configserver程序结构:

    然后创建一个application.java 类

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

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

    appliction.yml配置文件:

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

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

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

    配置文件的结构应该是在客户端的 配置文件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的文件夹下找配置文件);

    客户端工程:

    客户端的工程结构:

    客户端的配置文件:

    
    
        4.0.0
    
        com.honkib.springcloud
        spring-cloud-config-client
        1.0-SNAPSHOT
    
    
        
            org.springframework.boot
            spring-boot-starter-parent
            1.2.3.RELEASE
             
        
        
    
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-starter-parent
                    1.0.1.RELEASE
                    pom
                    import
                
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.cloud
                spring-cloud-config-client
            
            
                org.springframework.boot
                spring-boot-starter-actuator
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
    		
    			org.springframework.boot
    			spring-boot-starter-actuator
    		
            
        
        
            
                spring-milestones
                Spring Milestones
                http://repo.spring.io/milestone
                
                    false
                
            
        
        
            
                spring-milestones
                Spring Milestones
                http://repo.spring.io/milestone
                
                    false
                
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89

    appliction.java

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

    bootstrap.preperties 配置文件:

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

    这样程序就已经完成了;

    接下来就是运行程序了;

    先启动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的文件目录结构;配置文件名称结构

  • 相关阅读:
    Linux命令大全
    Day43——约束条件之主键与外键
    经纬度转笛卡尔坐标
    MAC 版PowerPoint 插入latex数学公式
    2022.07.29 Linux矩阵键盘驱动开发笔记2
    win7常见问题
    快速选择排序
    11在SpringMVC中响应到浏览器的数据格式,@ResponseBody注解和@RestController复合注解的功能详解
    7. Spring Boot2.5 安全机制与 REST API 身份验证实战
    BottomNavigation 底部导航模版
  • 原文地址:https://blog.csdn.net/web15286201346/article/details/126496803